2026-01-29 10:53:26 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Simple File-Based Cache Helper
|
|
|
|
|
*
|
|
|
|
|
* Provides caching for frequently accessed data that doesn't change often,
|
|
|
|
|
* such as workflow rules, user preferences, and configuration data.
|
|
|
|
|
*/
|
|
|
|
|
class CacheHelper {
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
private static ?string $cacheDir = null;
|
|
|
|
|
private static array $memoryCache = [];
|
2026-01-29 10:53:26 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the cache directory path
|
|
|
|
|
*
|
|
|
|
|
* @return string Cache directory path
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
private static function getCacheDir(): string {
|
2026-01-29 10:53:26 -05:00
|
|
|
if (self::$cacheDir === null) {
|
|
|
|
|
self::$cacheDir = sys_get_temp_dir() . '/tinker_tickets_cache';
|
|
|
|
|
if (!is_dir(self::$cacheDir)) {
|
|
|
|
|
mkdir(self::$cacheDir, 0755, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return self::$cacheDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a cache key from components
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Cache prefix (e.g., 'workflow', 'user_prefs')
|
|
|
|
|
* @param mixed $identifier Unique identifier
|
|
|
|
|
* @return string Cache key
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
private static function makeKey(string $prefix, $identifier = null): string {
|
2026-01-29 10:53:26 -05:00
|
|
|
$key = $prefix;
|
|
|
|
|
if ($identifier !== null) {
|
|
|
|
|
$key .= '_' . md5(serialize($identifier));
|
|
|
|
|
}
|
|
|
|
|
return preg_replace('/[^a-zA-Z0-9_]/', '_', $key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cached data
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Cache prefix
|
|
|
|
|
* @param mixed $identifier Unique identifier
|
|
|
|
|
* @param int $ttl Time-to-live in seconds (default 300 = 5 minutes)
|
|
|
|
|
* @return mixed|null Cached data or null if not found/expired
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function get(string $prefix, $identifier = null, int $ttl = 300) {
|
2026-01-29 10:53:26 -05:00
|
|
|
$key = self::makeKey($prefix, $identifier);
|
|
|
|
|
|
|
|
|
|
// Check memory cache first (fastest)
|
|
|
|
|
if (isset(self::$memoryCache[$key])) {
|
|
|
|
|
$cached = self::$memoryCache[$key];
|
|
|
|
|
if (time() - $cached['time'] < $ttl) {
|
|
|
|
|
return $cached['data'];
|
|
|
|
|
}
|
|
|
|
|
unset(self::$memoryCache[$key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check file cache
|
|
|
|
|
$filePath = self::getCacheDir() . '/' . $key . '.json';
|
|
|
|
|
if (file_exists($filePath)) {
|
|
|
|
|
$content = @file_get_contents($filePath);
|
|
|
|
|
if ($content !== false) {
|
|
|
|
|
$cached = json_decode($content, true);
|
|
|
|
|
if ($cached && isset($cached['time']) && isset($cached['data'])) {
|
|
|
|
|
if (time() - $cached['time'] < $ttl) {
|
|
|
|
|
// Store in memory cache for faster subsequent access
|
|
|
|
|
self::$memoryCache[$key] = $cached;
|
|
|
|
|
return $cached['data'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Expired - delete file
|
|
|
|
|
@unlink($filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store data in cache
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Cache prefix
|
|
|
|
|
* @param mixed $identifier Unique identifier
|
|
|
|
|
* @param mixed $data Data to cache
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function set(string $prefix, $identifier, $data): bool {
|
2026-01-29 10:53:26 -05:00
|
|
|
$key = self::makeKey($prefix, $identifier);
|
|
|
|
|
$cached = [
|
|
|
|
|
'time' => time(),
|
|
|
|
|
'data' => $data
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Store in memory cache
|
|
|
|
|
self::$memoryCache[$key] = $cached;
|
|
|
|
|
|
|
|
|
|
// Store in file cache
|
|
|
|
|
$filePath = self::getCacheDir() . '/' . $key . '.json';
|
|
|
|
|
return @file_put_contents($filePath, json_encode($cached), LOCK_EX) !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete cached data
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Cache prefix
|
|
|
|
|
* @param mixed $identifier Unique identifier (null to delete all with prefix)
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function delete(string $prefix, $identifier = null): bool {
|
2026-01-29 10:53:26 -05:00
|
|
|
if ($identifier !== null) {
|
|
|
|
|
$key = self::makeKey($prefix, $identifier);
|
|
|
|
|
unset(self::$memoryCache[$key]);
|
|
|
|
|
$filePath = self::getCacheDir() . '/' . $key . '.json';
|
|
|
|
|
return !file_exists($filePath) || @unlink($filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all files with this prefix
|
|
|
|
|
$pattern = self::getCacheDir() . '/' . preg_replace('/[^a-zA-Z0-9_]/', '_', $prefix) . '*.json';
|
|
|
|
|
$files = glob($pattern);
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
@unlink($file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear memory cache entries with this prefix
|
|
|
|
|
foreach (array_keys(self::$memoryCache) as $key) {
|
|
|
|
|
if (strpos($key, $prefix) === 0) {
|
|
|
|
|
unset(self::$memoryCache[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear all cache
|
|
|
|
|
*
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function clearAll(): bool {
|
2026-01-29 10:53:26 -05:00
|
|
|
self::$memoryCache = [];
|
|
|
|
|
|
|
|
|
|
$files = glob(self::getCacheDir() . '/*.json');
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
@unlink($file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get data from cache or fetch it using a callback
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Cache prefix
|
|
|
|
|
* @param mixed $identifier Unique identifier
|
|
|
|
|
* @param callable $callback Function to call if cache miss
|
|
|
|
|
* @param int $ttl Time-to-live in seconds
|
|
|
|
|
* @return mixed Cached or freshly fetched data
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function remember(string $prefix, $identifier, callable $callback, int $ttl = 300) {
|
2026-01-29 10:53:26 -05:00
|
|
|
$data = self::get($prefix, $identifier, $ttl);
|
|
|
|
|
|
|
|
|
|
if ($data === null) {
|
|
|
|
|
$data = $callback();
|
|
|
|
|
if ($data !== null) {
|
|
|
|
|
self::set($prefix, $identifier, $data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clean up expired cache files (call periodically)
|
|
|
|
|
*
|
|
|
|
|
* @param int $maxAge Maximum age in seconds (default 1 hour)
|
|
|
|
|
*/
|
Add PHP 7.4+ type hints to helpers, models, and middleware
Added strict typing with parameter types, return types, and property
types across all core classes:
- helpers: Database, ErrorHandler, CacheHelper
- models: TicketModel, UserModel, WorkflowModel, TemplateModel, UserPreferencesModel
- middleware: RateLimitMiddleware, CsrfMiddleware, SecurityHeadersMiddleware
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:04:36 -05:00
|
|
|
public static function cleanup(int $maxAge = 3600): void {
|
2026-01-29 10:53:26 -05:00
|
|
|
$files = glob(self::getCacheDir() . '/*.json');
|
|
|
|
|
$now = time();
|
|
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if ($now - filemtime($file) > $maxAge) {
|
|
|
|
|
@unlink($file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|