|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace imperazim\db\cache; |
| 6 | + |
| 7 | +use imperazim\db\Database; |
| 8 | + |
| 9 | +/** |
| 10 | +* Transparent query cache layer for Database. |
| 11 | +* |
| 12 | +* Usage: |
| 13 | +* $cached = new CacheLayer($db, defaultTtl: 60); |
| 14 | +* $users = $cached->remember('users:all', 30, fn() => $db->select('users', '*')); |
| 15 | +* $cached->forget('users:all'); |
| 16 | +* $cached->flush(); |
| 17 | +*/ |
| 18 | +final class CacheLayer { |
| 19 | + |
| 20 | + /** @var array<string, array{data: mixed, expires: float}> */ |
| 21 | + private array $cache = []; |
| 22 | + |
| 23 | + /** @var array<string, string[]> table => [cache keys] for auto-invalidation */ |
| 24 | + private array $tableKeys = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param Database $db Database instance |
| 28 | + * @param int $defaultTtl Default TTL in seconds |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + private Database $db, |
| 32 | + private int $defaultTtl = 60 |
| 33 | + ) {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets a cached value or computes and caches it. |
| 37 | + * |
| 38 | + * @param string $key Cache key |
| 39 | + * @param int|null $ttl TTL in seconds (null = default) |
| 40 | + * @param \Closure $loader Callback that returns the data to cache |
| 41 | + * @return mixed Cached or freshly loaded data |
| 42 | + */ |
| 43 | + public function remember(string $key, ?int $ttl, \Closure $loader): mixed { |
| 44 | + if ($this->has($key)) { |
| 45 | + return $this->cache[$key]['data']; |
| 46 | + } |
| 47 | + |
| 48 | + $data = $loader(); |
| 49 | + $this->put($key, $data, $ttl); |
| 50 | + return $data; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Stores a value in the cache. |
| 55 | + * |
| 56 | + * @param string $key Cache key |
| 57 | + * @param mixed $data Data to cache |
| 58 | + * @param int|null $ttl TTL in seconds |
| 59 | + */ |
| 60 | + public function put(string $key, mixed $data, ?int $ttl = null): void { |
| 61 | + $this->cache[$key] = [ |
| 62 | + 'data' => $data, |
| 63 | + 'expires' => microtime(true) + ($ttl ?? $this->defaultTtl), |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Checks if a non-expired cache entry exists. |
| 69 | + * |
| 70 | + * @param string $key Cache key |
| 71 | + * @return bool |
| 72 | + */ |
| 73 | + public function has(string $key): bool { |
| 74 | + if (!isset($this->cache[$key])) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + if (microtime(true) > $this->cache[$key]['expires']) { |
| 78 | + unset($this->cache[$key]); |
| 79 | + return false; |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets a cached value. |
| 86 | + * |
| 87 | + * @param string $key Cache key |
| 88 | + * @param mixed $default Default if not found |
| 89 | + * @return mixed |
| 90 | + */ |
| 91 | + public function get(string $key, mixed $default = null): mixed { |
| 92 | + return $this->has($key) ? $this->cache[$key]['data'] : $default; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Removes a cache entry. |
| 97 | + * |
| 98 | + * @param string $key Cache key |
| 99 | + */ |
| 100 | + public function forget(string $key): void { |
| 101 | + unset($this->cache[$key]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Tags a cache key as belonging to a table (for auto-invalidation). |
| 106 | + * |
| 107 | + * @param string $table Table name |
| 108 | + * @param string $key Cache key |
| 109 | + */ |
| 110 | + public function tag(string $table, string $key): void { |
| 111 | + $this->tableKeys[$table][] = $key; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Cached select with auto table tagging. |
| 116 | + * |
| 117 | + * @param string $table Table name |
| 118 | + * @param string $columns Columns to select |
| 119 | + * @param array $filters Where conditions |
| 120 | + * @param int|null $ttl Cache TTL |
| 121 | + * @return array Result rows |
| 122 | + */ |
| 123 | + public function select(string $table, string $columns = '*', array $filters = [], ?int $ttl = null): array { |
| 124 | + $key = "select:{$table}:" . md5($columns . serialize($filters)); |
| 125 | + $this->tag($table, $key); |
| 126 | + |
| 127 | + return $this->remember($key, $ttl, fn() => $this->db->select($table, $columns, $filters)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Inserts a row and invalidates cache for the table. |
| 132 | + * |
| 133 | + * @param string $table Table name |
| 134 | + * @param array $data Row data |
| 135 | + */ |
| 136 | + public function insert(string $table, array $data): void { |
| 137 | + $this->db->insert($table, $data); |
| 138 | + $this->invalidateTable($table); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Updates rows and invalidates cache for the table. |
| 143 | + * |
| 144 | + * @param string $table Table name |
| 145 | + * @param string $column Column to update |
| 146 | + * @param mixed $value New value |
| 147 | + * @param array $filters Where conditions |
| 148 | + * @return bool |
| 149 | + */ |
| 150 | + public function update(string $table, string $column, mixed $value, array $filters = []): bool { |
| 151 | + $result = $this->db->update($table, $column, $value, $filters); |
| 152 | + $this->invalidateTable($table); |
| 153 | + return $result; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Deletes rows and invalidates cache for the table. |
| 158 | + * |
| 159 | + * @param string $table Table name |
| 160 | + * @param array $filters Where conditions |
| 161 | + * @return int |
| 162 | + */ |
| 163 | + public function delete(string $table, array $filters): int { |
| 164 | + $result = $this->db->delete($table, $filters); |
| 165 | + $this->invalidateTable($table); |
| 166 | + return $result; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Invalidates all cache entries tagged to a table. |
| 171 | + * |
| 172 | + * @param string $table Table name |
| 173 | + */ |
| 174 | + public function invalidateTable(string $table): void { |
| 175 | + foreach ($this->tableKeys[$table] ?? [] as $key) { |
| 176 | + unset($this->cache[$key]); |
| 177 | + } |
| 178 | + unset($this->tableKeys[$table]); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Clears all cached data. |
| 183 | + */ |
| 184 | + public function flush(): void { |
| 185 | + $this->cache = []; |
| 186 | + $this->tableKeys = []; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns cache statistics. |
| 191 | + * |
| 192 | + * @return array{entries: int, tables: int} |
| 193 | + */ |
| 194 | + public function stats(): array { |
| 195 | + $this->cleanup(); |
| 196 | + return [ |
| 197 | + 'entries' => count($this->cache), |
| 198 | + 'tables' => count($this->tableKeys), |
| 199 | + ]; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Removes expired entries. |
| 204 | + */ |
| 205 | + private function cleanup(): void { |
| 206 | + $now = microtime(true); |
| 207 | + foreach ($this->cache as $key => $entry) { |
| 208 | + if ($now > $entry['expires']) { |
| 209 | + unset($this->cache[$key]); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns the underlying database instance. |
| 216 | + * |
| 217 | + * @return Database |
| 218 | + */ |
| 219 | + public function getDatabase(): Database { |
| 220 | + return $this->db; |
| 221 | + } |
| 222 | +} |
0 commit comments