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