1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\LaraCache; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
|
7
|
|
|
class CacheEntity |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Cache name |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public string $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Cache Driver (store) |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public string $driver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Indicate if cache should exist forever |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
public bool $forever = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Indicates if caching operation should perform in the background or not |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
public bool $isQueueable; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Queue name |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public string $queueName; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Queue connection |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public string $queueConnection; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Indicate if cache should debounce |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
public bool $debounce = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Debounce time in seconds |
61
|
|
|
* debounce = 0 means the debouncing is disabled |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
public int $debounceWaitTime = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Debounce queue name |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
public string $debounceQueueName; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Debounce queue connection |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
public string $debounceQueueConnection; |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Indicate if cache should exist till end of day |
84
|
|
|
* |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
public bool $validForRestOfDay = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Indicate if cache should exist till end of week |
91
|
|
|
* |
92
|
|
|
* @var bool |
93
|
|
|
*/ |
94
|
|
|
public bool $validForRestOfWeek = false; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Cache TTL in seconds |
98
|
|
|
* ttl = 0 means we want cache queries forever |
99
|
|
|
* |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
public int $ttl = 0; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Indicate if queries should refresh after create |
106
|
|
|
* |
107
|
|
|
* @var bool |
108
|
|
|
*/ |
109
|
|
|
public bool $refreshAfterCreate = true; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Indicate if queries should refresh after update |
113
|
|
|
* |
114
|
|
|
* @var bool |
115
|
|
|
*/ |
116
|
|
|
public bool $refreshAfterUpdate = true; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Indicate if queries should refresh after delete |
120
|
|
|
* |
121
|
|
|
* @var bool |
122
|
|
|
*/ |
123
|
|
|
public bool $refreshAfterDelete = true; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Indicate if queries should refresh after restore |
127
|
|
|
* |
128
|
|
|
* @var bool |
129
|
|
|
*/ |
130
|
|
|
public bool $refreshAfterRestore = true; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Specify default value of cache entity |
134
|
|
|
* |
135
|
|
|
* @var mixed |
136
|
|
|
*/ |
137
|
|
|
public mixed $default = null; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* The anonymous function that should be executed to store cache values to cache store. |
141
|
|
|
* |
142
|
|
|
* @var Closure|null |
143
|
|
|
*/ |
144
|
|
|
public ?Closure $cacheClosure = null; |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
public function __construct(string $name) |
148
|
|
|
{ |
149
|
|
|
$this->name = $name; |
150
|
|
|
$this->driver = config('laracache.driver') ?? config('cache.default'); |
151
|
|
|
|
152
|
|
|
$queue = config('laracache.queue'); |
153
|
|
|
$debounce = config('laracache.debounce'); |
154
|
|
|
|
155
|
|
|
if (is_array($queue)) { |
156
|
|
|
$this->isQueueable = $queue['status'] ?? false; |
157
|
|
|
$this->queueName = $queue['name'] ?? 'default'; |
158
|
|
|
$this->queueConnection = $queue['connection'] ?? config('queue.default'); |
159
|
|
|
} |
160
|
|
|
else { |
161
|
|
|
$this->isQueueable = (bool)$queue; |
162
|
|
|
$this->queueName = 'default'; |
163
|
|
|
$this->queueConnection = config('queue.default'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$debounceWaitTime = $debounce['wait'] ?? 5; |
167
|
|
|
|
168
|
|
|
$this->debounce = $debounceWaitTime > 0 ? ($debounce['status'] ?? false) : false; |
169
|
|
|
$this->debounceWaitTime = $debounceWaitTime; |
170
|
|
|
$this->debounceQueueName = $debounce['queue']['name'] ?? 'default'; |
171
|
|
|
$this->debounceQueueConnection = $debounce['queue']['connection'] ?? config('queue.default'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Create a new cache entity. |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* @return CacheEntity |
179
|
|
|
*/ |
180
|
|
|
public static function make(string $name): CacheEntity |
181
|
|
|
{ |
182
|
|
|
return new static($name); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Specify custom driver for cache entity |
187
|
|
|
* |
188
|
|
|
* @param string $driver |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function setDriver(string $driver): CacheEntity |
192
|
|
|
{ |
193
|
|
|
$this->driver = $driver; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Specify if cache operation should perform in background or not |
200
|
|
|
* |
201
|
|
|
* @param bool $status |
202
|
|
|
* @param string $onConnection |
203
|
|
|
* @param string $onQueue |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function isQueueable(bool $status = true, string $onConnection = '', string $onQueue = ''): CacheEntity |
207
|
|
|
{ |
208
|
|
|
$this->isQueueable = $status; |
209
|
|
|
|
210
|
|
|
if ($onConnection) { |
211
|
|
|
$this->queueConnection = $onConnection; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($onQueue) { |
215
|
|
|
$this->queueName = $onQueue; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Specify if cache should debounce |
224
|
|
|
* |
225
|
|
|
* @param bool $status |
226
|
|
|
* @param int $waitTime |
227
|
|
|
* @param string $onConnection |
228
|
|
|
* @param string $onQueue |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function debounce(bool $status = true, int $waitTime = 5, string $onConnection = '', string $onQueue = ''): CacheEntity |
232
|
|
|
{ |
233
|
|
|
$this->debounceWaitTime = max($waitTime, 0); |
234
|
|
|
$this->debounce = $waitTime > 0 ? $status : false; |
235
|
|
|
|
236
|
|
|
if ($onConnection) { |
237
|
|
|
$this->debounceQueueConnection = $onConnection; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($onQueue) { |
241
|
|
|
$this->debounceQueueName = $onQueue; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Specify that the cache should refresh after create a model instance |
250
|
|
|
* |
251
|
|
|
* @param bool $status |
252
|
|
|
* @return $this |
253
|
|
|
*/ |
254
|
|
|
public function refreshAfterCreate(bool $status = true): CacheEntity |
255
|
|
|
{ |
256
|
|
|
$this->refreshAfterCreate = $status; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Specify that the cache should refresh after update a model instance |
263
|
|
|
* |
264
|
|
|
* @param bool $status |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
|
|
public function refreshAfterUpdate(bool $status = true): CacheEntity |
268
|
|
|
{ |
269
|
|
|
$this->refreshAfterUpdate = $status; |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Specify that the cache should refresh after delete a model instance |
276
|
|
|
* |
277
|
|
|
* @param bool $status |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
|
|
public function refreshAfterDelete(bool $status = true): CacheEntity |
281
|
|
|
{ |
282
|
|
|
$this->refreshAfterDelete = $status; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Specify that the cache should refresh after restore a model instance |
289
|
|
|
* |
290
|
|
|
* @param bool $status |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
public function refreshAfterRestore(bool $status = true): CacheEntity |
294
|
|
|
{ |
295
|
|
|
$this->refreshAfterRestore = $status; |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Specify that cache entity should exist there forever |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function forever(): CacheEntity |
306
|
|
|
{ |
307
|
|
|
$this->forever = true; |
308
|
|
|
$this->validForRestOfDay = false; |
309
|
|
|
$this->validForRestOfWeek = false; |
310
|
|
|
$this->ttl = 0; |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Specify that cache entity should exist there till end of day |
317
|
|
|
* |
318
|
|
|
* @return $this |
319
|
|
|
*/ |
320
|
|
|
public function validForRestOfDay(): CacheEntity |
321
|
|
|
{ |
322
|
|
|
$this->validForRestOfDay = true; |
323
|
|
|
$this->validForRestOfWeek = false; |
324
|
|
|
$this->forever = false; |
325
|
|
|
$this->ttl = 0; |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Specify that cache entity should exist there till end of week |
332
|
|
|
* |
333
|
|
|
* @return $this |
334
|
|
|
*/ |
335
|
|
|
public function validForRestOfWeek(): CacheEntity |
336
|
|
|
{ |
337
|
|
|
$this->validForRestOfDay = false; |
338
|
|
|
$this->validForRestOfWeek = true; |
339
|
|
|
$this->forever = false; |
340
|
|
|
$this->ttl = 0; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Specify cache time to live in second |
347
|
|
|
* |
348
|
|
|
* @param int $seconds |
349
|
|
|
* @return $this |
350
|
|
|
*/ |
351
|
|
|
public function ttl(int $seconds): CacheEntity |
352
|
|
|
{ |
353
|
|
|
$this->ttl = max($seconds, 0); |
354
|
|
|
$this->forever = $this->ttl === 0; |
355
|
|
|
$this->validForRestOfDay = false; |
356
|
|
|
$this->validForRestOfWeek = false; |
357
|
|
|
|
358
|
|
|
return $this; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Set default value of cache entity |
363
|
|
|
* |
364
|
|
|
* @param mixed $defaultValue |
365
|
|
|
* @return $this |
366
|
|
|
*/ |
367
|
|
|
public function setDefault(mixed $defaultValue): CacheEntity |
368
|
|
|
{ |
369
|
|
|
$this->default = $defaultValue; |
370
|
|
|
|
371
|
|
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Get TTL |
376
|
|
|
* |
377
|
|
|
* @return int |
378
|
|
|
* @internal |
379
|
|
|
*/ |
380
|
|
|
public function getTtl(): int |
381
|
|
|
{ |
382
|
|
|
if ($this->forever) { |
383
|
|
|
return 0; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if ($this->validForRestOfDay) { |
387
|
|
|
return day_ending_seconds(); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
if ($this->validForRestOfWeek) { |
391
|
|
|
return week_ending_seconds(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return $this->ttl; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Specify cache closure |
399
|
|
|
* |
400
|
|
|
* @param Closure $closure |
401
|
|
|
* @return $this |
402
|
|
|
*/ |
403
|
|
|
public function cache(Closure $closure): CacheEntity |
404
|
|
|
{ |
405
|
|
|
$this->cacheClosure = $closure; |
406
|
|
|
|
407
|
|
|
return $this; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|