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
|
|
|
* Indicate if cache should exist till end of day |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
public bool $validForRestOfDay = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Indicate if cache should exist till end of week |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public bool $validForRestOfWeek = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Cache TTL in seconds |
53
|
|
|
* ttl = 0 means we want cache queries forever |
54
|
|
|
* |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
public int $ttl = 0; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Indicate if queries should refresh after create |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
public bool $refreshAfterCreate = true; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Indicate if queries should refresh after update |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
public bool $refreshAfterUpdate = true; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Indicate if queries should refresh after delete |
75
|
|
|
* |
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
public bool $refreshAfterDelete = true; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Indicate if queries should refresh after restore |
82
|
|
|
* |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
public bool $refreshAfterRestore = true; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Specify default value of cache entity |
89
|
|
|
* |
90
|
|
|
* @var mixed |
91
|
|
|
*/ |
92
|
|
|
public mixed $default = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The anonymous function that should be executed to store cache values to cache store. |
96
|
|
|
* |
97
|
|
|
* @var Closure|null |
98
|
|
|
*/ |
99
|
|
|
public ?Closure $cacheClosure = null; |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
public function __construct(string $name) |
103
|
|
|
{ |
104
|
|
|
$this->name = $name; |
105
|
|
|
$this->driver = config('laracache.driver') ?? config('cache.default'); |
106
|
|
|
$this->isQueueable = config('laracache.queue') ?? false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create a new cache entity. |
111
|
|
|
* |
112
|
|
|
* @param string $name |
113
|
|
|
* @return CacheEntity |
114
|
|
|
*/ |
115
|
|
|
public static function make(string $name): CacheEntity |
116
|
|
|
{ |
117
|
|
|
return new static($name); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Specify custom driver for cache entity |
122
|
|
|
* |
123
|
|
|
* @param string $driver |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setDriver(string $driver): CacheEntity |
127
|
|
|
{ |
128
|
|
|
$this->driver = $driver; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Specify if cache operation should perform in background or not |
135
|
|
|
* |
136
|
|
|
* @param bool $status |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function isQueueable(bool $status = true): CacheEntity |
140
|
|
|
{ |
141
|
|
|
$this->isQueueable = $status; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Specify that the cache should refresh after create a model instance |
148
|
|
|
* |
149
|
|
|
* @param bool $status |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function refreshAfterCreate(bool $status = true): CacheEntity |
153
|
|
|
{ |
154
|
|
|
$this->refreshAfterCreate = $status; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Specify that the cache should refresh after update a model instance |
161
|
|
|
* |
162
|
|
|
* @param bool $status |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function refreshAfterUpdate(bool $status = true): CacheEntity |
166
|
|
|
{ |
167
|
|
|
$this->refreshAfterUpdate = $status; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Specify that the cache should refresh after delete a model instance |
174
|
|
|
* |
175
|
|
|
* @param bool $status |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function refreshAfterDelete(bool $status = true): CacheEntity |
179
|
|
|
{ |
180
|
|
|
$this->refreshAfterDelete = $status; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Specify that the cache should refresh after restore a model instance |
187
|
|
|
* |
188
|
|
|
* @param bool $status |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function refreshAfterRestore(bool $status = true): CacheEntity |
192
|
|
|
{ |
193
|
|
|
$this->refreshAfterRestore = $status; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Specify that cache entity should exist there forever |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function forever(): CacheEntity |
204
|
|
|
{ |
205
|
|
|
$this->forever = true; |
206
|
|
|
$this->validForRestOfDay = false; |
207
|
|
|
$this->validForRestOfWeek = false; |
208
|
|
|
$this->ttl = 0; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Specify that cache entity should exist there till end of day |
215
|
|
|
* |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
public function validForRestOfDay(): CacheEntity |
219
|
|
|
{ |
220
|
|
|
$this->validForRestOfDay = true; |
221
|
|
|
$this->validForRestOfWeek = false; |
222
|
|
|
$this->forever = false; |
223
|
|
|
$this->ttl = 0; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Specify that cache entity should exist there till end of week |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function validForRestOfWeek(): CacheEntity |
234
|
|
|
{ |
235
|
|
|
$this->validForRestOfDay = false; |
236
|
|
|
$this->validForRestOfWeek = true; |
237
|
|
|
$this->forever = false; |
238
|
|
|
$this->ttl = 0; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Specify cache time to live in second |
245
|
|
|
* |
246
|
|
|
* @param int $seconds |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
|
|
public function ttl(int $seconds): CacheEntity |
250
|
|
|
{ |
251
|
|
|
$this->ttl = max($seconds, 0); |
252
|
|
|
$this->forever = $this->ttl === 0; |
253
|
|
|
$this->validForRestOfDay = false; |
254
|
|
|
$this->validForRestOfWeek = false; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set default value of cache entity |
261
|
|
|
* |
262
|
|
|
* @param mixed $defaultValue |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
public function setDefault(mixed $defaultValue): CacheEntity |
266
|
|
|
{ |
267
|
|
|
$this->default = $defaultValue; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get TTL |
274
|
|
|
* |
275
|
|
|
* @return int |
276
|
|
|
* @internal |
277
|
|
|
*/ |
278
|
|
|
public function getTtl(): int |
279
|
|
|
{ |
280
|
|
|
if ($this->forever) { |
281
|
|
|
return 0; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ($this->validForRestOfDay) { |
285
|
|
|
return day_ending_seconds(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ($this->validForRestOfWeek) { |
289
|
|
|
return week_ending_seconds(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $this->ttl; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Specify cache closure |
297
|
|
|
* |
298
|
|
|
* @param Closure $closure |
299
|
|
|
* @return $this |
300
|
|
|
*/ |
301
|
|
|
public function cache(Closure $closure): CacheEntity |
302
|
|
|
{ |
303
|
|
|
$this->cacheClosure = $closure; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|