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