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
|
|
|
* Indicate if cache should exist forever |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
public bool $forever = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Indicate if cache should exist till end of day |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
public bool $validForRestOfDay = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Indicate if cache should exist till end of week |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
public bool $validForRestOfWeek = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Cache TTL in seconds |
39
|
|
|
* ttl = 0 means we want cache queries forever |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public int $ttl = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Indicate if queries should refresh after create |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
public bool $refreshAfterCreate = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Indicate if queries should refresh after update |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
public bool $refreshAfterUpdate = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Indicate if queries should refresh after delete |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
public bool $refreshAfterDelete = true; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Indicate if queries should refresh after restore |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
public bool $refreshAfterRestore = true; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Specify default value of cache entity |
75
|
|
|
* |
76
|
|
|
* @var mixed |
77
|
|
|
*/ |
78
|
|
|
public mixed $default = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The anonymous function that should be executed to store cache values to cache store. |
82
|
|
|
* |
83
|
|
|
* @var Closure|null |
84
|
|
|
*/ |
85
|
|
|
public ?Closure $cacheClosure = null; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function __construct(string $name) |
89
|
|
|
{ |
90
|
|
|
$this->name = $name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a new cache entity. |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @return CacheEntity |
98
|
|
|
*/ |
99
|
|
|
public static function make(string $name): CacheEntity |
100
|
|
|
{ |
101
|
|
|
return new static($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Specify that the cache should refresh after create a model instance |
106
|
|
|
* |
107
|
|
|
* @param bool $status |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function refreshAfterCreate(bool $status = true): CacheEntity |
111
|
|
|
{ |
112
|
|
|
$this->refreshAfterCreate = $status; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Specify that the cache should refresh after update a model instance |
119
|
|
|
* |
120
|
|
|
* @param bool $status |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function refreshAfterUpdate(bool $status = true): CacheEntity |
124
|
|
|
{ |
125
|
|
|
$this->refreshAfterUpdate = $status; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Specify that the cache should refresh after delete a model instance |
132
|
|
|
* |
133
|
|
|
* @param bool $status |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function refreshAfterDelete(bool $status = true): CacheEntity |
137
|
|
|
{ |
138
|
|
|
$this->refreshAfterDelete = $status; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Specify that the cache should refresh after restore a model instance |
145
|
|
|
* |
146
|
|
|
* @param bool $status |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function refreshAfterRestore(bool $status = true): CacheEntity |
150
|
|
|
{ |
151
|
|
|
$this->refreshAfterRestore = $status; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Specify that cache entity should exist there forever |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function forever(): CacheEntity |
162
|
|
|
{ |
163
|
|
|
$this->forever = true; |
164
|
|
|
$this->validForRestOfDay = false; |
165
|
|
|
$this->validForRestOfWeek = false; |
166
|
|
|
$this->ttl = 0; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Specify that cache entity should exist there till end of day |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function validForRestOfDay(): CacheEntity |
177
|
|
|
{ |
178
|
|
|
$this->validForRestOfDay = true; |
179
|
|
|
$this->validForRestOfWeek = false; |
180
|
|
|
$this->forever = false; |
181
|
|
|
$this->ttl = 0; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Specify that cache entity should exist there till end of week |
188
|
|
|
* |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function validForRestOfWeek(): CacheEntity |
192
|
|
|
{ |
193
|
|
|
$this->validForRestOfDay = false; |
194
|
|
|
$this->validForRestOfWeek = true; |
195
|
|
|
$this->forever = false; |
196
|
|
|
$this->ttl = 0; |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Specify cache time to live in second |
203
|
|
|
* |
204
|
|
|
* @param int $seconds |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function ttl(int $seconds): CacheEntity |
208
|
|
|
{ |
209
|
|
|
$this->ttl = max($seconds, 0); |
210
|
|
|
$this->forever = $this->ttl === 0; |
211
|
|
|
$this->validForRestOfDay = false; |
212
|
|
|
$this->validForRestOfWeek = false; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set default value of cache entity |
219
|
|
|
* |
220
|
|
|
* @param mixed $defaultValue |
221
|
|
|
* @return $this |
222
|
|
|
*/ |
223
|
|
|
public function setDefault(mixed $defaultValue): CacheEntity |
224
|
|
|
{ |
225
|
|
|
$this->default = $defaultValue; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Specify cache closure |
232
|
|
|
* |
233
|
|
|
* @param Closure $closure |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
|
|
public function cache(Closure $closure): CacheEntity |
237
|
|
|
{ |
238
|
|
|
$this->cacheClosure = $closure; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|