1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Silviooosilva\CacheerPhp\Interface\CacheerInterface; |
7
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore; |
8
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore; |
9
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore; |
10
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore; |
11
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheConfig; |
12
|
|
|
use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter; |
13
|
|
|
use Silviooosilva\CacheerPhp\Utils\CacheDriver; |
14
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CacheerPHP |
19
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
20
|
|
|
* @package Silviooosilva\CacheerPhp |
21
|
|
|
*/ |
22
|
|
|
final class Cacheer implements CacheerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private string $message; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var boolean |
31
|
|
|
*/ |
32
|
|
|
private bool $success; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
private bool $formatted = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private bool $compression = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
private ?string $encryptionKey = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore |
51
|
|
|
*/ |
52
|
|
|
public $cacheStore; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
public array $options = []; |
58
|
|
|
|
59
|
|
|
public function __construct(array $options = [], $formatted = false) |
60
|
|
|
{ |
61
|
|
|
$this->formatted = $formatted; |
62
|
|
|
$this->validateOptions($options); |
63
|
|
|
$this->setDriver()->useDefaultDriver(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $cacheKey |
68
|
|
|
* @param mixed $cacheData |
69
|
|
|
* @param string $namespace |
70
|
|
|
* @param int|string $ttl |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
74
|
|
|
{ |
75
|
|
|
if (!empty($this->getCache($cacheKey, $namespace))) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->putCache($cacheKey, $cacheData, $namespace, $ttl); |
80
|
|
|
$this->setMessage($this->getMessage(), $this->isSuccess()); |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $cacheKey |
87
|
|
|
* @param mixed $cacheData |
88
|
|
|
* @param string $namespace |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
92
|
|
|
{ |
93
|
|
|
$this->cacheStore->appendCache($cacheKey, $cacheData, $namespace); |
94
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $cacheKey |
99
|
|
|
* @param string $namespace |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function clearCache(string $cacheKey, string $namespace = '') |
103
|
|
|
{ |
104
|
|
|
$this->cacheStore->clearCache($cacheKey, $namespace); |
105
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $cacheKey |
110
|
|
|
* @param int $amount |
111
|
|
|
* @param string $namespace |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
115
|
|
|
{ |
116
|
|
|
return $this->increment($cacheKey, ($amount * -1), $namespace); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $cacheKey |
121
|
|
|
* @param mixed $cacheData |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function forever(string $cacheKey, mixed $cacheData) |
125
|
|
|
{ |
126
|
|
|
$this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
127
|
|
|
$this->setMessage($this->getMessage(), $this->isSuccess()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function flushCache() |
134
|
|
|
{ |
135
|
|
|
$this->cacheStore->flushCache(); |
136
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $cacheKey |
141
|
|
|
* @param string $namespace |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function getAndForget(string $cacheKey, string $namespace = '') |
145
|
|
|
{ |
146
|
|
|
$cachedData = $this->getCache($cacheKey, $namespace); |
147
|
|
|
|
148
|
|
|
if (!empty($cachedData)) { |
149
|
|
|
$this->setMessage("Cache retrieved and deleted successfully!", true); |
150
|
|
|
$this->clearCache($cacheKey, $namespace); |
151
|
|
|
return $cachedData; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $cacheKey |
159
|
|
|
* @param string $namespace |
160
|
|
|
* @param string|int $ttl |
161
|
|
|
* @return CacheDataFormatter|mixed |
162
|
|
|
*/ |
163
|
|
|
public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
164
|
|
|
{ |
165
|
|
|
$cacheData = $this->cacheStore->getCache($cacheKey, $namespace, $ttl); |
166
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
167
|
|
|
|
168
|
|
|
if ($this->cacheStore->isSuccess() && ($this->compression || $this->encryptionKey !== null)) { |
169
|
|
|
$cacheData = CacheerHelper::recoverFromStorage($cacheData, $this->compression, $this->encryptionKey); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->formatted ? new CacheDataFormatter($cacheData) : $cacheData; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $cacheKeys |
177
|
|
|
* @param string $namespace |
178
|
|
|
* @param string|int $ttl |
179
|
|
|
* @return CacheDataFormatter|array |
180
|
|
|
*/ |
181
|
|
|
public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
182
|
|
|
{ |
183
|
|
|
$cachedData = $this->cacheStore->getMany($cacheKeys, $namespace, $ttl); |
184
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
185
|
|
|
|
186
|
|
|
if ($this->cacheStore->isSuccess() && ($this->compression || $this->encryptionKey !== null)) { |
187
|
|
|
foreach ($cachedData as &$data) { |
188
|
|
|
$data = CacheerHelper::recoverFromStorage($data, $this->compression, $this->encryptionKey); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->formatted ? new CacheDataFormatter($cachedData) : $cachedData; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $cacheKey |
197
|
|
|
* @param string $namespace |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function has(string $cacheKey, string $namespace = '') |
201
|
|
|
{ |
202
|
|
|
$this->cacheStore->has($cacheKey, $namespace); |
203
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $cacheKey |
208
|
|
|
* @param int $amount |
209
|
|
|
* @param string $namespace |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
213
|
|
|
{ |
214
|
|
|
$cacheData = $this->getCache($cacheKey, $namespace); |
215
|
|
|
|
216
|
|
|
if(!empty($cacheData) && is_numeric($cacheData)) { |
217
|
|
|
$this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
218
|
|
|
$this->setMessage($this->getMessage(), $this->isSuccess()); |
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return boolean |
227
|
|
|
*/ |
228
|
|
|
public function isSuccess() |
229
|
|
|
{ |
230
|
|
|
return $this->success; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $cacheKey |
235
|
|
|
* @param mixed $cacheData |
236
|
|
|
* @param string $namespace |
237
|
|
|
* @param string|int $ttl |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600) |
241
|
|
|
{ |
242
|
|
|
$data = CacheerHelper::prepareForStorage($cacheData, $this->compression, $this->encryptionKey); |
243
|
|
|
$this->cacheStore->putCache($cacheKey, $data, $namespace, $ttl); |
244
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param array $items |
249
|
|
|
* @param string $namespace |
250
|
|
|
* @param integer $batchSize |
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
254
|
|
|
{ |
255
|
|
|
$this->cacheStore->putMany($items, $namespace, $batchSize); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $cacheKey |
260
|
|
|
* @param string|int $ttl |
261
|
|
|
* @param string $namespace |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public function renewCache(string $cacheKey, string|int $ttl = 3600, string $namespace = '') |
265
|
|
|
{ |
266
|
|
|
$this->cacheStore->renewCache($cacheKey, $ttl, $namespace); |
267
|
|
|
|
268
|
|
|
if ($this->cacheStore->isSuccess()) { |
269
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
270
|
|
|
} else { |
271
|
|
|
$this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param string $cacheKey |
277
|
|
|
* @param int|string $ttl |
278
|
|
|
* @param Closure $callback |
279
|
|
|
* @return mixed |
280
|
|
|
*/ |
281
|
|
|
public function remember(string $cacheKey, int|string $ttl, Closure $callback) |
282
|
|
|
{ |
283
|
|
|
$cachedData = $this->getCache($cacheKey, ttl: $ttl); |
284
|
|
|
|
285
|
|
|
if(!empty($cachedData)) { |
286
|
|
|
return $cachedData; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$cacheData = $callback(); |
290
|
|
|
$this->putCache($cacheKey, $cacheData, ttl: $ttl); |
291
|
|
|
$this->setMessage($this->getMessage(), $this->isSuccess()); |
292
|
|
|
|
293
|
|
|
return $cacheData; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param string $cacheKey |
298
|
|
|
* @param Closure $callback |
299
|
|
|
* @return mixed |
300
|
|
|
*/ |
301
|
|
|
public function rememberForever(string $cacheKey, Closure $callback) |
302
|
|
|
{ |
303
|
|
|
return $this->remember($cacheKey, 31536000 * 1000, $callback); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return CacheConfig |
308
|
|
|
*/ |
309
|
|
|
public function setConfig() |
310
|
|
|
{ |
311
|
|
|
return new CacheConfig($this); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return CacheDriver |
316
|
|
|
*/ |
317
|
|
|
public function setDriver() |
318
|
|
|
{ |
319
|
|
|
return new CacheDriver($this); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $message |
324
|
|
|
* @param boolean $success |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
private function setMessage(string $message, bool $success) |
328
|
|
|
{ |
329
|
|
|
$this->message = $message; |
330
|
|
|
$this->success = $success; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function getMessage() |
337
|
|
|
{ |
338
|
|
|
return $this->message; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
public function useFormatter() |
345
|
|
|
{ |
346
|
|
|
$this->formatted = !$this->formatted; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param array $options |
351
|
|
|
* @return void |
352
|
|
|
*/ |
353
|
|
|
private function validateOptions(array $options) |
354
|
|
|
{ |
355
|
|
|
$this->options = $options; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Enable or disable data compression |
360
|
|
|
* |
361
|
|
|
* @param bool $status |
362
|
|
|
* @return $this |
363
|
|
|
*/ |
364
|
|
|
public function useCompression(bool $status = true) |
365
|
|
|
{ |
366
|
|
|
$this->compression = $status; |
367
|
|
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Enable encryption for cached data |
372
|
|
|
* |
373
|
|
|
* @param string $key |
374
|
|
|
* @return $this |
375
|
|
|
*/ |
376
|
|
|
public function useEncryption(string $key) |
377
|
|
|
{ |
378
|
|
|
$this->encryptionKey = $key; |
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|