@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @param string|int $ttl |
119 | 119 | * @return mixed |
120 | 120 | */ |
121 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
121 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
122 | 122 | { |
123 | 123 | $fullCacheKey = $this->buildKey($cacheKey, $namespace); |
124 | 124 | $cacheData = $this->redis->get($fullCacheKey); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param string|int $ttl |
140 | 140 | * @return array |
141 | 141 | */ |
142 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
142 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
143 | 143 | { |
144 | 144 | $results = []; |
145 | 145 | foreach ($cacheKeys as $cacheKey) { |
@@ -227,12 +227,12 @@ discard block |
||
227 | 227 | * @param string|int|null $ttl |
228 | 228 | * @return mixed |
229 | 229 | */ |
230 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int|null $ttl = null) |
|
230 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int | null $ttl = null) |
|
231 | 231 | { |
232 | 232 | $cacheFullKey = $this->buildKey($cacheKey, $namespace); |
233 | 233 | $serializedData = CacheRedisHelper::serialize($cacheData); |
234 | 234 | |
235 | - $result = $ttl ? $this->redis->setex($cacheFullKey, (int) $ttl, $serializedData) |
|
235 | + $result = $ttl ? $this->redis->setex($cacheFullKey, (int)$ttl, $serializedData) |
|
236 | 236 | : $this->redis->set($cacheFullKey, $serializedData); |
237 | 237 | |
238 | 238 | if ($result) { |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | * @param string $namespace |
270 | 270 | * @return void |
271 | 271 | */ |
272 | - public function renewCache(string $cacheKey, string|int $ttl, string $namespace = '') |
|
272 | + public function renewCache(string $cacheKey, string | int $ttl, string $namespace = '') |
|
273 | 273 | { |
274 | 274 | $cacheFullKey = $this->buildKey($cacheKey, $namespace); |
275 | 275 | $dump = $this->getDump($cacheFullKey); |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | * @param mixed $dump |
298 | 298 | * @return bool |
299 | 299 | */ |
300 | - private function restoreKey(string $fullKey, string|int $ttl, mixed $dump) |
|
300 | + private function restoreKey(string $fullKey, string | int $ttl, mixed $dump) |
|
301 | 301 | { |
302 | 302 | try { |
303 | 303 | $this->redis->restore($fullKey, $ttl * 1000, $dump, 'REPLACE'); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | * @param string|int $ttl |
159 | 159 | * @return string |
160 | 160 | */ |
161 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
161 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
162 | 162 | { |
163 | 163 | |
164 | 164 | $ttl = CacheFileHelper::ttl($ttl, $this->defaultTTL); |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * @param string|int $ttl |
182 | 182 | * @return array |
183 | 183 | */ |
184 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
184 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
185 | 185 | { |
186 | 186 | $ttl = CacheFileHelper::ttl($ttl, $this->defaultTTL); |
187 | 187 | $results = []; |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | * @param string|int $ttl |
224 | 224 | * @return void |
225 | 225 | */ |
226 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
|
226 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600) |
|
227 | 227 | { |
228 | 228 | $cacheFile = $this->buildCacheFilePath($cacheKey, $namespace); |
229 | 229 | $data = $this->fileManager->serialize($cacheData); |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | * @param string $namespace |
257 | 257 | * @return void |
258 | 258 | */ |
259 | - public function renewCache(string $cacheKey, string|int $ttl, string $namespace = '') |
|
259 | + public function renewCache(string $cacheKey, string | int $ttl, string $namespace = '') |
|
260 | 260 | { |
261 | 261 | $cacheData = $this->getCache($cacheKey, $namespace); |
262 | 262 | if ($cacheData) { |
@@ -346,12 +346,12 @@ discard block |
||
346 | 346 | { |
347 | 347 | $flushAfterSeconds = CacheFileHelper::convertExpirationToSeconds($flushAfter); |
348 | 348 | |
349 | - if(!$this->fileManager->fileExists($this->lastFlushTimeFile)) { |
|
349 | + if (!$this->fileManager->fileExists($this->lastFlushTimeFile)) { |
|
350 | 350 | $this->fileManager->writeFile($this->lastFlushTimeFile, time()); |
351 | 351 | return; |
352 | 352 | } |
353 | 353 | |
354 | - $lastFlushTime = (int) $this->fileManager->readFile($this->lastFlushTimeFile); |
|
354 | + $lastFlushTime = (int)$this->fileManager->readFile($this->lastFlushTimeFile); |
|
355 | 355 | |
356 | 356 | if ((time() - $lastFlushTime) >= $flushAfterSeconds) { |
357 | 357 | $this->flushCache(); |
@@ -13,124 +13,124 @@ 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 | - * @param boolean |
|
23 | - */ |
|
24 | - private bool $success = false; |
|
25 | - |
|
26 | - /** |
|
27 | - * @param string |
|
28 | - */ |
|
29 | - private string $message = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var CacheLogger |
|
33 | - */ |
|
34 | - private $logger = null; |
|
35 | - |
|
36 | - public function __construct(string $logPath) |
|
37 | - { |
|
16 | + /** |
|
17 | + * @param array $arrayStore |
|
18 | + */ |
|
19 | + private array $arrayStore = []; |
|
20 | + |
|
21 | + /** |
|
22 | + * @param boolean |
|
23 | + */ |
|
24 | + private bool $success = false; |
|
25 | + |
|
26 | + /** |
|
27 | + * @param string |
|
28 | + */ |
|
29 | + private string $message = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var CacheLogger |
|
33 | + */ |
|
34 | + private $logger = null; |
|
35 | + |
|
36 | + public function __construct(string $logPath) |
|
37 | + { |
|
38 | 38 | $this->logger = new CacheLogger($logPath); |
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @param string $cacheKey |
|
43 | - * @param mixed $cacheData |
|
44 | - * @param string $namespace |
|
45 | - * @return bool |
|
46 | - */ |
|
47 | - public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
|
48 | - { |
|
49 | - $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
50 | - |
|
51 | - if (!$this->has($cacheKey, $namespace)) { |
|
52 | - $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
53 | - $this->logger->debug("{$this->getMessage()} from array driver."); |
|
54 | - return false; |
|
55 | - } |
|
56 | - |
|
57 | - $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
58 | - $this->setMessage("Cache appended successfully", true); |
|
59 | - return true; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @param string $cacheKey |
|
64 | - * @param string $namespace |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - private function buildArrayKey(string $cacheKey, string $namespace = '') |
|
68 | - { |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @param string $cacheKey |
|
43 | + * @param mixed $cacheData |
|
44 | + * @param string $namespace |
|
45 | + * @return bool |
|
46 | + */ |
|
47 | + public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
|
48 | + { |
|
49 | + $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
50 | + |
|
51 | + if (!$this->has($cacheKey, $namespace)) { |
|
52 | + $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
53 | + $this->logger->debug("{$this->getMessage()} from array driver."); |
|
54 | + return false; |
|
55 | + } |
|
56 | + |
|
57 | + $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
58 | + $this->setMessage("Cache appended successfully", true); |
|
59 | + return true; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @param string $cacheKey |
|
64 | + * @param string $namespace |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + private function buildArrayKey(string $cacheKey, string $namespace = '') |
|
68 | + { |
|
69 | 69 | return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey; |
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @param string $cacheKey |
|
74 | - * @param string $namespace |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - public function clearCache(string $cacheKey, string $namespace = '') |
|
78 | - { |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @param string $cacheKey |
|
74 | + * @param string $namespace |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + public function clearCache(string $cacheKey, string $namespace = '') |
|
78 | + { |
|
79 | 79 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
80 | 80 | unset($this->arrayStore[$arrayStoreKey]); |
81 | 81 | $this->setMessage("Cache cleared successfully", true); |
82 | 82 | $this->logger->debug("{$this->getMessage()} from array driver."); |
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string $cacheKey |
|
87 | - * @param int $amount |
|
88 | - * @param string $namespace |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
92 | - { |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string $cacheKey |
|
87 | + * @param int $amount |
|
88 | + * @param string $namespace |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
92 | + { |
|
93 | 93 | return $this->increment($cacheKey, ($amount * -1), $namespace); |
94 | - } |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function flushCache() |
|
100 | - { |
|
96 | + /** |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function flushCache() |
|
100 | + { |
|
101 | 101 | unset($this->arrayStore); |
102 | 102 | $this->arrayStore = []; |
103 | 103 | $this->setMessage("Cache flushed successfully", true); |
104 | 104 | $this->logger->debug("{$this->getMessage()} from array driver."); |
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param string $cacheKey |
|
109 | - * @param mixed $cacheData |
|
110 | - * @param string $namespace |
|
111 | - * @param int|string $ttl |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - public function forever(string $cacheKey, mixed $cacheData) |
|
115 | - { |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param string $cacheKey |
|
109 | + * @param mixed $cacheData |
|
110 | + * @param string $namespace |
|
111 | + * @param int|string $ttl |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + public function forever(string $cacheKey, mixed $cacheData) |
|
115 | + { |
|
116 | 116 | $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
117 | 117 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param string $cacheKey |
|
122 | - * @param string $namespace |
|
123 | - * @param int|string $ttl |
|
124 | - * @return mixed |
|
125 | - */ |
|
126 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
127 | - { |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param string $cacheKey |
|
122 | + * @param string $namespace |
|
123 | + * @param int|string $ttl |
|
124 | + * @return mixed |
|
125 | + */ |
|
126 | + public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
127 | + { |
|
128 | 128 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
129 | 129 | |
130 | 130 | if (!$this->has($cacheKey, $namespace)) { |
131 | - $this->setMessage("cacheData not found, does not exists or expired", false); |
|
132 | - $this->logger->debug("{$this->getMessage()} from array driver."); |
|
133 | - return false; |
|
131 | + $this->setMessage("cacheData not found, does not exists or expired", false); |
|
132 | + $this->logger->debug("{$this->getMessage()} from array driver."); |
|
133 | + return false; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | $cacheData = $this->arrayStore[$arrayStoreKey]; |
@@ -138,127 +138,127 @@ discard block |
||
138 | 138 | $now = time(); |
139 | 139 | |
140 | 140 | if($expirationTime !== 0 && $now >= $expirationTime) { |
141 | - list($np, $key) = explode(':', $arrayStoreKey); |
|
142 | - $this->clearCache($key, $np); |
|
143 | - $this->setMessage("cacheKey: {$key} has expired.", false); |
|
144 | - $this->logger->debug("{$this->getMessage()} from array driver."); |
|
145 | - return false; |
|
141 | + list($np, $key) = explode(':', $arrayStoreKey); |
|
142 | + $this->clearCache($key, $np); |
|
143 | + $this->setMessage("cacheKey: {$key} has expired.", false); |
|
144 | + $this->logger->debug("{$this->getMessage()} from array driver."); |
|
145 | + return false; |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | $this->setMessage("Cache retrieved successfully", true); |
149 | 149 | $this->logger->debug("{$this->getMessage()} from array driver."); |
150 | 150 | return $this->serialize($cacheData['cacheData'], false); |
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param array $cacheKeys |
|
155 | - * @param string $namespace |
|
156 | - * @param string|int $ttl |
|
157 | - * @return array |
|
158 | - */ |
|
159 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
160 | - { |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param array $cacheKeys |
|
155 | + * @param string $namespace |
|
156 | + * @param string|int $ttl |
|
157 | + * @return array |
|
158 | + */ |
|
159 | + public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
160 | + { |
|
161 | 161 | $results = []; |
162 | 162 | foreach ($cacheKeys as $cacheKey) { |
163 | - $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
163 | + $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
164 | 164 | } |
165 | 165 | return $results; |
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param string $cacheKey |
|
170 | - * @param string $namespace |
|
171 | - * @return bool |
|
172 | - */ |
|
173 | - public function has(string $cacheKey, string $namespace = '') |
|
174 | - { |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param string $cacheKey |
|
170 | + * @param string $namespace |
|
171 | + * @return bool |
|
172 | + */ |
|
173 | + public function has(string $cacheKey, string $namespace = '') |
|
174 | + { |
|
175 | 175 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
176 | 176 | return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime']; |
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @param string $cacheKey |
|
181 | - * @param int $amount |
|
182 | - * @param string $namespace |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
186 | - { |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @param string $cacheKey |
|
181 | + * @param int $amount |
|
182 | + * @param string $namespace |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
186 | + { |
|
187 | 187 | $cacheData = $this->getCache($cacheKey, $namespace); |
188 | 188 | |
189 | 189 | if(!empty($cacheData) && is_numeric($cacheData)) { |
190 | - $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
191 | - $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
192 | - return true; |
|
190 | + $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
191 | + $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
192 | + return true; |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | return false; |
196 | - } |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * @return boolean |
|
200 | - */ |
|
201 | - public function isSuccess() |
|
202 | - { |
|
198 | + /** |
|
199 | + * @return boolean |
|
200 | + */ |
|
201 | + public function isSuccess() |
|
202 | + { |
|
203 | 203 | return $this->success; |
204 | - } |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function getMessage() |
|
210 | - { |
|
206 | + /** |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function getMessage() |
|
210 | + { |
|
211 | 211 | return $this->message; |
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @param string $cacheKey |
|
216 | - * @param mixed $cacheData |
|
217 | - * @param string $namespace |
|
218 | - * @param int|string $ttl |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
222 | - { |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @param string $cacheKey |
|
216 | + * @param mixed $cacheData |
|
217 | + * @param string $namespace |
|
218 | + * @param int|string $ttl |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
222 | + { |
|
223 | 223 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
224 | 224 | |
225 | 225 | $this->arrayStore[$arrayStoreKey] = [ |
226 | - 'cacheData' => serialize($cacheData), |
|
227 | - 'expirationTime' => time() + $ttl |
|
226 | + 'cacheData' => serialize($cacheData), |
|
227 | + 'expirationTime' => time() + $ttl |
|
228 | 228 | ]; |
229 | 229 | |
230 | 230 | $this->setMessage("Cache stored successfully", true); |
231 | 231 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
232 | 232 | return true; |
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param array $items |
|
237 | - * @param string $namespace |
|
238 | - * @param int $batchSize |
|
239 | - * @return void |
|
240 | - */ |
|
241 | - public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
|
242 | - { |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param array $items |
|
237 | + * @param string $namespace |
|
238 | + * @param int $batchSize |
|
239 | + * @return void |
|
240 | + */ |
|
241 | + public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
|
242 | + { |
|
243 | 243 | $chunks = array_chunk($items, $batchSize, true); |
244 | 244 | |
245 | 245 | foreach ($chunks as $chunk) { |
246 | - foreach ($chunk as $key => $data) { |
|
247 | - $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
246 | + foreach ($chunk as $key => $data) { |
|
247 | + $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
248 | + } |
|
248 | 249 | } |
249 | - } |
|
250 | 250 | $this->setMessage("{$this->getMessage()}", $this->isSuccess()); |
251 | 251 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * @param string $cacheKey |
|
256 | - * @param string|int $ttl |
|
257 | - * @param string $namespace |
|
258 | - * @return void |
|
259 | - */ |
|
260 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
261 | - { |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * @param string $cacheKey |
|
256 | + * @param string|int $ttl |
|
257 | + * @param string $namespace |
|
258 | + * @return void |
|
259 | + */ |
|
260 | + public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
261 | + { |
|
262 | 262 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
263 | 263 | |
264 | 264 | if (isset($this->arrayStore[$arrayStoreKey])) { |
@@ -266,27 +266,27 @@ discard block |
||
266 | 266 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
267 | 267 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
268 | 268 | $this->logger->debug("{$this->getMessage()} from array driver."); |
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @param string $message |
|
274 | - * @param boolean $success |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - private function setMessage(string $message, bool $success) |
|
278 | - { |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @param string $message |
|
274 | + * @param boolean $success |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + private function setMessage(string $message, bool $success) |
|
278 | + { |
|
279 | 279 | $this->message = $message; |
280 | 280 | $this->success = $success; |
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param mixed $data |
|
285 | - * @param bool $serialize |
|
286 | - * @return mixed |
|
287 | - */ |
|
288 | - private function serialize(mixed $data, bool $serialize = true) |
|
289 | - { |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param mixed $data |
|
285 | + * @param bool $serialize |
|
286 | + * @return mixed |
|
287 | + */ |
|
288 | + private function serialize(mixed $data, bool $serialize = true) |
|
289 | + { |
|
290 | 290 | return $serialize ? serialize($data) : unserialize($data); |
291 | - } |
|
291 | + } |
|
292 | 292 | } |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * @param int|string $ttl |
124 | 124 | * @return mixed |
125 | 125 | */ |
126 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
126 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
127 | 127 | { |
128 | 128 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
129 | 129 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | $expirationTime = $cacheData['expirationTime'] ?? 0; |
138 | 138 | $now = time(); |
139 | 139 | |
140 | - if($expirationTime !== 0 && $now >= $expirationTime) { |
|
140 | + if ($expirationTime !== 0 && $now >= $expirationTime) { |
|
141 | 141 | list($np, $key) = explode(':', $arrayStoreKey); |
142 | 142 | $this->clearCache($key, $np); |
143 | 143 | $this->setMessage("cacheKey: {$key} has expired.", false); |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | * @param string|int $ttl |
157 | 157 | * @return array |
158 | 158 | */ |
159 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
159 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
160 | 160 | { |
161 | 161 | $results = []; |
162 | 162 | foreach ($cacheKeys as $cacheKey) { |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | { |
187 | 187 | $cacheData = $this->getCache($cacheKey, $namespace); |
188 | 188 | |
189 | - if(!empty($cacheData) && is_numeric($cacheData)) { |
|
189 | + if (!empty($cacheData) && is_numeric($cacheData)) { |
|
190 | 190 | $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
191 | 191 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
192 | 192 | return true; |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | * @param int|string $ttl |
219 | 219 | * @return bool |
220 | 220 | */ |
221 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
221 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600) |
|
222 | 222 | { |
223 | 223 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
224 | 224 | |
@@ -257,12 +257,12 @@ discard block |
||
257 | 257 | * @param string $namespace |
258 | 258 | * @return void |
259 | 259 | */ |
260 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
260 | + public function renewCache(string $cacheKey, int | string $ttl = 3600, string $namespace = '') |
|
261 | 261 | { |
262 | 262 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
263 | 263 | |
264 | 264 | if (isset($this->arrayStore[$arrayStoreKey])) { |
265 | - $ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time(); |
|
265 | + $ttlSeconds = is_numeric($ttl) ? (int)$ttl : strtotime($ttl) - time(); |
|
266 | 266 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
267 | 267 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
268 | 268 | $this->logger->debug("{$this->getMessage()} from array driver."); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | public function clearCache(string $cacheKey, string $namespace = '') |
69 | 69 | { |
70 | 70 | $data = $this->cacheRepository->clear($cacheKey, $namespace); |
71 | - if($data) { |
|
71 | + if ($data) { |
|
72 | 72 | $this->setMessage("Cache deleted successfully!", true); |
73 | 73 | } else { |
74 | 74 | $this->setMessage("Cache does not exists!", false); |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function flushCache() |
84 | 84 | { |
85 | - if($this->cacheRepository->flush()){ |
|
85 | + if ($this->cacheRepository->flush()) { |
|
86 | 86 | $this->setMessage("Flush finished successfully", true); |
87 | 87 | } else { |
88 | 88 | $this->setMessage("Something went wrong. Please, try again.", false); |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * @param string|int $ttl |
99 | 99 | * @return mixed |
100 | 100 | */ |
101 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
101 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
102 | 102 | { |
103 | 103 | $cacheData = $this->retrieveCache($cacheKey, $namespace); |
104 | 104 | if ($cacheData) { |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * @param string|int $ttl |
118 | 118 | * @return array |
119 | 119 | */ |
120 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
120 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
121 | 121 | { |
122 | 122 | $cacheData = []; |
123 | 123 | foreach ($cacheKeys as $cacheKey) { |
@@ -190,9 +190,9 @@ discard block |
||
190 | 190 | * @param string|int $ttl |
191 | 191 | * @return bool |
192 | 192 | */ |
193 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
|
193 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600) |
|
194 | 194 | { |
195 | - if($this->storeCache($cacheKey, $cacheData, $namespace, $ttl)){ |
|
195 | + if ($this->storeCache($cacheKey, $cacheData, $namespace, $ttl)) { |
|
196 | 196 | $this->logger->debug("{$this->getMessage()} from database driver."); |
197 | 197 | return true; |
198 | 198 | } |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | */ |
224 | 224 | private function processBatchItems(array $batchItems, string $namespace) |
225 | 225 | { |
226 | - foreach($batchItems as $item) { |
|
226 | + foreach ($batchItems as $item) { |
|
227 | 227 | CacheDatabaseHelper::validateCacheItem($item); |
228 | 228 | $cacheKey = $item['cacheKey']; |
229 | 229 | $cacheData = $item['cacheData']; |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * @param string $namespace |
239 | 239 | * @return bool |
240 | 240 | */ |
241 | - private function renew(string $cacheKey, string|int $ttl = 3600, string $namespace = '') |
|
241 | + private function renew(string $cacheKey, string | int $ttl = 3600, string $namespace = '') |
|
242 | 242 | { |
243 | 243 | $cacheData = $this->getCache($cacheKey, $namespace); |
244 | 244 | if ($cacheData) { |
@@ -281,10 +281,10 @@ discard block |
||
281 | 281 | * @param integer $ttl |
282 | 282 | * @return bool |
283 | 283 | */ |
284 | - private function storeCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
|
284 | + private function storeCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600) |
|
285 | 285 | { |
286 | 286 | $data = $this->cacheRepository->store($cacheKey, $cacheData, $namespace, $ttl); |
287 | - if($data) { |
|
287 | + if ($data) { |
|
288 | 288 | $this->setMessage("Cache Stored Successfully", true); |
289 | 289 | return true; |
290 | 290 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | private function updateCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
302 | 302 | { |
303 | 303 | $data = $this->cacheRepository->update($cacheKey, $cacheData, $namespace); |
304 | - if($data) { |
|
304 | + if ($data) { |
|
305 | 305 | $this->setMessage("Cache updated successfully.", true); |
306 | 306 | return true; |
307 | 307 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * @param int|string $ttl |
71 | 71 | * @return bool |
72 | 72 | */ |
73 | - public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
73 | + public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600) |
|
74 | 74 | { |
75 | 75 | if (!empty($this->getCache($cacheKey, $namespace))) { |
76 | 76 | return true; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * @param string|int $ttl |
161 | 161 | * @return CacheDataFormatter|mixed |
162 | 162 | */ |
163 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
163 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
164 | 164 | { |
165 | 165 | $cacheData = $this->cacheStore->getCache($cacheKey, $namespace, $ttl); |
166 | 166 | $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | * @param string|int $ttl |
179 | 179 | * @return CacheDataFormatter|array |
180 | 180 | */ |
181 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
181 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
182 | 182 | { |
183 | 183 | $cachedData = $this->cacheStore->getMany($cacheKeys, $namespace, $ttl); |
184 | 184 | $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | { |
214 | 214 | $cacheData = $this->getCache($cacheKey, $namespace); |
215 | 215 | |
216 | - if(!empty($cacheData) && is_numeric($cacheData)) { |
|
216 | + if (!empty($cacheData) && is_numeric($cacheData)) { |
|
217 | 217 | $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
218 | 218 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
219 | 219 | return true; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * @param string|int $ttl |
238 | 238 | * @return void |
239 | 239 | */ |
240 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
|
240 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string | int $ttl = 3600) |
|
241 | 241 | { |
242 | 242 | $data = CacheerHelper::prepareForStorage($cacheData, $this->compression, $this->encryptionKey); |
243 | 243 | $this->cacheStore->putCache($cacheKey, $data, $namespace, $ttl); |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | * @param string $namespace |
262 | 262 | * @return void |
263 | 263 | */ |
264 | - public function renewCache(string $cacheKey, string|int $ttl = 3600, string $namespace = '') |
|
264 | + public function renewCache(string $cacheKey, string | int $ttl = 3600, string $namespace = '') |
|
265 | 265 | { |
266 | 266 | $this->cacheStore->renewCache($cacheKey, $ttl, $namespace); |
267 | 267 | |
@@ -278,11 +278,11 @@ discard block |
||
278 | 278 | * @param Closure $callback |
279 | 279 | * @return mixed |
280 | 280 | */ |
281 | - public function remember(string $cacheKey, int|string $ttl, Closure $callback) |
|
281 | + public function remember(string $cacheKey, int | string $ttl, Closure $callback) |
|
282 | 282 | { |
283 | 283 | $cachedData = $this->getCache($cacheKey, ttl: $ttl); |
284 | 284 | |
285 | - if(!empty($cachedData)) { |
|
285 | + if (!empty($cachedData)) { |
|
286 | 286 | return $cachedData; |
287 | 287 | } |
288 | 288 |
@@ -7,27 +7,27 @@ discard block |
||
7 | 7 | class RedisCacheStoreTest extends TestCase |
8 | 8 | { |
9 | 9 | |
10 | - /** @var Cacheer */ |
|
11 | - private $cache; |
|
10 | + /** @var Cacheer */ |
|
11 | + private $cache; |
|
12 | 12 | |
13 | - protected function setUp(): void |
|
14 | - { |
|
13 | + protected function setUp(): void |
|
14 | + { |
|
15 | 15 | $this->cache = new Cacheer(); |
16 | 16 | $this->cache->setDriver()->useRedisDriver(); |
17 | - } |
|
17 | + } |
|
18 | 18 | |
19 | - protected function tearDown(): void |
|
20 | - { |
|
19 | + protected function tearDown(): void |
|
20 | + { |
|
21 | 21 | $this->cache->flushCache(); |
22 | - } |
|
22 | + } |
|
23 | 23 | |
24 | - public function testUsingRedisDriverSetsProperInstance() |
|
25 | - { |
|
24 | + public function testUsingRedisDriverSetsProperInstance() |
|
25 | + { |
|
26 | 26 | $this->assertInstanceOf(RedisCacheStore::class, $this->cache->cacheStore); |
27 | - } |
|
27 | + } |
|
28 | 28 | |
29 | - public function testPutCacheInRedis() |
|
30 | - { |
|
29 | + public function testPutCacheInRedis() |
|
30 | + { |
|
31 | 31 | $cacheKey = 'test_key'; |
32 | 32 | $cacheData = ['name' => 'Sílvio Silva', 'role' => 'Developer']; |
33 | 33 | |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
38 | 38 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
39 | 39 | |
40 | - } |
|
40 | + } |
|
41 | 41 | |
42 | - public function testGetCacheFromRedis() |
|
43 | - { |
|
42 | + public function testGetCacheFromRedis() |
|
43 | + { |
|
44 | 44 | $cacheKey = 'test_key'; |
45 | 45 | $cacheData = ['name' => 'Sílvio Silva', 'role' => 'Developer']; |
46 | 46 | |
@@ -51,10 +51,10 @@ discard block |
||
51 | 51 | $data = $this->cache->getCache($cacheKey); |
52 | 52 | $this->assertNotEmpty($data); |
53 | 53 | $this->assertEquals($cacheData, $data); |
54 | - } |
|
54 | + } |
|
55 | 55 | |
56 | - public function testExpiredCacheInRedis() |
|
57 | - { |
|
56 | + public function testExpiredCacheInRedis() |
|
57 | + { |
|
58 | 58 | $cacheKey = 'expired_key'; |
59 | 59 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
60 | 60 | |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
65 | 65 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
66 | 66 | $this->assertFalse($this->cache->isSuccess()); |
67 | - } |
|
67 | + } |
|
68 | 68 | |
69 | - public function testOverwriteExistingCacheInRedis() |
|
70 | - { |
|
69 | + public function testOverwriteExistingCacheInRedis() |
|
70 | + { |
|
71 | 71 | $cacheKey = 'overwrite_key'; |
72 | 72 | $initialCacheData = ['name' => 'Initial Data', 'email' => '[email protected]']; |
73 | 73 | $newCacheData = ['name' => 'New Data', 'email' => '[email protected]']; |
@@ -78,11 +78,11 @@ discard block |
||
78 | 78 | $this->cache->appendCache($cacheKey, $newCacheData); |
79 | 79 | $this->assertEquals("Cache appended successfully", $this->cache->getMessage()); |
80 | 80 | $this->assertEquals($newCacheData, $this->cache->getCache($cacheKey)); |
81 | - } |
|
81 | + } |
|
82 | 82 | |
83 | - public function testPutManyCacheItemsInRedis() |
|
84 | - { |
|
85 | - $items = [ |
|
83 | + public function testPutManyCacheItemsInRedis() |
|
84 | + { |
|
85 | + $items = [ |
|
86 | 86 | [ |
87 | 87 | 'cacheKey' => 'user_1_profile', |
88 | 88 | 'cacheData' => [ |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | foreach ($items as $item) { |
108 | 108 | $this->assertEquals($item['cacheData'], $this->cache->getCache($item['cacheKey'])); |
109 | 109 | } |
110 | - } |
|
110 | + } |
|
111 | 111 | |
112 | - public function testAppendCacheWithNamespaceInRedis() |
|
112 | + public function testAppendCacheWithNamespaceInRedis() |
|
113 | 113 | { |
114 | 114 | $cacheKey = 'test_append_key_ns'; |
115 | 115 | $namespace = 'test_namespace'; |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | $this->assertEquals($expectedData, $cachedData); |
133 | 133 | } |
134 | 134 | |
135 | - public function testDataOutputShouldBeOfTypeArray() |
|
135 | + public function testDataOutputShouldBeOfTypeArray() |
|
136 | 136 | { |
137 | 137 | |
138 | 138 | $this->cache->useFormatter(); |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | $this->assertJson($cacheOutput); |
179 | 179 | } |
180 | 180 | |
181 | - public function testClearCacheDataFromRedis() |
|
181 | + public function testClearCacheDataFromRedis() |
|
182 | 182 | { |
183 | 183 | $cacheKey = 'test_key'; |
184 | 184 | $data = 'test_data'; |
@@ -193,8 +193,8 @@ discard block |
||
193 | 193 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
194 | 194 | } |
195 | 195 | |
196 | - public function testFlushCacheDataFromRedis() |
|
197 | - { |
|
196 | + public function testFlushCacheDataFromRedis() |
|
197 | + { |
|
198 | 198 | $key1 = 'test_key1'; |
199 | 199 | $data1 = 'test_data1'; |
200 | 200 | |
@@ -212,8 +212,8 @@ discard block |
||
212 | 212 | $this->assertEquals("Cache flushed successfully", $this->cache->getMessage()); |
213 | 213 | } |
214 | 214 | |
215 | - public function testHasCacheFromRedis() |
|
216 | - { |
|
215 | + public function testHasCacheFromRedis() |
|
216 | + { |
|
217 | 217 | $cacheKey = 'test_key'; |
218 | 218 | $cacheData = ['name' => 'Sílvio Silva', 'role' => 'Developer']; |
219 | 219 | |
@@ -221,10 +221,10 @@ discard block |
||
221 | 221 | |
222 | 222 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
223 | 223 | $this->assertTrue($this->cache->isSuccess()); |
224 | - } |
|
224 | + } |
|
225 | 225 | |
226 | - public function testRenewCacheFromRedis() |
|
227 | - { |
|
226 | + public function testRenewCacheFromRedis() |
|
227 | + { |
|
228 | 228 | $cacheKey = 'expired_key'; |
229 | 229 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
230 | 230 | |
@@ -239,10 +239,10 @@ discard block |
||
239 | 239 | $this->cache->renewCache($cacheKey, 7200); |
240 | 240 | $this->assertTrue($this->cache->isSuccess()); |
241 | 241 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
242 | - } |
|
242 | + } |
|
243 | 243 | |
244 | 244 | public function testRenewCacheWithNamespaceFromRedis() |
245 | - { |
|
245 | + { |
|
246 | 246 | $cacheKey = 'expired_key'; |
247 | 247 | $namespace = 'expired_namespace'; |
248 | 248 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
@@ -256,9 +256,9 @@ discard block |
||
256 | 256 | $this->cache->renewCache($cacheKey, 7200, $namespace); |
257 | 257 | $this->assertTrue($this->cache->isSuccess()); |
258 | 258 | $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace)); |
259 | - } |
|
259 | + } |
|
260 | 260 | |
261 | - public function test_remember_saves_and_recover_values() |
|
261 | + public function test_remember_saves_and_recover_values() |
|
262 | 262 | { |
263 | 263 | $this->cache->flushCache(); |
264 | 264 | |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | } |
294 | 294 | |
295 | 295 | |
296 | - public function test_get_and_forget() |
|
296 | + public function test_get_and_forget() |
|
297 | 297 | { |
298 | 298 | $cacheKey = 'cache_key_test'; |
299 | 299 | $this->cache->putCache($cacheKey, 10); |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | $this->assertNull($noCacheData); |
315 | 315 | } |
316 | 316 | |
317 | - public function test_store_if_not_present_with_add_function() |
|
317 | + public function test_store_if_not_present_with_add_function() |
|
318 | 318 | { |
319 | 319 | $existentKey = 'cache_key_test'; |
320 | 320 | |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | |
339 | 339 | } |
340 | 340 | |
341 | - public function test_increment_function() { |
|
341 | + public function test_increment_function() { |
|
342 | 342 | |
343 | 343 | $cacheKey = 'test_increment'; |
344 | 344 | $cacheData = 2025; |