|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpRss; |
| 4 | + |
| 5 | +use PhpRss\Logger; |
| 6 | + |
| 7 | +/** |
| 8 | + * Caching layer for application data. |
| 9 | + * |
| 10 | + * Provides caching functionality with support for both file-based (SQLite-compatible) |
| 11 | + * and Redis backends. Automatically handles serialization, expiration, and cache invalidation. |
| 12 | + * |
| 13 | + * Cache keys follow the pattern: "prefix:key" (e.g., "feed_counts:123") |
| 14 | + */ |
| 15 | +class Cache |
| 16 | +{ |
| 17 | + /** @var Cache\CacheInterface|null Cache implementation instance */ |
| 18 | + private static ?Cache\CacheInterface $instance = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * Get the cache instance. |
| 22 | + * |
| 23 | + * Initializes the cache backend based on configuration (file-based or Redis). |
| 24 | + * Falls back to file cache if Redis is unavailable or disabled. |
| 25 | + * |
| 26 | + * @return Cache\CacheInterface Cache implementation |
| 27 | + */ |
| 28 | + private static function getInstance(): Cache\CacheInterface |
| 29 | + { |
| 30 | + if (self::$instance === null) { |
| 31 | + // Check if caching is enabled |
| 32 | + if (!Config::get('cache.enabled', true)) { |
| 33 | + // Return a no-op cache if disabled |
| 34 | + self::$instance = new Cache\NullCache(); |
| 35 | + return self::$instance; |
| 36 | + } |
| 37 | + |
| 38 | + $driver = Config::get('cache.driver', 'file'); |
| 39 | + |
| 40 | + if ($driver === 'redis' && extension_loaded('redis')) { |
| 41 | + try { |
| 42 | + self::$instance = new Cache\RedisCache(); |
| 43 | + } catch (\Exception $e) { |
| 44 | + // Fallback to file cache if Redis fails |
| 45 | + Logger::warning("Redis cache failed, falling back to file cache", [ |
| 46 | + 'error' => $e->getMessage() |
| 47 | + ]); |
| 48 | + self::$instance = new Cache\FileCache(); |
| 49 | + } |
| 50 | + } else { |
| 51 | + self::$instance = new Cache\FileCache(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return self::$instance; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get a value from the cache. |
| 60 | + * |
| 61 | + * @param string $key Cache key |
| 62 | + * @param mixed $default Default value if key doesn't exist |
| 63 | + * @return mixed Cached value or default |
| 64 | + */ |
| 65 | + public static function get(string $key, $default = null) |
| 66 | + { |
| 67 | + return self::getInstance()->get($key, $default); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Store a value in the cache. |
| 72 | + * |
| 73 | + * @param string $key Cache key |
| 74 | + * @param mixed $value Value to cache |
| 75 | + * @param int|null $ttl Time to live in seconds (null = use default) |
| 76 | + * @return bool True on success |
| 77 | + */ |
| 78 | + public static function set(string $key, $value, ?int $ttl = null): bool |
| 79 | + { |
| 80 | + return self::getInstance()->set($key, $value, $ttl); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Delete a value from the cache. |
| 85 | + * |
| 86 | + * @param string $key Cache key |
| 87 | + * @return bool True if key was deleted |
| 88 | + */ |
| 89 | + public static function delete(string $key): bool |
| 90 | + { |
| 91 | + return self::getInstance()->delete($key); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Check if a key exists in the cache. |
| 96 | + * |
| 97 | + * @param string $key Cache key |
| 98 | + * @return bool True if key exists |
| 99 | + */ |
| 100 | + public static function has(string $key): bool |
| 101 | + { |
| 102 | + return self::getInstance()->has($key); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Clear all cache entries with a given prefix. |
| 107 | + * |
| 108 | + * @param string $prefix Key prefix (e.g., "feed_counts") |
| 109 | + * @return int Number of keys cleared |
| 110 | + */ |
| 111 | + public static function clear(string $prefix): int |
| 112 | + { |
| 113 | + return self::getInstance()->clear($prefix); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Invalidate cache for a specific feed. |
| 118 | + * |
| 119 | + * Clears all cache entries related to a feed (counts, metadata, etc.). |
| 120 | + * |
| 121 | + * @param int $feedId Feed ID |
| 122 | + * @return void |
| 123 | + */ |
| 124 | + public static function invalidateFeed(int $feedId): void |
| 125 | + { |
| 126 | + self::delete("feed_counts:{$feedId}"); |
| 127 | + self::delete("feed_metadata:{$feedId}"); |
| 128 | + self::clear("user_feeds:"); // Clear user feed lists (they contain counts) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Invalidate cache for a user's feeds. |
| 133 | + * |
| 134 | + * Clears cached feed lists for a specific user. |
| 135 | + * |
| 136 | + * @param int $userId User ID |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + public static function invalidateUserFeeds(int $userId): void |
| 140 | + { |
| 141 | + self::delete("user_feeds:{$userId}"); |
| 142 | + self::delete("user_feeds:{$userId}:hide_no_unread"); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get or remember a value from cache. |
| 147 | + * |
| 148 | + * If the key exists, returns cached value. Otherwise, executes callback, |
| 149 | + * caches the result, and returns it. |
| 150 | + * |
| 151 | + * If caching is disabled, always executes callback and returns result without caching. |
| 152 | + * |
| 153 | + * @param string $key Cache key |
| 154 | + * @param callable $callback Callback to generate value if not cached |
| 155 | + * @param int|null $ttl Time to live in seconds |
| 156 | + * @return mixed Cached or generated value |
| 157 | + */ |
| 158 | + public static function remember(string $key, callable $callback, ?int $ttl = null) |
| 159 | + { |
| 160 | + // If caching is disabled, just execute callback |
| 161 | + if (!Config::get('cache.enabled', true)) { |
| 162 | + return $callback(); |
| 163 | + } |
| 164 | + |
| 165 | + if (self::has($key)) { |
| 166 | + return self::get($key); |
| 167 | + } |
| 168 | + |
| 169 | + $value = $callback(); |
| 170 | + self::set($key, $value, $ttl); |
| 171 | + return $value; |
| 172 | + } |
| 173 | +} |
0 commit comments