1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\caching; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Component; |
12
|
|
|
use yii\helpers\StringHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cache is the base class for cache classes supporting different cache storage implementations. |
16
|
|
|
* |
17
|
|
|
* A data item can be stored in the cache by calling [[set()]] and be retrieved back |
18
|
|
|
* later (in the same or different request) by [[get()]]. In both operations, |
19
|
|
|
* a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]] |
20
|
|
|
* can also be specified when calling [[set()]]. If the data item expires or the dependency |
21
|
|
|
* changes at the time of calling [[get()]], the cache will return no data. |
22
|
|
|
* |
23
|
|
|
* A typical usage pattern of cache is like the following: |
24
|
|
|
* |
25
|
|
|
* ```php |
26
|
|
|
* $key = 'demo'; |
27
|
|
|
* $data = $cache->get($key); |
28
|
|
|
* if ($data === false) { |
29
|
|
|
* // ...generate $data here... |
30
|
|
|
* $cache->set($key, $data, $duration, $dependency); |
31
|
|
|
* } |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* Because Cache implements the [[\ArrayAccess]] interface, it can be used like an array. For example, |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* $cache['foo'] = 'some data'; |
38
|
|
|
* echo $cache['foo']; |
39
|
|
|
* ``` |
40
|
|
|
* |
41
|
|
|
* Derived classes should implement the following methods which do the actual cache storage operations: |
42
|
|
|
* |
43
|
|
|
* - [[getValue()]]: retrieve the value with a key (if any) from cache |
44
|
|
|
* - [[setValue()]]: store the value with a key into cache |
45
|
|
|
* - [[addValue()]]: store the value only if the cache does not have this key before |
46
|
|
|
* - [[deleteValue()]]: delete the value with the specified key from cache |
47
|
|
|
* - [[flushValues()]]: delete all values from cache |
48
|
|
|
* |
49
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
50
|
|
|
* |
51
|
|
|
* @author Qiang Xue <[email protected]> |
52
|
|
|
* @since 2.0 |
53
|
|
|
*/ |
54
|
|
|
abstract class Cache extends Component implements CacheInterface |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var string|callable A string prefixed to every cache key so that it is unique globally in the whole cache storage. |
58
|
|
|
* It is recommended that you set a unique cache key prefix for each application if the same cache |
59
|
|
|
* storage is being used by different applications. |
60
|
|
|
* |
61
|
|
|
* To ensure interoperability, only alphanumeric characters should be used. |
62
|
|
|
* |
63
|
|
|
* since 2.0.X its allowed to use a closure function, this can be usefull when working with environment variables or |
64
|
|
|
* any other environment depending cache prefix. |
65
|
|
|
*/ |
66
|
|
|
public $keyPrefix; |
67
|
|
|
/** |
68
|
|
|
* @var array|null|false the functions used to serialize and unserialize cached data. Defaults to null, meaning |
69
|
|
|
* using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient |
70
|
|
|
* serializer (e.g. [igbinary](https://pecl.php.net/package/igbinary)), you may configure this property with |
71
|
|
|
* a two-element array. The first element specifies the serialization function, and the second the deserialization |
72
|
|
|
* function. If this property is set false, data will be directly sent to and retrieved from the underlying |
73
|
|
|
* cache component without any serialization or deserialization. You should not turn off serialization if |
74
|
|
|
* you are using [[Dependency|cache dependency]], because it relies on data serialization. Also, some |
75
|
|
|
* implementations of the cache can not correctly save and retrieve data different from a string type. |
76
|
|
|
*/ |
77
|
|
|
public $serializer; |
78
|
|
|
/** |
79
|
|
|
* @var int default duration in seconds before a cache entry will expire. Default value is 0, meaning infinity. |
80
|
|
|
* This value is used by [[set()]] if the duration is not explicitly given. |
81
|
|
|
* @since 2.0.11 |
82
|
|
|
*/ |
83
|
|
|
public $defaultDuration = 0; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var bool whether [igbinary serialization](https://pecl.php.net/package/igbinary) is available or not. |
87
|
|
|
*/ |
88
|
|
|
private $_igbinaryAvailable = false; |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
270 |
|
public function init() |
95
|
|
|
{ |
96
|
270 |
|
parent::init(); |
97
|
270 |
|
$this->_igbinaryAvailable = \extension_loaded('igbinary'); |
98
|
270 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Builds a normalized cache key from a given key. |
102
|
|
|
* |
103
|
|
|
* If the given key is a string containing alphanumeric characters only and no more than 32 characters, |
104
|
|
|
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key |
105
|
|
|
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]]. |
106
|
|
|
* |
107
|
|
|
* @param mixed $key the key to be normalized |
108
|
|
|
* @return string the generated cache key |
109
|
|
|
*/ |
110
|
246 |
|
public function buildKey($key) |
111
|
|
|
{ |
112
|
246 |
|
if (is_string($key)) { |
113
|
202 |
|
$key = ctype_alnum($key) && StringHelper::byteLength($key) <= 32 ? $key : md5($key); |
114
|
|
|
} else { |
115
|
74 |
|
if ($this->_igbinaryAvailable) { |
116
|
|
|
$serializedKey = igbinary_serialize($key); |
117
|
|
|
} else { |
118
|
74 |
|
$serializedKey = serialize($key); |
119
|
|
|
} |
120
|
|
|
|
121
|
74 |
|
$key = md5($serializedKey); |
122
|
|
|
} |
123
|
|
|
|
124
|
246 |
|
$prefix = is_callable($this->keyPrefix) ? call_user_func($this->keyPrefix, $key, $this) : $this->keyPrefix; |
125
|
|
|
|
126
|
246 |
|
return $prefix . $key; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieves a value from cache with a specified key. |
131
|
|
|
* @param mixed $key a key identifying the cached value. This can be a simple string or |
132
|
|
|
* a complex data structure consisting of factors representing the key. |
133
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache, expired, |
134
|
|
|
* or the dependency associated with the cached data has changed. |
135
|
|
|
*/ |
136
|
147 |
|
public function get($key) |
137
|
|
|
{ |
138
|
147 |
|
$key = $this->buildKey($key); |
139
|
147 |
|
$value = $this->getValue($key); |
140
|
147 |
|
if ($value === false || $this->serializer === false) { |
141
|
117 |
|
return $value; |
142
|
90 |
|
} elseif ($this->serializer === null) { |
143
|
90 |
|
$value = unserialize((string)$value); |
144
|
|
|
} else { |
145
|
|
|
$value = call_user_func($this->serializer[1], $value); |
146
|
|
|
} |
147
|
90 |
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) { |
148
|
89 |
|
return $value[0]; |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Checks whether a specified key exists in the cache. |
156
|
|
|
* This can be faster than getting the value from the cache if the data is big. |
157
|
|
|
* In case a cache does not support this feature natively, this method will try to simulate it |
158
|
|
|
* but has no performance improvement over getting it. |
159
|
|
|
* Note that this method does not check whether the dependency associated |
160
|
|
|
* with the cached data, if there is any, has changed. So a call to [[get]] |
161
|
|
|
* may return false while exists returns true. |
162
|
|
|
* @param mixed $key a key identifying the cached value. This can be a simple string or |
163
|
|
|
* a complex data structure consisting of factors representing the key. |
164
|
|
|
* @return bool true if a value exists in cache, false if the value is not in the cache or expired. |
165
|
|
|
*/ |
166
|
|
|
public function exists($key) |
167
|
|
|
{ |
168
|
|
|
$key = $this->buildKey($key); |
169
|
|
|
$value = $this->getValue($key); |
170
|
|
|
|
171
|
|
|
return $value !== false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Retrieves multiple values from cache with the specified keys. |
176
|
|
|
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
177
|
|
|
* which may improve the performance. In case a cache does not support this feature natively, |
178
|
|
|
* this method will try to simulate it. |
179
|
|
|
* |
180
|
|
|
* @param string[] $keys list of string keys identifying the cached values |
181
|
|
|
* @return array list of cached values corresponding to the specified keys. The array |
182
|
|
|
* is returned in terms of (key, value) pairs. |
183
|
|
|
* If a value is not cached or expired, the corresponding array value will be false. |
184
|
|
|
* @deprecated This method is an alias for [[multiGet()]] and will be removed in 2.1.0. |
185
|
|
|
*/ |
186
|
|
|
public function mget($keys) |
187
|
|
|
{ |
188
|
|
|
return $this->multiGet($keys); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Retrieves multiple values from cache with the specified keys. |
193
|
|
|
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
194
|
|
|
* which may improve the performance. In case a cache does not support this feature natively, |
195
|
|
|
* this method will try to simulate it. |
196
|
|
|
* @param string[] $keys list of string keys identifying the cached values |
197
|
|
|
* @return array list of cached values corresponding to the specified keys. The array |
198
|
|
|
* is returned in terms of (key, value) pairs. |
199
|
|
|
* If a value is not cached or expired, the corresponding array value will be false. |
200
|
|
|
* @since 2.0.7 |
201
|
|
|
*/ |
202
|
34 |
|
public function multiGet($keys) |
203
|
|
|
{ |
204
|
34 |
|
$keyMap = []; |
205
|
34 |
|
foreach ($keys as $key) { |
206
|
34 |
|
$keyMap[$key] = $this->buildKey($key); |
207
|
|
|
} |
208
|
34 |
|
$values = $this->getValues(array_values($keyMap)); |
209
|
34 |
|
$results = []; |
210
|
34 |
|
foreach ($keyMap as $key => $newKey) { |
211
|
34 |
|
$results[$key] = false; |
212
|
34 |
|
if (isset($values[$newKey])) { |
213
|
34 |
|
if ($this->serializer === false) { |
214
|
|
|
$results[$key] = $values[$newKey]; |
215
|
|
|
} else { |
216
|
34 |
|
$value = $this->serializer === null ? unserialize($values[$newKey]) |
217
|
34 |
|
: call_user_func($this->serializer[1], $values[$newKey]); |
218
|
|
|
|
219
|
34 |
|
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) { |
220
|
34 |
|
$results[$key] = $value[0]; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
34 |
|
return $results; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Stores a value identified by a key into cache. |
231
|
|
|
* If the cache already contains such a key, the existing value and |
232
|
|
|
* expiration time will be replaced with the new ones, respectively. |
233
|
|
|
* |
234
|
|
|
* @param mixed $key a key identifying the value to be cached. This can be a simple string or |
235
|
|
|
* a complex data structure consisting of factors representing the key. |
236
|
|
|
* @param mixed $value the value to be cached |
237
|
|
|
* @param int|null $duration default duration in seconds before the cache will expire. If not set, |
238
|
|
|
* default [[defaultDuration]] value is used. |
239
|
|
|
* @param Dependency|null $dependency dependency of the cached item. If the dependency changes, |
240
|
|
|
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
241
|
|
|
* This parameter is ignored if [[serializer]] is false. |
242
|
|
|
* @return bool whether the value is successfully stored into cache |
243
|
|
|
*/ |
244
|
138 |
|
public function set($key, $value, $duration = null, $dependency = null) |
245
|
|
|
{ |
246
|
138 |
|
if ($duration === null) { |
247
|
71 |
|
$duration = $this->defaultDuration; |
248
|
|
|
} |
249
|
|
|
|
250
|
138 |
|
if ($dependency !== null && $this->serializer !== false) { |
251
|
31 |
|
$dependency->evaluateDependency($this); |
252
|
|
|
} |
253
|
138 |
|
if ($this->serializer === null) { |
254
|
138 |
|
$value = serialize([$value, $dependency]); |
255
|
|
|
} elseif ($this->serializer !== false) { |
256
|
|
|
$value = call_user_func($this->serializer[0], [$value, $dependency]); |
257
|
|
|
} |
258
|
138 |
|
$key = $this->buildKey($key); |
259
|
|
|
|
260
|
138 |
|
return $this->setValue($key, $value, $duration); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Stores multiple items in cache. Each item contains a value identified by a key. |
265
|
|
|
* If the cache already contains such a key, the existing value and |
266
|
|
|
* expiration time will be replaced with the new ones, respectively. |
267
|
|
|
* |
268
|
|
|
* @param array $items the items to be cached, as key-value pairs. |
269
|
|
|
* @param int|null $duration default duration in seconds before the cache will expire. If not set, |
270
|
|
|
* default [[defaultDuration]] value is used. |
271
|
|
|
* @param Dependency|null $dependency dependency of the cached items. If the dependency changes, |
272
|
|
|
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
273
|
|
|
* This parameter is ignored if [[serializer]] is false. |
274
|
|
|
* @return array array of failed keys |
275
|
|
|
* @deprecated This method is an alias for [[multiSet()]] and will be removed in 2.1.0. |
276
|
|
|
*/ |
277
|
|
|
public function mset($items, $duration = null, $dependency = null) |
278
|
|
|
{ |
279
|
|
|
return $this->multiSet($items, $duration, $dependency); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Stores multiple items in cache. Each item contains a value identified by a key. |
284
|
|
|
* If the cache already contains such a key, the existing value and |
285
|
|
|
* expiration time will be replaced with the new ones, respectively. |
286
|
|
|
* |
287
|
|
|
* @param array $items the items to be cached, as key-value pairs. |
288
|
|
|
* @param int|null $duration default duration in seconds before the cache will expire. If not set, |
289
|
|
|
* default [[defaultDuration]] value is used. |
290
|
|
|
* @param Dependency|null $dependency dependency of the cached items. If the dependency changes, |
291
|
|
|
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
292
|
|
|
* This parameter is ignored if [[serializer]] is false. |
293
|
|
|
* @return array array of failed keys |
294
|
|
|
* @since 2.0.7 |
295
|
|
|
*/ |
296
|
35 |
|
public function multiSet($items, $duration = null, $dependency = null) |
297
|
|
|
{ |
298
|
35 |
|
if ($duration === null) { |
299
|
27 |
|
$duration = $this->defaultDuration; |
300
|
|
|
} |
301
|
|
|
|
302
|
35 |
|
if ($dependency !== null && $this->serializer !== false) { |
303
|
|
|
$dependency->evaluateDependency($this); |
304
|
|
|
} |
305
|
|
|
|
306
|
35 |
|
$data = []; |
307
|
35 |
|
foreach ($items as $key => $value) { |
308
|
35 |
|
if ($this->serializer === null) { |
309
|
35 |
|
$value = serialize([$value, $dependency]); |
310
|
|
|
} elseif ($this->serializer !== false) { |
311
|
|
|
$value = call_user_func($this->serializer[0], [$value, $dependency]); |
312
|
|
|
} |
313
|
|
|
|
314
|
35 |
|
$key = $this->buildKey($key); |
315
|
35 |
|
$data[$key] = $value; |
316
|
|
|
} |
317
|
|
|
|
318
|
35 |
|
return $this->setValues($data, $duration); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Stores multiple items in cache. Each item contains a value identified by a key. |
323
|
|
|
* If the cache already contains such a key, the existing value and expiration time will be preserved. |
324
|
|
|
* |
325
|
|
|
* @param array $items the items to be cached, as key-value pairs. |
326
|
|
|
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire. |
327
|
|
|
* @param Dependency|null $dependency dependency of the cached items. If the dependency changes, |
328
|
|
|
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
329
|
|
|
* This parameter is ignored if [[serializer]] is false. |
330
|
|
|
* @return array array of failed keys |
331
|
|
|
* @deprecated This method is an alias for [[multiAdd()]] and will be removed in 2.1.0. |
332
|
|
|
*/ |
333
|
|
|
public function madd($items, $duration = 0, $dependency = null) |
334
|
|
|
{ |
335
|
|
|
return $this->multiAdd($items, $duration, $dependency); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Stores multiple items in cache. Each item contains a value identified by a key. |
340
|
|
|
* If the cache already contains such a key, the existing value and expiration time will be preserved. |
341
|
|
|
* |
342
|
|
|
* @param array $items the items to be cached, as key-value pairs. |
343
|
|
|
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire. |
344
|
|
|
* @param Dependency|null $dependency dependency of the cached items. If the dependency changes, |
345
|
|
|
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
346
|
|
|
* This parameter is ignored if [[serializer]] is false. |
347
|
|
|
* @return array array of failed keys |
348
|
|
|
* @since 2.0.7 |
349
|
|
|
*/ |
350
|
4 |
|
public function multiAdd($items, $duration = 0, $dependency = null) |
351
|
|
|
{ |
352
|
4 |
|
if ($dependency !== null && $this->serializer !== false) { |
353
|
|
|
$dependency->evaluateDependency($this); |
354
|
|
|
} |
355
|
|
|
|
356
|
4 |
|
$data = []; |
357
|
4 |
|
foreach ($items as $key => $value) { |
358
|
4 |
|
if ($this->serializer === null) { |
359
|
4 |
|
$value = serialize([$value, $dependency]); |
360
|
|
|
} elseif ($this->serializer !== false) { |
361
|
|
|
$value = call_user_func($this->serializer[0], [$value, $dependency]); |
362
|
|
|
} |
363
|
|
|
|
364
|
4 |
|
$key = $this->buildKey($key); |
365
|
4 |
|
$data[$key] = $value; |
366
|
|
|
} |
367
|
|
|
|
368
|
4 |
|
return $this->addValues($data, $duration); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
373
|
|
|
* Nothing will be done if the cache already contains the key. |
374
|
|
|
* @param mixed $key a key identifying the value to be cached. This can be a simple string or |
375
|
|
|
* a complex data structure consisting of factors representing the key. |
376
|
|
|
* @param mixed $value the value to be cached |
377
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
378
|
|
|
* @param Dependency|null $dependency dependency of the cached item. If the dependency changes, |
379
|
|
|
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
380
|
|
|
* This parameter is ignored if [[serializer]] is false. |
381
|
|
|
* @return bool whether the value is successfully stored into cache |
382
|
|
|
*/ |
383
|
8 |
|
public function add($key, $value, $duration = 0, $dependency = null) |
384
|
|
|
{ |
385
|
8 |
|
if ($dependency !== null && $this->serializer !== false) { |
386
|
|
|
$dependency->evaluateDependency($this); |
387
|
|
|
} |
388
|
8 |
|
if ($this->serializer === null) { |
389
|
8 |
|
$value = serialize([$value, $dependency]); |
390
|
|
|
} elseif ($this->serializer !== false) { |
391
|
|
|
$value = call_user_func($this->serializer[0], [$value, $dependency]); |
392
|
|
|
} |
393
|
8 |
|
$key = $this->buildKey($key); |
394
|
|
|
|
395
|
8 |
|
return $this->addValue($key, $value, $duration); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Deletes a value with the specified key from cache. |
400
|
|
|
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or |
401
|
|
|
* a complex data structure consisting of factors representing the key. |
402
|
|
|
* @return bool if no error happens during deletion |
403
|
|
|
*/ |
404
|
128 |
|
public function delete($key) |
405
|
|
|
{ |
406
|
128 |
|
$key = $this->buildKey($key); |
407
|
|
|
|
408
|
128 |
|
return $this->deleteValue($key); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Deletes all values from cache. |
413
|
|
|
* Be careful of performing this operation if the cache is shared among multiple applications. |
414
|
|
|
* @return bool whether the flush operation was successful. |
415
|
|
|
*/ |
416
|
48 |
|
public function flush() |
417
|
|
|
{ |
418
|
48 |
|
return $this->flushValues(); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Retrieves a value from cache with a specified key. |
423
|
|
|
* This method should be implemented by child classes to retrieve the data |
424
|
|
|
* from specific cache storage. |
425
|
|
|
* @param string $key a unique key identifying the cached value |
426
|
|
|
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired. Most often |
427
|
|
|
* value is a string. If you have disabled [[serializer]], it could be something else. |
428
|
|
|
*/ |
429
|
|
|
abstract protected function getValue($key); |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Stores a value identified by a key in cache. |
433
|
|
|
* This method should be implemented by child classes to store the data |
434
|
|
|
* in specific cache storage. |
435
|
|
|
* @param string $key the key identifying the value to be cached |
436
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
437
|
|
|
* it could be something else. |
438
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
439
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise |
440
|
|
|
*/ |
441
|
|
|
abstract protected function setValue($key, $value, $duration); |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
445
|
|
|
* This method should be implemented by child classes to store the data |
446
|
|
|
* in specific cache storage. |
447
|
|
|
* @param string $key the key identifying the value to be cached |
448
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
449
|
|
|
* it could be something else. |
450
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
451
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise |
452
|
|
|
*/ |
453
|
|
|
abstract protected function addValue($key, $value, $duration); |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Deletes a value with the specified key from cache |
457
|
|
|
* This method should be implemented by child classes to delete the data from actual cache storage. |
458
|
|
|
* @param string $key the key of the value to be deleted |
459
|
|
|
* @return bool if no error happens during deletion |
460
|
|
|
*/ |
461
|
|
|
abstract protected function deleteValue($key); |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Deletes all values from cache. |
465
|
|
|
* Child classes may implement this method to realize the flush operation. |
466
|
|
|
* @return bool whether the flush operation was successful. |
467
|
|
|
*/ |
468
|
|
|
abstract protected function flushValues(); |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Retrieves multiple values from cache with the specified keys. |
472
|
|
|
* The default implementation calls [[getValue()]] multiple times to retrieve |
473
|
|
|
* the cached values one by one. If the underlying cache storage supports multiget, |
474
|
|
|
* this method should be overridden to exploit that feature. |
475
|
|
|
* @param array $keys a list of keys identifying the cached values |
476
|
|
|
* @return array a list of cached values indexed by the keys |
477
|
|
|
*/ |
478
|
30 |
|
protected function getValues($keys) |
479
|
|
|
{ |
480
|
30 |
|
$results = []; |
481
|
30 |
|
foreach ($keys as $key) { |
482
|
30 |
|
$results[$key] = $this->getValue($key); |
483
|
|
|
} |
484
|
|
|
|
485
|
30 |
|
return $results; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Stores multiple key-value pairs in cache. |
490
|
|
|
* The default implementation calls [[setValue()]] multiple times store values one by one. If the underlying cache |
491
|
|
|
* storage supports multi-set, this method should be overridden to exploit that feature. |
492
|
|
|
* @param array $data array where key corresponds to cache key while value is the value stored |
493
|
|
|
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
494
|
|
|
* @return array array of failed keys |
495
|
|
|
*/ |
496
|
35 |
|
protected function setValues($data, $duration) |
497
|
|
|
{ |
498
|
35 |
|
$failedKeys = []; |
499
|
35 |
|
foreach ($data as $key => $value) { |
500
|
35 |
|
if ($this->setValue($key, $value, $duration) === false) { |
501
|
|
|
$failedKeys[] = $key; |
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
|
505
|
35 |
|
return $failedKeys; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Adds multiple key-value pairs to cache. |
510
|
|
|
* The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache |
511
|
|
|
* storage supports multi-add, this method should be overridden to exploit that feature. |
512
|
|
|
* @param array $data array where key corresponds to cache key while value is the value stored. |
513
|
|
|
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
514
|
|
|
* @return array array of failed keys |
515
|
|
|
*/ |
516
|
4 |
|
protected function addValues($data, $duration) |
517
|
|
|
{ |
518
|
4 |
|
$failedKeys = []; |
519
|
4 |
|
foreach ($data as $key => $value) { |
520
|
4 |
|
if ($this->addValue($key, $value, $duration) === false) { |
521
|
4 |
|
$failedKeys[] = $key; |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
|
525
|
4 |
|
return $failedKeys; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Returns whether there is a cache entry with a specified key. |
530
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
531
|
|
|
* @param string $key a key identifying the cached value |
532
|
|
|
* @return bool |
533
|
|
|
*/ |
534
|
|
|
#[\ReturnTypeWillChange] |
535
|
|
|
public function offsetExists($key) |
536
|
|
|
{ |
537
|
|
|
return $this->get($key) !== false; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Retrieves the value from cache with a specified key. |
542
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
543
|
|
|
* @param string $key a key identifying the cached value |
544
|
|
|
* @return mixed the value stored in cache, false if the value is not in the cache or expired. |
545
|
|
|
*/ |
546
|
|
|
#[\ReturnTypeWillChange] |
547
|
4 |
|
public function offsetGet($key) |
548
|
|
|
{ |
549
|
4 |
|
return $this->get($key); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Stores the value identified by a key into cache. |
554
|
|
|
* If the cache already contains such a key, the existing value will be |
555
|
|
|
* replaced with the new ones. To add expiration and dependencies, use the [[set()]] method. |
556
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
557
|
|
|
* @param string $key the key identifying the value to be cached |
558
|
|
|
* @param mixed $value the value to be cached |
559
|
|
|
*/ |
560
|
|
|
#[\ReturnTypeWillChange] |
561
|
40 |
|
public function offsetSet($key, $value) |
562
|
|
|
{ |
563
|
40 |
|
$this->set($key, $value); |
564
|
40 |
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Deletes the value with the specified key from cache |
568
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
569
|
|
|
* @param string $key the key of the value to be deleted |
570
|
|
|
*/ |
571
|
|
|
#[\ReturnTypeWillChange] |
572
|
|
|
public function offsetUnset($key) |
573
|
|
|
{ |
574
|
|
|
$this->delete($key); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key, |
579
|
|
|
* or to store the result of $callable execution if there is no cache available for the $key. |
580
|
|
|
* |
581
|
|
|
* Usage example: |
582
|
|
|
* |
583
|
|
|
* ```php |
584
|
|
|
* public function getTopProducts($count = 10) { |
585
|
|
|
* $cache = $this->cache; // Could be Yii::$app->cache |
586
|
|
|
* return $cache->getOrSet(['top-n-products', 'n' => $count], function () use ($count) { |
587
|
|
|
* return Products::find()->mostPopular()->limit($count)->all(); |
588
|
|
|
* }, 1000); |
589
|
|
|
* } |
590
|
|
|
* ``` |
591
|
|
|
* |
592
|
|
|
* @param mixed $key a key identifying the value to be cached. This can be a simple string or |
593
|
|
|
* a complex data structure consisting of factors representing the key. |
594
|
|
|
* @param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached. |
595
|
|
|
* If you use $callable that can return `false`, then keep in mind that [[getOrSet()]] may work inefficiently |
596
|
|
|
* because the [[yii\caching\Cache::get()]] method uses `false` return value to indicate the data item is not found |
597
|
|
|
* in the cache. Thus, caching of `false` value will lead to unnecessary internal calls. |
598
|
|
|
* @param int|null $duration default duration in seconds before the cache will expire. If not set, |
599
|
|
|
* [[defaultDuration]] value will be used. |
600
|
|
|
* @param Dependency|null $dependency dependency of the cached item. If the dependency changes, |
601
|
|
|
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
602
|
|
|
* This parameter is ignored if [[serializer]] is `false`. |
603
|
|
|
* @return mixed result of $callable execution |
604
|
|
|
* @since 2.0.11 |
605
|
|
|
*/ |
606
|
8 |
|
public function getOrSet($key, $callable, $duration = null, $dependency = null) |
607
|
|
|
{ |
608
|
8 |
|
if (($value = $this->get($key)) !== false) { |
609
|
4 |
|
return $value; |
610
|
|
|
} |
611
|
|
|
|
612
|
8 |
|
$value = call_user_func($callable, $this); |
613
|
8 |
|
if (!$this->set($key, $value, $duration, $dependency)) { |
614
|
|
|
Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__); |
615
|
|
|
} |
616
|
|
|
|
617
|
8 |
|
return $value; |
618
|
|
|
} |
619
|
|
|
} |
620
|
|
|
|