@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * @param string|int $ttl |
| 133 | 133 | * @return mixed |
| 134 | 134 | */ |
| 135 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 135 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600): mixed |
|
| 136 | 136 | { |
| 137 | 137 | $fullCacheKey = $this->buildKey($cacheKey, $namespace); |
| 138 | 138 | $cacheData = $this->redis->get($fullCacheKey); |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | * @param string|int $ttl |
| 188 | 188 | * @return array |
| 189 | 189 | */ |
| 190 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 190 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600): array |
|
| 191 | 191 | { |
| 192 | 192 | $results = []; |
| 193 | 193 | foreach ($cacheKeys as $cacheKey) { |
@@ -285,12 +285,12 @@ discard block |
||
| 285 | 285 | * @param string|int|null $ttl |
| 286 | 286 | * @return Status|null |
| 287 | 287 | */ |
| 288 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int|null $ttl = null): ?Status |
|
| 288 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int | null $ttl = null): ?Status |
|
| 289 | 289 | { |
| 290 | 290 | $cacheFullKey = $this->buildKey($cacheKey, $namespace); |
| 291 | 291 | $serializedData = CacheRedisHelper::serialize($cacheData); |
| 292 | 292 | |
| 293 | - $result = $ttl ? $this->redis->setex($cacheFullKey, (int) $ttl, $serializedData) |
|
| 293 | + $result = $ttl ? $this->redis->setex($cacheFullKey, (int)$ttl, $serializedData) |
|
| 294 | 294 | : $this->redis->set($cacheFullKey, $serializedData); |
| 295 | 295 | |
| 296 | 296 | if ($result) { |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | * @return void |
| 333 | 333 | * @throws CacheRedisException |
| 334 | 334 | */ |
| 335 | - public function renewCache(string $cacheKey, string|int $ttl, string $namespace = ''): void |
|
| 335 | + public function renewCache(string $cacheKey, string | int $ttl, string $namespace = ''): void |
|
| 336 | 336 | { |
| 337 | 337 | $cacheFullKey = $this->buildKey($cacheKey, $namespace); |
| 338 | 338 | $dump = $this->getDump($cacheFullKey); |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * @return bool |
| 364 | 364 | * @throws CacheRedisException |
| 365 | 365 | */ |
| 366 | - private function restoreKey(string $fullKey, string|int $ttl, mixed $dump): bool |
|
| 366 | + private function restoreKey(string $fullKey, string | int $ttl, mixed $dump): bool |
|
| 367 | 367 | { |
| 368 | 368 | try { |
| 369 | 369 | $this->redis->restore($fullKey, $ttl * 1000, $dump, 'REPLACE'); |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | * @return mixed |
| 190 | 190 | * @throws CacheFileException return string|void |
| 191 | 191 | */ |
| 192 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 192 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600): mixed |
|
| 193 | 193 | { |
| 194 | 194 | |
| 195 | 195 | $ttl = CacheFileHelper::ttl($ttl, $this->defaultTTL); |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | * @return array |
| 278 | 278 | * @throws CacheFileException |
| 279 | 279 | */ |
| 280 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 280 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600): array |
|
| 281 | 281 | { |
| 282 | 282 | $ttl = CacheFileHelper::ttl($ttl, $this->defaultTTL); |
| 283 | 283 | $results = []; |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | * @return void |
| 325 | 325 | * @throws CacheFileException |
| 326 | 326 | */ |
| 327 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600): void |
|
| 327 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600): void |
|
| 328 | 328 | { |
| 329 | 329 | $cacheFile = $this->buildCacheFilePath($cacheKey, $namespace); |
| 330 | 330 | $data = $this->fileManager->serialize($cacheData); |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * @return void |
| 364 | 364 | * @throws CacheFileException |
| 365 | 365 | */ |
| 366 | - public function renewCache(string $cacheKey, string|int $ttl, string $namespace = ''): void |
|
| 366 | + public function renewCache(string $cacheKey, string | int $ttl, string $namespace = ''): void |
|
| 367 | 367 | { |
| 368 | 368 | $cacheData = $this->getCache($cacheKey, $namespace); |
| 369 | 369 | if ($cacheData) { |
@@ -13,111 +13,111 @@ discard block |
||
| 13 | 13 | class ArrayCacheStore implements CacheerInterface |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @param array $arrayStore |
|
| 18 | - */ |
|
| 19 | - private array $arrayStore = []; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var boolean |
|
| 23 | - */ |
|
| 24 | - private bool $success = false; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private string $message = ''; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var ?CacheLogger |
|
| 33 | - */ |
|
| 34 | - private ?CacheLogger $logger = null; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * ArrayCacheStore constructor. |
|
| 38 | - * |
|
| 39 | - * @param string $logPath |
|
| 40 | - */ |
|
| 41 | - public function __construct(string $logPath) |
|
| 42 | - { |
|
| 16 | + /** |
|
| 17 | + * @param array $arrayStore |
|
| 18 | + */ |
|
| 19 | + private array $arrayStore = []; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var boolean |
|
| 23 | + */ |
|
| 24 | + private bool $success = false; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private string $message = ''; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var ?CacheLogger |
|
| 33 | + */ |
|
| 34 | + private ?CacheLogger $logger = null; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * ArrayCacheStore constructor. |
|
| 38 | + * |
|
| 39 | + * @param string $logPath |
|
| 40 | + */ |
|
| 41 | + public function __construct(string $logPath) |
|
| 42 | + { |
|
| 43 | 43 | $this->logger = new CacheLogger($logPath); |
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Appends data to an existing cache item. |
|
| 48 | - * |
|
| 49 | - * @param string $cacheKey |
|
| 50 | - * @param mixed $cacheData |
|
| 51 | - * @param string $namespace |
|
| 52 | - * @return bool |
|
| 53 | - */ |
|
| 54 | - public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = ''): bool |
|
| 55 | - { |
|
| 56 | - $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
| 57 | - |
|
| 58 | - if (!$this->has($cacheKey, $namespace)) { |
|
| 59 | - $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
| 60 | - $this->logger->debug("{$this->getMessage()} from array driver."); |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
| 65 | - $this->setMessage("Cache appended successfully", true); |
|
| 66 | - return true; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Builds a unique key for the array store. |
|
| 71 | - * |
|
| 72 | - * @param string $cacheKey |
|
| 73 | - * @param string $namespace |
|
| 74 | - * @return string |
|
| 75 | - */ |
|
| 76 | - private function buildArrayKey(string $cacheKey, string $namespace = ''): string |
|
| 77 | - { |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Appends data to an existing cache item. |
|
| 48 | + * |
|
| 49 | + * @param string $cacheKey |
|
| 50 | + * @param mixed $cacheData |
|
| 51 | + * @param string $namespace |
|
| 52 | + * @return bool |
|
| 53 | + */ |
|
| 54 | + public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = ''): bool |
|
| 55 | + { |
|
| 56 | + $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
| 57 | + |
|
| 58 | + if (!$this->has($cacheKey, $namespace)) { |
|
| 59 | + $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
| 60 | + $this->logger->debug("{$this->getMessage()} from array driver."); |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
| 65 | + $this->setMessage("Cache appended successfully", true); |
|
| 66 | + return true; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Builds a unique key for the array store. |
|
| 71 | + * |
|
| 72 | + * @param string $cacheKey |
|
| 73 | + * @param string $namespace |
|
| 74 | + * @return string |
|
| 75 | + */ |
|
| 76 | + private function buildArrayKey(string $cacheKey, string $namespace = ''): string |
|
| 77 | + { |
|
| 78 | 78 | return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey; |
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Clears a specific cache item. |
|
| 83 | - * |
|
| 84 | - * @param string $cacheKey |
|
| 85 | - * @param string $namespace |
|
| 86 | - * @return void |
|
| 87 | - */ |
|
| 88 | - public function clearCache(string $cacheKey, string $namespace = ''): void |
|
| 89 | - { |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Clears a specific cache item. |
|
| 83 | + * |
|
| 84 | + * @param string $cacheKey |
|
| 85 | + * @param string $namespace |
|
| 86 | + * @return void |
|
| 87 | + */ |
|
| 88 | + public function clearCache(string $cacheKey, string $namespace = ''): void |
|
| 89 | + { |
|
| 90 | 90 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 91 | 91 | unset($this->arrayStore[$arrayStoreKey]); |
| 92 | 92 | $this->setMessage("Cache cleared successfully", true); |
| 93 | 93 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Decrements a cache item by a specified amount. |
|
| 98 | - * |
|
| 99 | - * @param string $cacheKey |
|
| 100 | - * @param int $amount |
|
| 101 | - * @param string $namespace |
|
| 102 | - * @return bool |
|
| 103 | - */ |
|
| 104 | - public function decrement(string $cacheKey, int $amount = 1, string $namespace = ''): bool |
|
| 105 | - { |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Decrements a cache item by a specified amount. |
|
| 98 | + * |
|
| 99 | + * @param string $cacheKey |
|
| 100 | + * @param int $amount |
|
| 101 | + * @param string $namespace |
|
| 102 | + * @return bool |
|
| 103 | + */ |
|
| 104 | + public function decrement(string $cacheKey, int $amount = 1, string $namespace = ''): bool |
|
| 105 | + { |
|
| 106 | 106 | return $this->increment($cacheKey, ($amount * -1), $namespace); |
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Flushes all cache items. |
|
| 111 | - * |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function flushCache(): void |
|
| 115 | - { |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Flushes all cache items. |
|
| 111 | + * |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function flushCache(): void |
|
| 115 | + { |
|
| 116 | 116 | unset($this->arrayStore); |
| 117 | 117 | $this->arrayStore = []; |
| 118 | 118 | $this->setMessage("Cache flushed successfully", true); |
| 119 | 119 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 120 | - } |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | 123 | * Stores a cache item permanently. |
@@ -126,226 +126,226 @@ discard block |
||
| 126 | 126 | * @param mixed $cacheData |
| 127 | 127 | * @return void |
| 128 | 128 | */ |
| 129 | - public function forever(string $cacheKey, mixed $cacheData): void |
|
| 130 | - { |
|
| 129 | + public function forever(string $cacheKey, mixed $cacheData): void |
|
| 130 | + { |
|
| 131 | 131 | $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
| 132 | 132 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Retrieves a single cache item. |
|
| 137 | - * |
|
| 138 | - * @param string $cacheKey |
|
| 139 | - * @param string $namespace |
|
| 140 | - * @param int|string $ttl |
|
| 141 | - * @return mixed |
|
| 142 | - */ |
|
| 143 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 144 | - { |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Retrieves a single cache item. |
|
| 137 | + * |
|
| 138 | + * @param string $cacheKey |
|
| 139 | + * @param string $namespace |
|
| 140 | + * @param int|string $ttl |
|
| 141 | + * @return mixed |
|
| 142 | + */ |
|
| 143 | + public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 144 | + { |
|
| 145 | 145 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 146 | 146 | |
| 147 | 147 | if (!$this->has($cacheKey, $namespace)) { |
| 148 | - $this->handleCacheNotFound(); |
|
| 149 | - return false; |
|
| 148 | + $this->handleCacheNotFound(); |
|
| 149 | + return false; |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | $cacheData = $this->arrayStore[$arrayStoreKey]; |
| 153 | 153 | if ($this->isExpired($cacheData)) { |
| 154 | - $this->handleCacheExpired($arrayStoreKey); |
|
| 155 | - return false; |
|
| 154 | + $this->handleCacheExpired($arrayStoreKey); |
|
| 155 | + return false; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | $this->setMessage("Cache retrieved successfully", true); |
| 159 | 159 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 160 | 160 | return $this->serialize($cacheData['cacheData'], false); |
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Verify if the cache is expired. |
|
| 165 | - * |
|
| 166 | - * @param array $cacheData |
|
| 167 | - * @return bool |
|
| 168 | - */ |
|
| 169 | - private function isExpired(array $cacheData): bool |
|
| 170 | - { |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Verify if the cache is expired. |
|
| 165 | + * |
|
| 166 | + * @param array $cacheData |
|
| 167 | + * @return bool |
|
| 168 | + */ |
|
| 169 | + private function isExpired(array $cacheData): bool |
|
| 170 | + { |
|
| 171 | 171 | $expirationTime = $cacheData['expirationTime'] ?? 0; |
| 172 | 172 | $now = time(); |
| 173 | 173 | return $expirationTime !== 0 && $now >= $expirationTime; |
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Handles the case when cache data is not found. |
|
| 178 | - * |
|
| 179 | - * @return void |
|
| 180 | - */ |
|
| 181 | - private function handleCacheNotFound(): void |
|
| 182 | - { |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Handles the case when cache data is not found. |
|
| 178 | + * |
|
| 179 | + * @return void |
|
| 180 | + */ |
|
| 181 | + private function handleCacheNotFound(): void |
|
| 182 | + { |
|
| 183 | 183 | $this->setMessage("cacheData not found, does not exists or expired", false); |
| 184 | 184 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Handles the case when cache data has expired. |
|
| 189 | - * |
|
| 190 | - * @param string $arrayStoreKey |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - private function handleCacheExpired(string $arrayStoreKey): void |
|
| 194 | - { |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Handles the case when cache data has expired. |
|
| 189 | + * |
|
| 190 | + * @param string $arrayStoreKey |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + private function handleCacheExpired(string $arrayStoreKey): void |
|
| 194 | + { |
|
| 195 | 195 | $parts = explode(':', $arrayStoreKey, 2); |
| 196 | 196 | if (count($parts) === 2) { |
| 197 | - list($np, $key) = $parts; |
|
| 197 | + list($np, $key) = $parts; |
|
| 198 | 198 | } else { |
| 199 | - $np = ''; |
|
| 200 | - $key = $arrayStoreKey; |
|
| 199 | + $np = ''; |
|
| 200 | + $key = $arrayStoreKey; |
|
| 201 | 201 | } |
| 202 | 202 | $this->clearCache($key, $np); |
| 203 | 203 | $this->setMessage("cacheKey: {$key} has expired.", false); |
| 204 | 204 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Gets all items in a specific namespace. |
|
| 209 | - * |
|
| 210 | - * @param string $namespace |
|
| 211 | - * @return array |
|
| 212 | - */ |
|
| 213 | - public function getAll(string $namespace = ''): array |
|
| 214 | - { |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Gets all items in a specific namespace. |
|
| 209 | + * |
|
| 210 | + * @param string $namespace |
|
| 211 | + * @return array |
|
| 212 | + */ |
|
| 213 | + public function getAll(string $namespace = ''): array |
|
| 214 | + { |
|
| 215 | 215 | $results = []; |
| 216 | 216 | foreach ($this->arrayStore as $key => $data) { |
| 217 | - if (str_starts_with($key, $namespace . ':') || empty($namespace)) { |
|
| 217 | + if (str_starts_with($key, $namespace . ':') || empty($namespace)) { |
|
| 218 | 218 | $results[$key] = $this->serialize($data['cacheData'], false); |
| 219 | - } |
|
| 219 | + } |
|
| 220 | 220 | } |
| 221 | 221 | return $results; |
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Retrieves multiple cache items by their keys. |
|
| 226 | - * |
|
| 227 | - * @param array $cacheKeys |
|
| 228 | - * @param string $namespace |
|
| 229 | - * @param string|int $ttl |
|
| 230 | - * @return array |
|
| 231 | - */ |
|
| 232 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 233 | - { |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Retrieves multiple cache items by their keys. |
|
| 226 | + * |
|
| 227 | + * @param array $cacheKeys |
|
| 228 | + * @param string $namespace |
|
| 229 | + * @param string|int $ttl |
|
| 230 | + * @return array |
|
| 231 | + */ |
|
| 232 | + public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 233 | + { |
|
| 234 | 234 | $results = []; |
| 235 | 235 | foreach ($cacheKeys as $cacheKey) { |
| 236 | - $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
| 236 | + $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
| 237 | 237 | } |
| 238 | 238 | return $results; |
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Checks if a cache item exists. |
|
| 243 | - * |
|
| 244 | - * @param string $cacheKey |
|
| 245 | - * @param string $namespace |
|
| 246 | - * @return bool |
|
| 247 | - */ |
|
| 248 | - public function has(string $cacheKey, string $namespace = ''): bool |
|
| 249 | - { |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Checks if a cache item exists. |
|
| 243 | + * |
|
| 244 | + * @param string $cacheKey |
|
| 245 | + * @param string $namespace |
|
| 246 | + * @return bool |
|
| 247 | + */ |
|
| 248 | + public function has(string $cacheKey, string $namespace = ''): bool |
|
| 249 | + { |
|
| 250 | 250 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 251 | 251 | return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime']; |
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Increments a cache item by a specified amount. |
|
| 256 | - * |
|
| 257 | - * @param string $cacheKey |
|
| 258 | - * @param int $amount |
|
| 259 | - * @param string $namespace |
|
| 260 | - * @return bool |
|
| 261 | - */ |
|
| 262 | - public function increment(string $cacheKey, int $amount = 1, string $namespace = ''): bool |
|
| 263 | - { |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Increments a cache item by a specified amount. |
|
| 256 | + * |
|
| 257 | + * @param string $cacheKey |
|
| 258 | + * @param int $amount |
|
| 259 | + * @param string $namespace |
|
| 260 | + * @return bool |
|
| 261 | + */ |
|
| 262 | + public function increment(string $cacheKey, int $amount = 1, string $namespace = ''): bool |
|
| 263 | + { |
|
| 264 | 264 | $cacheData = $this->getCache($cacheKey, $namespace); |
| 265 | 265 | |
| 266 | 266 | if(!empty($cacheData) && is_numeric($cacheData)) { |
| 267 | - $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
| 268 | - $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
| 269 | - return true; |
|
| 267 | + $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
| 268 | + $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
| 269 | + return true; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | return false; |
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Checks if the operation was successful. |
|
| 277 | - * |
|
| 278 | - * @return boolean |
|
| 279 | - */ |
|
| 280 | - public function isSuccess(): bool |
|
| 281 | - { |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Checks if the operation was successful. |
|
| 277 | + * |
|
| 278 | + * @return boolean |
|
| 279 | + */ |
|
| 280 | + public function isSuccess(): bool |
|
| 281 | + { |
|
| 282 | 282 | return $this->success; |
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Gets the last message. |
|
| 287 | - * |
|
| 288 | - * @return string |
|
| 289 | - */ |
|
| 290 | - public function getMessage(): string |
|
| 291 | - { |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Gets the last message. |
|
| 287 | + * |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 290 | + public function getMessage(): string |
|
| 291 | + { |
|
| 292 | 292 | return $this->message; |
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Stores an item in the cache with a specific TTL. |
|
| 297 | - * |
|
| 298 | - * @param string $cacheKey |
|
| 299 | - * @param mixed $cacheData |
|
| 300 | - * @param string $namespace |
|
| 301 | - * @param int|string $ttl |
|
| 302 | - * @return bool |
|
| 303 | - */ |
|
| 304 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): bool |
|
| 305 | - { |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Stores an item in the cache with a specific TTL. |
|
| 297 | + * |
|
| 298 | + * @param string $cacheKey |
|
| 299 | + * @param mixed $cacheData |
|
| 300 | + * @param string $namespace |
|
| 301 | + * @param int|string $ttl |
|
| 302 | + * @return bool |
|
| 303 | + */ |
|
| 304 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): bool |
|
| 305 | + { |
|
| 306 | 306 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 307 | 307 | |
| 308 | 308 | $this->arrayStore[$arrayStoreKey] = [ |
| 309 | - 'cacheData' => serialize($cacheData), |
|
| 310 | - 'expirationTime' => time() + $ttl |
|
| 309 | + 'cacheData' => serialize($cacheData), |
|
| 310 | + 'expirationTime' => time() + $ttl |
|
| 311 | 311 | ]; |
| 312 | 312 | |
| 313 | 313 | $this->setMessage("Cache stored successfully", true); |
| 314 | 314 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
| 315 | 315 | return true; |
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Stores multiple items in the cache in batches. |
|
| 320 | - * |
|
| 321 | - * @param array $items |
|
| 322 | - * @param string $namespace |
|
| 323 | - * @param int $batchSize |
|
| 324 | - * @return void |
|
| 325 | - */ |
|
| 326 | - public function putMany(array $items, string $namespace = '', int $batchSize = 100): void |
|
| 327 | - { |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Stores multiple items in the cache in batches. |
|
| 320 | + * |
|
| 321 | + * @param array $items |
|
| 322 | + * @param string $namespace |
|
| 323 | + * @param int $batchSize |
|
| 324 | + * @return void |
|
| 325 | + */ |
|
| 326 | + public function putMany(array $items, string $namespace = '', int $batchSize = 100): void |
|
| 327 | + { |
|
| 328 | 328 | $chunks = array_chunk($items, $batchSize, true); |
| 329 | 329 | |
| 330 | 330 | foreach ($chunks as $chunk) { |
| 331 | - foreach ($chunk as $key => $data) { |
|
| 332 | - $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
| 331 | + foreach ($chunk as $key => $data) { |
|
| 332 | + $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
| 333 | + } |
|
| 333 | 334 | } |
| 334 | - } |
|
| 335 | 335 | $this->setMessage("{$this->getMessage()}", $this->isSuccess()); |
| 336 | 336 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Renews the expiration time of a cache item. |
|
| 341 | - * |
|
| 342 | - * @param string $cacheKey |
|
| 343 | - * @param string|int $ttl |
|
| 344 | - * @param string $namespace |
|
| 345 | - * @return void |
|
| 346 | - */ |
|
| 347 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = ''): void |
|
| 348 | - { |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Renews the expiration time of a cache item. |
|
| 341 | + * |
|
| 342 | + * @param string $cacheKey |
|
| 343 | + * @param string|int $ttl |
|
| 344 | + * @param string $namespace |
|
| 345 | + * @return void |
|
| 346 | + */ |
|
| 347 | + public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = ''): void |
|
| 348 | + { |
|
| 349 | 349 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 350 | 350 | |
| 351 | 351 | if (isset($this->arrayStore[$arrayStoreKey])) { |
@@ -353,31 +353,31 @@ discard block |
||
| 353 | 353 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
| 354 | 354 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
| 355 | 355 | $this->logger->debug("{$this->getMessage()} from array driver."); |
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Sets a message and its success status. |
|
| 361 | - * |
|
| 362 | - * @param string $message |
|
| 363 | - * @param boolean $success |
|
| 364 | - * @return void |
|
| 365 | - */ |
|
| 366 | - private function setMessage(string $message, bool $success): void |
|
| 367 | - { |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Sets a message and its success status. |
|
| 361 | + * |
|
| 362 | + * @param string $message |
|
| 363 | + * @param boolean $success |
|
| 364 | + * @return void |
|
| 365 | + */ |
|
| 366 | + private function setMessage(string $message, bool $success): void |
|
| 367 | + { |
|
| 368 | 368 | $this->message = $message; |
| 369 | 369 | $this->success = $success; |
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Serializes or unserializes data based on the flag. |
|
| 374 | - * |
|
| 375 | - * @param mixed $data |
|
| 376 | - * @param bool $serialize |
|
| 377 | - * @return mixed |
|
| 378 | - */ |
|
| 379 | - private function serialize(mixed $data, bool $serialize = true): mixed |
|
| 380 | - { |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Serializes or unserializes data based on the flag. |
|
| 374 | + * |
|
| 375 | + * @param mixed $data |
|
| 376 | + * @param bool $serialize |
|
| 377 | + * @return mixed |
|
| 378 | + */ |
|
| 379 | + private function serialize(mixed $data, bool $serialize = true): mixed |
|
| 380 | + { |
|
| 381 | 381 | return $serialize ? serialize($data) : unserialize($data); |
| 382 | - } |
|
| 382 | + } |
|
| 383 | 383 | } |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | * @param int|string $ttl |
| 141 | 141 | * @return mixed |
| 142 | 142 | */ |
| 143 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 143 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600): mixed |
|
| 144 | 144 | { |
| 145 | 145 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 146 | 146 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | * @param string|int $ttl |
| 230 | 230 | * @return array |
| 231 | 231 | */ |
| 232 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 232 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600): array |
|
| 233 | 233 | { |
| 234 | 234 | $results = []; |
| 235 | 235 | foreach ($cacheKeys as $cacheKey) { |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | { |
| 264 | 264 | $cacheData = $this->getCache($cacheKey, $namespace); |
| 265 | 265 | |
| 266 | - if(!empty($cacheData) && is_numeric($cacheData)) { |
|
| 266 | + if (!empty($cacheData) && is_numeric($cacheData)) { |
|
| 267 | 267 | $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
| 268 | 268 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
| 269 | 269 | return true; |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @param int|string $ttl |
| 302 | 302 | * @return bool |
| 303 | 303 | */ |
| 304 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): bool |
|
| 304 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600): bool |
|
| 305 | 305 | { |
| 306 | 306 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 307 | 307 | |
@@ -344,12 +344,12 @@ discard block |
||
| 344 | 344 | * @param string $namespace |
| 345 | 345 | * @return void |
| 346 | 346 | */ |
| 347 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = ''): void |
|
| 347 | + public function renewCache(string $cacheKey, int | string $ttl = 3600, string $namespace = ''): void |
|
| 348 | 348 | { |
| 349 | 349 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
| 350 | 350 | |
| 351 | 351 | if (isset($this->arrayStore[$arrayStoreKey])) { |
| 352 | - $ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time(); |
|
| 352 | + $ttlSeconds = is_numeric($ttl) ? (int)$ttl : strtotime($ttl) - time(); |
|
| 353 | 353 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
| 354 | 354 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
| 355 | 355 | $this->logger->debug("{$this->getMessage()} from array driver."); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | public function clearCache(string $cacheKey, string $namespace = ''): void |
| 78 | 78 | { |
| 79 | 79 | $data = $this->cacheRepository->clear($cacheKey, $namespace); |
| 80 | - if($data) { |
|
| 80 | + if ($data) { |
|
| 81 | 81 | $this->setMessage("Cache deleted successfully!", true); |
| 82 | 82 | } else { |
| 83 | 83 | $this->setMessage("Cache does not exists!", false); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | public function flushCache(): void |
| 95 | 95 | { |
| 96 | - if($this->cacheRepository->flush()){ |
|
| 96 | + if ($this->cacheRepository->flush()) { |
|
| 97 | 97 | $this->setMessage("Flush finished successfully", true); |
| 98 | 98 | } else { |
| 99 | 99 | $this->setMessage("Something went wrong. Please, try again.", false); |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * @param string|int $ttl |
| 112 | 112 | * @return mixed |
| 113 | 113 | */ |
| 114 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed |
|
| 114 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600): mixed |
|
| 115 | 115 | { |
| 116 | 116 | $cacheData = $this->retrieveCache($cacheKey, $namespace); |
| 117 | 117 | if ($cacheData) { |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * @param string|int $ttl |
| 152 | 152 | * @return array |
| 153 | 153 | */ |
| 154 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): array |
|
| 154 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600): array |
|
| 155 | 155 | { |
| 156 | 156 | $cacheData = []; |
| 157 | 157 | foreach ($cacheKeys as $cacheKey) { |
@@ -234,9 +234,9 @@ discard block |
||
| 234 | 234 | * @param string|int $ttl |
| 235 | 235 | * @return bool |
| 236 | 236 | */ |
| 237 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600): bool |
|
| 237 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600): bool |
|
| 238 | 238 | { |
| 239 | - if($this->storeCache($cacheKey, $cacheData, $namespace, $ttl)){ |
|
| 239 | + if ($this->storeCache($cacheKey, $cacheData, $namespace, $ttl)) { |
|
| 240 | 240 | $this->logger->debug("{$this->getMessage()} from database driver."); |
| 241 | 241 | return true; |
| 242 | 242 | } |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | */ |
| 272 | 272 | private function processBatchItems(array $batchItems, string $namespace): void |
| 273 | 273 | { |
| 274 | - foreach($batchItems as $item) { |
|
| 274 | + foreach ($batchItems as $item) { |
|
| 275 | 275 | CacheDatabaseHelper::validateCacheItem($item); |
| 276 | 276 | $cacheKey = $item['cacheKey']; |
| 277 | 277 | $cacheData = $item['cacheData']; |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param string $namespace |
| 289 | 289 | * @return bool |
| 290 | 290 | */ |
| 291 | - private function renew(string $cacheKey, string|int $ttl = 3600, string $namespace = ''): bool |
|
| 291 | + private function renew(string $cacheKey, string | int $ttl = 3600, string $namespace = ''): bool |
|
| 292 | 292 | { |
| 293 | 293 | $cacheData = $this->getCache($cacheKey, $namespace); |
| 294 | 294 | if ($cacheData) { |
@@ -336,10 +336,10 @@ discard block |
||
| 336 | 336 | * @param string|int $ttl |
| 337 | 337 | * @return bool |
| 338 | 338 | */ |
| 339 | - private function storeCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600): bool |
|
| 339 | + private function storeCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600): bool |
|
| 340 | 340 | { |
| 341 | 341 | $data = $this->cacheRepository->store($cacheKey, $cacheData, $namespace, $ttl); |
| 342 | - if($data) { |
|
| 342 | + if ($data) { |
|
| 343 | 343 | $this->setMessage("Cache Stored Successfully", true); |
| 344 | 344 | return true; |
| 345 | 345 | } |
@@ -358,7 +358,7 @@ discard block |
||
| 358 | 358 | private function updateCache(string $cacheKey, mixed $cacheData, string $namespace = ''): bool |
| 359 | 359 | { |
| 360 | 360 | $data = $this->cacheRepository->update($cacheKey, $cacheData, $namespace); |
| 361 | - if($data) { |
|
| 361 | + if ($data) { |
|
| 362 | 362 | $this->setMessage("Cache updated successfully.", true); |
| 363 | 363 | return true; |
| 364 | 364 | } |
@@ -53,18 +53,18 @@ discard block |
||
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | - * @param string $filename |
|
| 57 | - * @return bool |
|
| 58 | - */ |
|
| 56 | + * @param string $filename |
|
| 57 | + * @return bool |
|
| 58 | + */ |
|
| 59 | 59 | public function fileExists(string $filename): bool |
| 60 | 60 | { |
| 61 | 61 | return file_exists($filename); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | - * @param string $filename |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 65 | + * @param string $filename |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | 68 | public function removeFile(string $filename): void |
| 69 | 69 | { |
| 70 | 70 | if (file_exists($filename)) { |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | - * @param string $dir |
|
| 77 | - * @return void |
|
| 78 | - */ |
|
| 76 | + * @param string $dir |
|
| 77 | + * @return void |
|
| 78 | + */ |
|
| 79 | 79 | public function clearDirectory(string $dir): void |
| 80 | 80 | { |
| 81 | 81 | $iterator = new RecursiveIteratorIterator( |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * @return string|bool |
| 45 | 45 | * @throws CacheFileException |
| 46 | 46 | */ |
| 47 | - public function readFile(string $filename): string|bool |
|
| 47 | + public function readFile(string $filename): string | bool |
|
| 48 | 48 | { |
| 49 | 49 | if (!$this->fileExists($filename)) { |
| 50 | 50 | throw CacheFileException::create("File not found: {$filename}"); |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function serialize(mixed $data, bool $serialize = true): mixed |
| 97 | 97 | { |
| 98 | - if($serialize) { |
|
| 98 | + if ($serialize) { |
|
| 99 | 99 | return serialize($data); |
| 100 | 100 | } |
| 101 | 101 | return unserialize($data); |
@@ -14,42 +14,42 @@ |
||
| 14 | 14 | class RedisCacheManager |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** @var Predis\Client */ |
|
| 18 | - private static $redis; |
|
| 19 | - |
|
| 20 | - /** @param string $namespace */ |
|
| 21 | - private static $namespace; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Connects to the Redis server using the configuration defined in REDIS_CONNECTION_CONFIG. |
|
| 25 | - * |
|
| 26 | - * @return Client |
|
| 27 | - */ |
|
| 28 | - public static function connect() |
|
| 29 | - { |
|
| 17 | + /** @var Predis\Client */ |
|
| 18 | + private static $redis; |
|
| 19 | + |
|
| 20 | + /** @param string $namespace */ |
|
| 21 | + private static $namespace; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Connects to the Redis server using the configuration defined in REDIS_CONNECTION_CONFIG. |
|
| 25 | + * |
|
| 26 | + * @return Client |
|
| 27 | + */ |
|
| 28 | + public static function connect() |
|
| 29 | + { |
|
| 30 | 30 | Autoloader::register(); |
| 31 | 31 | self::$redis = new Client([ |
| 32 | - 'scheme' => 'tcp', |
|
| 33 | - 'host' => REDIS_CONNECTION_CONFIG['REDIS_HOST'], |
|
| 34 | - 'port' => REDIS_CONNECTION_CONFIG['REDIS_PORT'], |
|
| 35 | - 'password' => REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'], |
|
| 36 | - 'database' => 0 |
|
| 32 | + 'scheme' => 'tcp', |
|
| 33 | + 'host' => REDIS_CONNECTION_CONFIG['REDIS_HOST'], |
|
| 34 | + 'port' => REDIS_CONNECTION_CONFIG['REDIS_PORT'], |
|
| 35 | + 'password' => REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'], |
|
| 36 | + 'database' => 0 |
|
| 37 | 37 | ]); |
| 38 | 38 | self::auth(); |
| 39 | 39 | self::$namespace = REDIS_CONNECTION_CONFIG['REDIS_NAMESPACE'] ?? 'Cache'; |
| 40 | 40 | return self::$redis; |
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Authenticates the Redis connection if a password is provided in the configuration. |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - private static function auth(): void |
|
| 49 | - { |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Authenticates the Redis connection if a password is provided in the configuration. |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + private static function auth(): void |
|
| 49 | + { |
|
| 50 | 50 | if(is_string(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']) && REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'] !== '') { |
| 51 | - self::$redis->auth(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']); |
|
| 51 | + self::$redis->auth(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']); |
|
| 52 | + } |
|
| 52 | 53 | } |
| 53 | - } |
|
| 54 | 54 | |
| 55 | 55 | } |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | */ |
| 48 | 48 | private static function auth(): void |
| 49 | 49 | { |
| 50 | - if(is_string(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']) && REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'] !== '') { |
|
| 50 | + if (is_string(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']) && REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'] !== '') { |
|
| 51 | 51 | self::$redis->auth(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']); |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -13,18 +13,18 @@ discard block |
||
| 13 | 13 | class FileCacheFlusher |
| 14 | 14 | { |
| 15 | 15 | /** |
| 16 | - * @var FileCacheManager |
|
| 17 | - */ |
|
| 16 | + * @var FileCacheManager |
|
| 17 | + */ |
|
| 18 | 18 | private FileCacheManager $fileManager; |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * @var string $cacheDir |
|
| 22 | - */ |
|
| 21 | + * @var string $cacheDir |
|
| 22 | + */ |
|
| 23 | 23 | private string $cacheDir; |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | - * @var string $lastFlushTimeFile |
|
| 27 | - */ |
|
| 26 | + * @var string $lastFlushTimeFile |
|
| 27 | + */ |
|
| 28 | 28 | private string $lastFlushTimeFile; |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -41,10 +41,10 @@ discard block |
||
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Flushes all cache items and updates the last flush timestamp. |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 44 | + * Flushes all cache items and updates the last flush timestamp. |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | 48 | public function flushCache(): void |
| 49 | 49 | { |
| 50 | 50 | $this->fileManager->clearDirectory($this->cacheDir); |
@@ -52,11 +52,11 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | - * Handles the auto-flush functionality based on options. |
|
| 56 | - * |
|
| 57 | - * @param array $options |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 55 | + * Handles the auto-flush functionality based on options. |
|
| 56 | + * |
|
| 57 | + * @param array $options |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | 60 | public function handleAutoFlush(array $options): void |
| 61 | 61 | { |
| 62 | 62 | if (isset($options['flushAfter'])) { |
@@ -60,11 +60,11 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * Retrieves cache data from the database. |
|
| 64 | - * |
|
| 65 | - * @param string $cacheKey |
|
| 66 | - * @param string $namespace |
|
| 67 | - * @return mixed |
|
| 63 | + * Retrieves cache data from the database. |
|
| 64 | + * |
|
| 65 | + * @param string $cacheKey |
|
| 66 | + * @param string $namespace |
|
| 67 | + * @return mixed |
|
| 68 | 68 | */ |
| 69 | 69 | public function retrieve(string $cacheKey, string $namespace = ''): mixed |
| 70 | 70 | { |
@@ -109,10 +109,10 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | - * Get Update query based on the database driver. |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 112 | + * Get Update query based on the database driver. |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | 116 | private function getUpdateQueryWithDriver(): string |
| 117 | 117 | { |
| 118 | 118 | $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); |
@@ -123,10 +123,10 @@ discard block |
||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | - * Get Delete query based on the database driver. |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 126 | + * Get Delete query based on the database driver. |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | 130 | private function getDeleteQueryWithDriver(): string |
| 131 | 131 | { |
| 132 | 132 | $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); |
@@ -137,13 +137,13 @@ discard block |
||
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | - * Updates an existing cache item in the database. |
|
| 141 | - * |
|
| 142 | - * @param string $cacheKey |
|
| 143 | - * @param mixed $cacheData |
|
| 144 | - * @param string $namespace |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 140 | + * Updates an existing cache item in the database. |
|
| 141 | + * |
|
| 142 | + * @param string $cacheKey |
|
| 143 | + * @param mixed $cacheData |
|
| 144 | + * @param string $namespace |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | 147 | public function update(string $cacheKey, mixed $cacheData, string $namespace = ''): bool |
| 148 | 148 | { |
| 149 | 149 | $query = $this->getUpdateQueryWithDriver(); |
@@ -157,12 +157,12 @@ discard block |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
| 160 | - * Clears a specific cache item from the database. |
|
| 161 | - * |
|
| 162 | - * @param string $cacheKey |
|
| 163 | - * @param string $namespace |
|
| 164 | - * @return bool |
|
| 165 | - */ |
|
| 160 | + * Clears a specific cache item from the database. |
|
| 161 | + * |
|
| 162 | + * @param string $cacheKey |
|
| 163 | + * @param string $namespace |
|
| 164 | + * @return bool |
|
| 165 | + */ |
|
| 166 | 166 | public function clear(string $cacheKey, string $namespace = ''): bool |
| 167 | 167 | { |
| 168 | 168 | $query = $this->getDeleteQueryWithDriver(); |
@@ -175,10 +175,10 @@ discard block |
||
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
| 178 | - * Gets the query to renew the expiration time of a cache item based on the database driver. |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - */ |
|
| 178 | + * Gets the query to renew the expiration time of a cache item based on the database driver. |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + */ |
|
| 182 | 182 | private function getRenewExpirationQueryWithDriver(): string |
| 183 | 183 | { |
| 184 | 184 | $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); |
@@ -193,13 +193,13 @@ discard block |
||
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
| 196 | - * Checks if a cache item is valid based on its key, namespace, and current time. |
|
| 197 | - * |
|
| 198 | - * @param string $cacheKey |
|
| 199 | - * @param string $namespace |
|
| 200 | - * @param string $currentTime |
|
| 201 | - * @return bool |
|
| 202 | - */ |
|
| 196 | + * Checks if a cache item is valid based on its key, namespace, and current time. |
|
| 197 | + * |
|
| 198 | + * @param string $cacheKey |
|
| 199 | + * @param string $namespace |
|
| 200 | + * @param string $currentTime |
|
| 201 | + * @return bool |
|
| 202 | + */ |
|
| 203 | 203 | private function hasValidCache(string $cacheKey, string $namespace, string $currentTime): bool |
| 204 | 204 | { |
| 205 | 205 | $stmt = $this->connection->prepare( |
@@ -215,13 +215,13 @@ discard block |
||
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
| 218 | - * Renews the expiration time of a cache item. |
|
| 219 | - * |
|
| 220 | - * @param string $cacheKey |
|
| 221 | - * @param string|int $ttl |
|
| 222 | - * @param string $namespace |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 218 | + * Renews the expiration time of a cache item. |
|
| 219 | + * |
|
| 220 | + * @param string $cacheKey |
|
| 221 | + * @param string|int $ttl |
|
| 222 | + * @param string $namespace |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | 225 | public function renew(string $cacheKey, string|int $ttl, string $namespace = ''): bool |
| 226 | 226 | { |
| 227 | 227 | $currentTime = date('Y-m-d H:i:s'); |
@@ -241,10 +241,10 @@ discard block |
||
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
| 244 | - * Flushes all cache items from the database. |
|
| 245 | - * |
|
| 246 | - * @return bool |
|
| 247 | - */ |
|
| 244 | + * Flushes all cache items from the database. |
|
| 245 | + * |
|
| 246 | + * @return bool |
|
| 247 | + */ |
|
| 248 | 248 | public function flush(): bool |
| 249 | 249 | { |
| 250 | 250 | return $this->connection->exec("DELETE FROM cacheer_table") !== false; |
@@ -263,11 +263,11 @@ discard block |
||
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | /** |
| 266 | - * Gets the current date and time based on the database driver. |
|
| 267 | - * |
|
| 268 | - * @param string $driver |
|
| 269 | - * @return string |
|
| 270 | - */ |
|
| 266 | + * Gets the current date and time based on the database driver. |
|
| 267 | + * |
|
| 268 | + * @param string $driver |
|
| 269 | + * @return string |
|
| 270 | + */ |
|
| 271 | 271 | private function getCurrentDateTime(string $driver): string |
| 272 | 272 | { |
| 273 | 273 | return ($driver === 'sqlite') ? "DATETIME('now', 'localtime')" : "NOW()"; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @param string|int $ttl |
| 38 | 38 | * @return bool |
| 39 | 39 | */ |
| 40 | - public function store(string $cacheKey, mixed $cacheData, string $namespace, string|int $ttl = 3600): bool |
|
| 40 | + public function store(string $cacheKey, mixed $cacheData, string $namespace, string | int $ttl = 3600): bool |
|
| 41 | 41 | { |
| 42 | 42 | if (!empty($this->retrieve($cacheKey, $namespace))) { |
| 43 | 43 | return $this->update($cacheKey, $cacheData, $namespace); |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | * @param string $namespace |
| 223 | 223 | * @return bool |
| 224 | 224 | */ |
| 225 | - public function renew(string $cacheKey, string|int $ttl, string $namespace = ''): bool |
|
| 225 | + public function renew(string $cacheKey, string | int $ttl, string $namespace = ''): bool |
|
| 226 | 226 | { |
| 227 | 227 | $currentTime = date('Y-m-d H:i:s'); |
| 228 | 228 | if (!$this->hasValidCache($cacheKey, $namespace, $currentTime)) { |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | |
| 232 | 232 | $query = $this->getRenewExpirationQueryWithDriver(); |
| 233 | 233 | $stmt = $this->connection->prepare($query); |
| 234 | - $stmt->bindValue(':ttl', (int) $ttl, PDO::PARAM_INT); |
|
| 234 | + $stmt->bindValue(':ttl', (int)$ttl, PDO::PARAM_INT); |
|
| 235 | 235 | $stmt->bindValue(':cacheKey', $cacheKey); |
| 236 | 236 | $stmt->bindValue(':namespace', $namespace); |
| 237 | 237 | $stmt->bindValue(':currentTime', $currentTime); |
@@ -12,13 +12,13 @@ |
||
| 12 | 12 | class OptionBuilder |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Creates a FileOptionBuilder instance for file-based cache options. |
|
| 17 | - * |
|
| 18 | - * @return FileOptionBuilder |
|
| 19 | - */ |
|
| 20 | - public static function forFile(): FileOptionBuilder |
|
| 21 | - { |
|
| 15 | + /** |
|
| 16 | + * Creates a FileOptionBuilder instance for file-based cache options. |
|
| 17 | + * |
|
| 18 | + * @return FileOptionBuilder |
|
| 19 | + */ |
|
| 20 | + public static function forFile(): FileOptionBuilder |
|
| 21 | + { |
|
| 22 | 22 | return new FileOptionBuilder(); |
| 23 | - } |
|
| 23 | + } |
|
| 24 | 24 | } |