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; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Config\ConfigurationOption; |
19
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use Phpfastcache\Exceptions\{ |
21
|
|
|
phpFastCacheDeprecatedException, phpFastCacheDriverCheckException, phpFastCacheDriverNotFoundException, phpFastCacheInstanceNotFoundException, phpFastCacheInvalidArgumentException, phpFastCacheInvalidConfigurationException |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class CacheManager |
26
|
|
|
* @package phpFastCache |
27
|
|
|
* |
28
|
|
|
* @method static ExtendedCacheItemPoolInterface Apc() Apc($config = []) Return a driver "Apc" instance |
29
|
|
|
* @method static ExtendedCacheItemPoolInterface Apcu() Apcu($config = []) Return a driver "Apcu" instance |
30
|
|
|
* @method static ExtendedCacheItemPoolInterface Cassandra() Cassandra($config = []) Return a driver "Cassandra" instance |
31
|
|
|
* @method static ExtendedCacheItemPoolInterface Cookie() Cookie($config = []) Return a driver "Cookie" instance |
32
|
|
|
* @method static ExtendedCacheItemPoolInterface Couchbase() Couchbase($config = []) Return a driver "Couchbase" instance |
33
|
|
|
* @method static ExtendedCacheItemPoolInterface Couchdb() Couchdb($config = []) Return a driver "Couchdb" instance |
34
|
|
|
* @method static ExtendedCacheItemPoolInterface Devnull() Devnull($config = []) Return a driver "Devnull" instance |
35
|
|
|
* @method static ExtendedCacheItemPoolInterface Files() Files($config = []) Return a driver "files" instance |
36
|
|
|
* @method static ExtendedCacheItemPoolInterface Leveldb() Leveldb($config = []) Return a driver "Leveldb" instance |
37
|
|
|
* @method static ExtendedCacheItemPoolInterface Memcache() Memcache($config = []) Return a driver "Memcache" instance |
38
|
|
|
* @method static ExtendedCacheItemPoolInterface Memcached() Memcached($config = []) Return a driver "Memcached" instance |
39
|
|
|
* @method static ExtendedCacheItemPoolInterface Memstatic() Memstatic($config = []) Return a driver "Memstatic" instance |
40
|
|
|
* @method static ExtendedCacheItemPoolInterface Mongodb() Mongodb($config = []) Return a driver "Mongodb" instance |
41
|
|
|
* @method static ExtendedCacheItemPoolInterface Predis() Predis($config = []) Return a driver "Predis" instance |
42
|
|
|
* @method static ExtendedCacheItemPoolInterface Redis() Redis($config = []) Return a driver "Pedis" instance |
43
|
|
|
* @method static ExtendedCacheItemPoolInterface Riak() Riak($config = []) Return a driver "Riak" instance |
44
|
|
|
* @method static ExtendedCacheItemPoolInterface Sqlite() Sqlite($config = []) Return a driver "Sqlite" instance |
45
|
|
|
* @method static ExtendedCacheItemPoolInterface Ssdb() Ssdb($config = []) Return a driver "Ssdb" instance |
46
|
|
|
* @method static ExtendedCacheItemPoolInterface Wincache() Wincache($config = []) Return a driver "Wincache" instance |
47
|
|
|
* @method static ExtendedCacheItemPoolInterface Xcache() Xcache($config = []) Return a driver "Xcache" instance |
48
|
|
|
* @method static ExtendedCacheItemPoolInterface Zenddisk() Zenddisk($config = []) Return a driver "Zend disk cache" instance |
49
|
|
|
* @method static ExtendedCacheItemPoolInterface Zendshm() Zendshm($config = []) Return a driver "Zend memory cache" instance |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
class CacheManager |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* @var ConfigurationOption |
56
|
|
|
*/ |
57
|
|
|
protected static $config; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
public static $ReadHits = 0; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var int |
66
|
|
|
*/ |
67
|
|
|
public static $WriteHits = 0; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected static $namespacePath; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var ExtendedCacheItemPoolInterface[] |
76
|
|
|
*/ |
77
|
|
|
protected static $instances = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $driver |
81
|
|
|
* @param array|ConfigurationOption $config |
82
|
|
|
* @param string $instanceId |
83
|
|
|
* |
84
|
|
|
* @return ExtendedCacheItemPoolInterface |
85
|
|
|
* |
86
|
|
|
* @throws phpFastCacheDriverCheckException |
87
|
|
|
* @throws phpFastCacheInvalidConfigurationException |
88
|
|
|
* @throws phpFastCacheDriverNotFoundException |
89
|
|
|
* @throws phpFastCacheInvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
public static function getInstance($driver = 'auto', $config = null, $instanceId = null) |
92
|
|
|
{ |
93
|
|
|
static $badPracticeOmeter = []; |
94
|
|
|
|
95
|
|
|
if ($instanceId !== null && !\is_string($instanceId)) { |
|
|
|
|
96
|
|
|
throw new phpFastCacheInvalidArgumentException('The Instance ID must be a string'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (is_array($config)) { |
100
|
|
|
$config = new ConfigurationOption($config); |
101
|
|
|
trigger_error( |
102
|
|
|
'The CacheManager will drops the support of primitive configuration arrays, use a "\Phpfastcache\Config\ConfigurationOption" object instead', |
103
|
|
|
E_USER_DEPRECATED |
104
|
|
|
); |
105
|
|
|
}elseif ($config === null){ |
106
|
|
|
$config = self::getDefaultConfig(); |
107
|
|
|
}else if(!($config instanceof ConfigurationOption)){ |
|
|
|
|
108
|
|
|
throw new phpFastCacheInvalidArgumentException(sprintf('Unsupported config type: %s', gettype($config))); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$driver = self::standardizeDriverName($driver); |
112
|
|
|
|
113
|
|
|
if (!$driver || $driver === 'Auto') { |
114
|
|
|
$driver = self::getAutoClass($config); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$instance = $instanceId ?: md5($driver . \serialize($config->toArray())); |
118
|
|
|
|
119
|
|
|
if (!isset(self::$instances[ $instance ])) { |
120
|
|
|
$badPracticeOmeter[ $driver ] = 1; |
121
|
|
|
$driverClass = self::getNamespacePath() . $driver . '\Driver'; |
122
|
|
|
try { |
123
|
|
|
if (\class_exists($driverClass)) { |
124
|
|
|
$configClass = $driverClass::getConfigClass(); |
125
|
|
|
self::$instances[ $instance ] = new $driverClass(new $configClass($config->toArray()), $instance); |
126
|
|
|
self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
127
|
|
|
} else { |
128
|
|
|
throw new phpFastCacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver)); |
129
|
|
|
} |
130
|
|
|
} catch (phpFastCacheDriverCheckException $e) { |
131
|
|
|
if ($config->getFallback()) { |
132
|
|
|
try { |
133
|
|
|
|
134
|
|
|
$fallback = $config->getFallback(); |
135
|
|
|
$config->setFallback(''); |
136
|
|
|
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); |
137
|
|
|
return self::getInstance($fallback, $config); |
138
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
139
|
|
|
throw new phpFastCacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e); |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} else if ($badPracticeOmeter[ $driver ] >= 2) { |
146
|
|
|
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
147
|
|
|
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$badPracticeOmeter[ $driver ]++; |
151
|
|
|
|
152
|
|
|
return self::$instances[ $instance ]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $instanceId |
157
|
|
|
* |
158
|
|
|
* @return ExtendedCacheItemPoolInterface |
159
|
|
|
* |
160
|
|
|
* @throws phpFastCacheInvalidArgumentException |
161
|
|
|
* @throws phpFastCacheInstanceNotFoundException |
162
|
|
|
*/ |
163
|
|
|
public static function getInstanceById($instanceId): ExtendedCacheItemPoolInterface |
164
|
|
|
{ |
165
|
|
|
if ($instanceId !== null && !\is_string($instanceId)) { |
|
|
|
|
166
|
|
|
throw new phpFastCacheInvalidArgumentException('The Instance ID must be a string'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (isset(self::$instances[ $instanceId ])) { |
170
|
|
|
return self::$instances[ $instanceId ]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
throw new phpFastCacheInstanceNotFoundException(sprintf('Instance ID %s not found', $instanceId)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* This method is intended for internal |
178
|
|
|
* use only and should not be used for |
179
|
|
|
* any external development use the |
180
|
|
|
* getInstances() method instead |
181
|
|
|
* |
182
|
|
|
* @internal |
183
|
|
|
* @return ExtendedCacheItemPoolInterface[] |
184
|
|
|
*/ |
185
|
|
|
public static function getInstances(): array |
186
|
|
|
{ |
187
|
|
|
return self::$instances; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* This method is intended for internal |
192
|
|
|
* use only and should not be used for |
193
|
|
|
* any external development use the |
194
|
|
|
* getInstances() method instead |
195
|
|
|
* |
196
|
|
|
* @todo Use a proper way to passe them as a reference ? |
197
|
|
|
* @internal |
198
|
|
|
* @return ExtendedCacheItemPoolInterface[] |
199
|
|
|
*/ |
200
|
|
|
public static function &getInternalInstances(): array |
201
|
|
|
{ |
202
|
|
|
return self::$instances; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @todo Does we really keep it ?? |
207
|
|
|
* @param $config |
208
|
|
|
* @return string |
209
|
|
|
* @throws phpFastCacheDriverCheckException |
210
|
|
|
*/ |
211
|
|
|
public static function getAutoClass(array $config = []) |
212
|
|
|
{ |
213
|
|
|
static $autoDriver; |
214
|
|
|
|
215
|
|
|
if ($autoDriver === null) { |
216
|
|
|
foreach (self::getStaticSystemDrivers() as $driver) { |
217
|
|
|
try { |
218
|
|
|
self::getInstance($driver, $config); |
219
|
|
|
$autoDriver = $driver; |
220
|
|
|
break; |
221
|
|
|
} catch (phpFastCacheDriverCheckException $e) { |
222
|
|
|
continue; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $autoDriver; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $name |
232
|
|
|
* @param array $arguments |
233
|
|
|
* @return \Psr\Cache\CacheItemPoolInterface |
234
|
|
|
*/ |
235
|
|
|
public static function __callStatic($name, $arguments) |
236
|
|
|
{ |
237
|
|
|
$options = (\array_key_exists(0, $arguments) && \is_array($arguments) ? $arguments[ 0 ] : []); |
238
|
|
|
|
239
|
|
|
return self::getInstance($name, $options); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
public static function clearInstances() |
246
|
|
|
{ |
247
|
|
|
self::$instances = []; |
248
|
|
|
|
249
|
|
|
gc_collect_cycles(); |
250
|
|
|
return !\count(self::$instances); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public static function getNamespacePath() |
257
|
|
|
{ |
258
|
|
|
return self::$namespacePath ?: __NAMESPACE__ . '\Drivers\\'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $path |
263
|
|
|
*/ |
264
|
|
|
public static function setNamespacePath($path) |
265
|
|
|
{ |
266
|
|
|
self::$namespacePath = \trim($path, "\\") . '\\'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param ConfigurationOption $config |
271
|
|
|
*/ |
272
|
|
|
public static function setDefaultConfig(ConfigurationOption $config) |
273
|
|
|
{ |
274
|
|
|
self::$config = $config; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return ConfigurationOption |
279
|
|
|
*/ |
280
|
|
|
public static function getDefaultConfig(): ConfigurationOption |
281
|
|
|
{ |
282
|
|
|
return self::$config ?: self::$config = new ConfigurationOption(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
public static function getStaticSystemDrivers() |
289
|
|
|
{ |
290
|
|
|
/** |
291
|
|
|
* @todo Reflection reader |
292
|
|
|
*/ |
293
|
|
|
return [ |
294
|
|
|
'Apc', |
295
|
|
|
'Apcu', |
296
|
|
|
'Cassandra', |
297
|
|
|
'Couchbase', |
298
|
|
|
'Couchdb', |
299
|
|
|
'Devnull', |
300
|
|
|
'Files', |
301
|
|
|
'Leveldb', |
302
|
|
|
'Memcache', |
303
|
|
|
'Memcached', |
304
|
|
|
'Memstatic', |
305
|
|
|
'Mongodb', |
306
|
|
|
'Predis', |
307
|
|
|
'Redis', |
308
|
|
|
'Riak', |
309
|
|
|
'Ssdb', |
310
|
|
|
'Sqlite', |
311
|
|
|
'Wincache', |
312
|
|
|
'Xcache', |
313
|
|
|
'Zenddisk', |
314
|
|
|
'Zendshm', |
315
|
|
|
]; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public static function getStaticAllDrivers() |
322
|
|
|
{ |
323
|
|
|
/** |
324
|
|
|
* @todo Reflection reader |
325
|
|
|
*/ |
326
|
|
|
return \array_merge(self::getStaticSystemDrivers(), [ |
327
|
|
|
'Devtrue', |
328
|
|
|
'Devfalse', |
329
|
|
|
'Cookie', |
330
|
|
|
]); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string $driverName |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
|
|
public static function standardizeDriverName(string $driverName): string |
338
|
|
|
{ |
339
|
|
|
return \ucfirst(\strtolower(\trim($driverName))); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|