@@ -53,23 +53,23 @@ |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
56 | - * Converts the exception to a JSON serializable format. |
|
57 | - * |
|
58 | - * @return string |
|
59 | - */ |
|
56 | + * Converts the exception to a JSON serializable format. |
|
57 | + * |
|
58 | + * @return string |
|
59 | + */ |
|
60 | 60 | public function jsonSerialize(): array |
61 | 61 | { |
62 | 62 | return parent::jsonSerialize(); |
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | - * Converts the exception to a JSON string. |
|
67 | - * |
|
68 | - * @param int $options |
|
69 | - * @return string |
|
70 | - */ |
|
66 | + * Converts the exception to a JSON string. |
|
67 | + * |
|
68 | + * @param int $options |
|
69 | + * @return string |
|
70 | + */ |
|
71 | 71 | public function toJson(int $options = 0) |
72 | 72 | { |
73 | - return parent::toJson($options); |
|
73 | + return parent::toJson($options); |
|
74 | 74 | } |
75 | 75 | } |
@@ -43,18 +43,18 @@ |
||
43 | 43 | |
44 | 44 | |
45 | 45 | /** |
46 | - * @var CacheLogger |
|
47 | - */ |
|
46 | + * @var CacheLogger |
|
47 | + */ |
|
48 | 48 | private $logger = null; |
49 | 49 | |
50 | 50 | /** |
51 | - * @var FileCacheManager |
|
52 | - */ |
|
51 | + * @var FileCacheManager |
|
52 | + */ |
|
53 | 53 | private FileCacheManager $fileManager; |
54 | 54 | |
55 | 55 | /** |
56 | - * @var FileCacheFlusher |
|
57 | - */ |
|
56 | + * @var FileCacheFlusher |
|
57 | + */ |
|
58 | 58 | private FileCacheFlusher $flusher; |
59 | 59 | |
60 | 60 |
@@ -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) { |
@@ -52,7 +52,7 @@ |
||
52 | 52 | return; |
53 | 53 | } |
54 | 54 | |
55 | - $lastFlushTime = (int) $this->fileManager->readFile($this->lastFlushTimeFile); |
|
55 | + $lastFlushTime = (int)$this->fileManager->readFile($this->lastFlushTimeFile); |
|
56 | 56 | |
57 | 57 | if ((time() - $lastFlushTime) >= $flushAfterSeconds) { |
58 | 58 | $this->flushCache(); |
@@ -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'])) { |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
53 | 53 | * @return mixed Returns the cached data or null if not found |
54 | 54 | */ |
55 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600); |
|
55 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * Retrieves multiple cache items by their keys. |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
63 | 63 | * @return CacheDataFormatter|mixed Returns a formatter with the retrieved items |
64 | 64 | */ |
65 | - public function getMany(array $cacheKeys, string $namespace, string|int $ttl = 3600); |
|
65 | + public function getMany(array $cacheKeys, string $namespace, string | int $ttl = 3600); |
|
66 | 66 | |
67 | 67 | /** |
68 | 68 | * Checks if a cache item exists. |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
83 | 83 | * @return bool True on success, false on failure |
84 | 84 | */ |
85 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600); |
|
85 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600); |
|
86 | 86 | |
87 | 87 | /** |
88 | 88 | * Stores multiple items in the cache. |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | public function test_remember_saves_and_recover_values() |
93 | 93 | { |
94 | 94 | $this->cache->flushCache(); |
95 | - $value = $this->cache->remember('remember_test_key', 60, function () { |
|
95 | + $value = $this->cache->remember('remember_test_key', 60, function() { |
|
96 | 96 | return 'valor_teste'; |
97 | 97 | }); |
98 | 98 | $this->assertEquals('valor_teste', $value); |
99 | - $cachedValue = $this->cache->remember('remember_test_key', 60, function(){ |
|
99 | + $cachedValue = $this->cache->remember('remember_test_key', 60, function() { |
|
100 | 100 | return 'novo_valor'; |
101 | 101 | }); |
102 | 102 | $this->assertEquals('valor_teste', $cachedValue); |
@@ -105,11 +105,11 @@ discard block |
||
105 | 105 | public function test_remember_forever_saves_value_indefinitely() |
106 | 106 | { |
107 | 107 | $this->cache->flushCache(); |
108 | - $value = $this->cache->rememberForever('remember_forever_key', function () { |
|
108 | + $value = $this->cache->rememberForever('remember_forever_key', function() { |
|
109 | 109 | return 'valor_eterno'; |
110 | 110 | }); |
111 | 111 | $this->assertEquals('valor_eterno', $value); |
112 | - $cachedValue = $this->cache->rememberForever('remember_forever_key', function () { |
|
112 | + $cachedValue = $this->cache->rememberForever('remember_forever_key', function() { |
|
113 | 113 | return 'novo_valor'; |
114 | 114 | }); |
115 | 115 | $this->assertEquals('valor_eterno', $cachedValue); |
@@ -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'); |
@@ -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); |