1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
namespace phpFastCache\Core\Pool; |
15
|
|
|
|
16
|
|
|
use phpFastCache\Core\Item\ExtendedCacheItemInterface; |
17
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class DriverBaseTrait |
22
|
|
|
* @package phpFastCache\Cache |
23
|
|
|
*/ |
24
|
|
|
trait DriverBaseTrait |
25
|
|
|
{ |
26
|
|
|
use ExtendedCacheItemPoolTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array default options, this will be merge to Driver's Options |
30
|
|
|
*/ |
31
|
|
|
protected $config = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $fallback = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed Instance of driver service |
40
|
|
|
*/ |
41
|
|
|
protected $instance; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $config_name |
45
|
|
|
* @param string $value |
46
|
|
|
*/ |
47
|
|
|
public function setup($config_name, $value = '') |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Config for class |
51
|
|
|
*/ |
52
|
|
|
if (is_array($config_name)) { |
53
|
|
|
$this->config = array_merge($this->config, $config_name); |
54
|
|
|
} else { |
55
|
|
|
$this->config[ $config_name ] = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getConfig() |
63
|
|
|
{ |
64
|
|
|
return $this->config; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function getConfigOption($optionName) |
72
|
|
|
{ |
73
|
|
|
if(isset($this->config[$optionName])){ |
74
|
|
|
return $this->config[$optionName]; |
75
|
|
|
}else{ |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Encode data types such as object/array |
82
|
|
|
* for driver that does not support |
83
|
|
|
* non-scalar value |
84
|
|
|
* @param $data |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function encode($data) |
88
|
|
|
{ |
89
|
|
|
return serialize($data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Decode data types such as object/array |
94
|
|
|
* for driver that does not support |
95
|
|
|
* non-scalar value |
96
|
|
|
* @param $value |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
protected function decode($value) |
100
|
|
|
{ |
101
|
|
|
return @unserialize($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check phpModules or CGI |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function isPHPModule() |
109
|
|
|
{ |
110
|
|
|
return (PHP_SAPI === 'apache2handler' || strpos(PHP_SAPI, 'handler') !== false); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $class |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function isExistingDriver($class) |
119
|
|
|
{ |
120
|
|
|
return class_exists("\\phpFastCache\\Drivers\\{$class}"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $tag |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function _getTagName($tag) |
129
|
|
|
{ |
130
|
|
|
return "__tag__" . $tag; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \phpFastCache\Core\Item\ExtendedCacheItemInterface $item |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function driverPreWrap(ExtendedCacheItemInterface $item) |
138
|
|
|
{ |
139
|
|
|
$wrap = [ |
140
|
|
|
self::DRIVER_DATA_WRAPPER_INDEX => $item->get(), |
141
|
|
|
self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
142
|
|
|
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
if($this->config['itemDetailedDate']){ |
146
|
|
|
$wrap[ self::DRIVER_MDATE_WRAPPER_INDEX ] = new \DateTime(); |
147
|
|
|
/** |
148
|
|
|
* If the creation date exists |
149
|
|
|
* reuse it else set a new Date |
150
|
|
|
*/ |
151
|
|
|
$wrap[ self::DRIVER_CDATE_WRAPPER_INDEX ] = $item->getCreationDate() ?: new \DateTime(); |
152
|
|
|
}else{ |
153
|
|
|
$wrap[ self::DRIVER_MDATE_WRAPPER_INDEX ] = null; |
154
|
|
|
$wrap[ self::DRIVER_CDATE_WRAPPER_INDEX ] = null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $wrap; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $wrapper |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
public function driverUnwrapData(array $wrapper) |
165
|
|
|
{ |
166
|
|
|
return $wrapper[ self::DRIVER_DATA_WRAPPER_INDEX ]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $wrapper |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
|
|
public function driverUnwrapTags(array $wrapper) |
174
|
|
|
{ |
175
|
|
|
return $wrapper[ self::DRIVER_TAGS_WRAPPER_INDEX ]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $wrapper |
181
|
|
|
* @return \DateTime |
182
|
|
|
*/ |
183
|
|
|
public function driverUnwrapEdate(array $wrapper) |
184
|
|
|
{ |
185
|
|
|
return $wrapper[ self::DRIVER_EDATE_WRAPPER_INDEX ]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $wrapper |
190
|
|
|
* @return \DateTime |
191
|
|
|
*/ |
192
|
|
|
public function driverUnwrapCdate(array $wrapper) |
193
|
|
|
{ |
194
|
|
|
return $wrapper[ self::DRIVER_CDATE_WRAPPER_INDEX ]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $wrapper |
200
|
|
|
* @return \DateTime |
201
|
|
|
*/ |
202
|
|
|
public function driverUnwrapMdate(array $wrapper) |
203
|
|
|
{ |
204
|
|
|
return $wrapper[ self::DRIVER_MDATE_WRAPPER_INDEX ]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getDriverName() |
211
|
|
|
{ |
212
|
|
|
static $driverName; |
213
|
|
|
|
214
|
|
|
return ($driverName ?: $driverName = ucfirst(substr(strrchr((new \ReflectionObject($this))->getNamespaceName(), '\\'), 1))); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param \phpFastCache\Core\Item\ExtendedCacheItemInterface $item |
219
|
|
|
* @return bool |
220
|
|
|
* @throws \LogicException |
221
|
|
|
*/ |
222
|
|
|
public function driverWriteTags(ExtendedCacheItemInterface $item) |
223
|
|
|
{ |
224
|
|
|
/** |
225
|
|
|
* Do not attempt to write tags |
226
|
|
|
* on tags item, it can leads |
227
|
|
|
* to an infinite recursive calls |
228
|
|
|
*/ |
229
|
|
|
if(strpos($item->getKey(), self::DRIVER_TAGS_KEY_PREFIX ) === 0){ |
230
|
|
|
throw new \LogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @var $tagsItems ExtendedCacheItemInterface[] |
235
|
|
|
*/ |
236
|
|
|
$tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
237
|
|
|
|
238
|
|
|
foreach ($tagsItems as $tagsItem) { |
239
|
|
|
$data = $tagsItem->get(); |
240
|
|
|
$expTimestamp = $item->getExpirationDate()->getTimestamp(); |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Using the key will |
244
|
|
|
* avoid to use array_unique |
245
|
|
|
* that has slow performances |
246
|
|
|
*/ |
247
|
|
|
|
248
|
|
|
$tagsItem->set(array_merge((array) $data, [$item->getKey() => $expTimestamp])); |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set the expiration date |
252
|
|
|
* of the $tagsItem based |
253
|
|
|
* on the older $item |
254
|
|
|
* expiration date |
255
|
|
|
*/ |
256
|
|
|
if ($expTimestamp > $tagsItem->getExpirationDate()->getTimestamp()) { |
257
|
|
|
$tagsItem->expiresAt($item->getExpirationDate()); |
258
|
|
|
} |
259
|
|
|
$this->driverWrite($tagsItem); |
260
|
|
|
$tagsItem->setHit(true); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Also update removed tags to |
265
|
|
|
* keep the index up to date |
266
|
|
|
*/ |
267
|
|
|
$tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
268
|
|
|
|
269
|
|
|
foreach ($tagsItems as $tagsItem) { |
270
|
|
|
$data = (array) $tagsItem->get(); |
271
|
|
|
|
272
|
|
|
unset($data[ $item->getKey() ]); |
273
|
|
|
$tagsItem->set($data); |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Recalculate the expiration date |
277
|
|
|
* |
278
|
|
|
* If the $tagsItem does not have |
279
|
|
|
* any cache item references left |
280
|
|
|
* then remove it from tagsItems index |
281
|
|
|
*/ |
282
|
|
|
if (count($data)) { |
283
|
|
|
$tagsItem->expiresAt(max($data)); |
284
|
|
|
$this->driverWrite($tagsItem); |
285
|
|
|
$tagsItem->setHit(true); |
286
|
|
|
} else { |
287
|
|
|
$this->deleteItem($tagsItem->getKey()); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return true; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param $key |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
public function getTagKey($key) |
299
|
|
|
{ |
300
|
|
|
return self::DRIVER_TAGS_KEY_PREFIX . $key; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param $key |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function getTagKeys(array $keys) |
308
|
|
|
{ |
309
|
|
|
foreach ($keys as &$key) { |
310
|
|
|
$key = $this->getTagKey($key); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return $keys; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param string $optionName |
318
|
|
|
* @param mixed $optionValue |
319
|
|
|
* @return bool |
320
|
|
|
* @throws phpFastCacheInvalidArgumentException |
321
|
|
|
*/ |
322
|
|
|
public static function isValidOption($optionName, $optionValue) |
323
|
|
|
{ |
324
|
|
|
if (!is_string($optionName)) { |
325
|
|
|
throw new phpFastCacheInvalidArgumentException('$optionName must be a string'); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return true; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
|
|
public static function getRequiredOptions() |
335
|
|
|
{ |
336
|
|
|
return []; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
public static function getValidOptions() |
343
|
|
|
{ |
344
|
|
|
return []; |
345
|
|
|
} |
346
|
|
|
} |