|
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
|
|
|
|
|
15
|
|
|
namespace phpFastCache; |
|
16
|
|
|
|
|
17
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
|
18
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CacheManager |
|
22
|
|
|
* @package phpFastCache |
|
23
|
|
|
* |
|
24
|
|
|
* @method static ExtendedCacheItemPoolInterface Apc() Apc($config = []) Return a driver "apc" instance |
|
25
|
|
|
* @method static ExtendedCacheItemPoolInterface Apcu() Apcu($config = []) Return a driver "apcu" instance |
|
26
|
|
|
* @method static ExtendedCacheItemPoolInterface Cookie() Cookie($config = []) Return a driver "cookie" instance |
|
27
|
|
|
* @method static ExtendedCacheItemPoolInterface Couchbase() Couchbase($config = []) Return a driver "couchbase" instance |
|
28
|
|
|
* @method static ExtendedCacheItemPoolInterface Files() Files($config = []) Return a driver "files" instance |
|
29
|
|
|
* @method static ExtendedCacheItemPoolInterface Leveldb() Leveldb($config = []) Return a driver "leveldb" instance |
|
30
|
|
|
* @method static ExtendedCacheItemPoolInterface Memcache() Memcache($config = []) Return a driver "memcache" instance |
|
31
|
|
|
* @method static ExtendedCacheItemPoolInterface Memcached() Memcached($config = []) Return a driver "memcached" instance |
|
32
|
|
|
* @method static ExtendedCacheItemPoolInterface Mongodb() Mongodb($config = []) Return a driver "mongodb" instance |
|
33
|
|
|
* @method static ExtendedCacheItemPoolInterface Predis() Predis($config = []) Return a driver "predis" instance |
|
34
|
|
|
* @method static ExtendedCacheItemPoolInterface Redis() Redis($config = []) Return a driver "redis" instance |
|
35
|
|
|
* @method static ExtendedCacheItemPoolInterface Sqlite() Sqlite($config = []) Return a driver "sqlite" instance |
|
36
|
|
|
* @method static ExtendedCacheItemPoolInterface Ssdb() Ssdb($config = []) Return a driver "ssdb" instance |
|
37
|
|
|
* @method static ExtendedCacheItemPoolInterface Wincache() Wincache($config = []) Return a driver "wincache" instance |
|
38
|
|
|
* @method static ExtendedCacheItemPoolInterface Xcache() Xcache($config = []) Return a driver "xcache" instance |
|
39
|
|
|
* @method static ExtendedCacheItemPoolInterface Zenddisk() Zenddisk($config = []) Return a driver "zend disk cache" instance |
|
40
|
|
|
* @method static ExtendedCacheItemPoolInterface Zendshm() Zendshm($config = []) Return a driver "zend memory cache" instance |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
class CacheManager |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
public static $ReadHits = 0; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
public static $WriteHits = 0; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var ExtendedCacheItemPoolInterface[] |
|
57
|
|
|
*/ |
|
58
|
|
|
protected static $config = [ |
|
59
|
|
|
'itemDetailedDate' => false,// Specify if the item must provide detailed creation/modification dates |
|
60
|
|
|
'autoTmpFallback' => false,// Automatically attempt to fallback to temporary directory if the cache fails to write on the specified directory |
|
61
|
|
|
'secureFileManipulation' => false,// Provide a secure file manipulation mechanism, on intensive usage the performance can be affected. |
|
62
|
|
|
'ignoreSymfonyNotice' => false,// Ignore Symfony notice for Symfony project which do not makes use of PhpFastCache's Symfony Bundle |
|
63
|
|
|
'defaultTtl' => 900,// Default time-to-live in second |
|
64
|
|
|
'securityKey' => 'auto',// The securityKey that will be used to create sub-directory |
|
65
|
|
|
'htaccess' => true,// Auto-generate .htaccess if tit is missing |
|
66
|
|
|
'default_chmod' => 0777, // 0777 recommended |
|
67
|
|
|
'path' => '',// if not set will be the value of sys_get_temp_dir() |
|
68
|
|
|
'fallback' => false, //Fall back when old driver is not support |
|
69
|
|
|
'limited_memory_each_object' => 4096, // maximum size (bytes) of object store in memory |
|
70
|
|
|
'compress_data' => false, // compress stored data, if the backend supports it |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected static $namespacePath; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var ExtendedCacheItemPoolInterface[] |
|
80
|
|
|
*/ |
|
81
|
|
|
protected static $instances = []; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $driver |
|
85
|
|
|
* @param array $config |
|
86
|
|
|
* @return ExtendedCacheItemPoolInterface |
|
87
|
|
|
* @throws phpFastCacheDriverCheckException |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function getInstance($driver = 'auto', $config = []) |
|
90
|
|
|
{ |
|
91
|
|
|
static $badPracticeOmeter = []; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @todo: Standardize a method for driver name |
|
95
|
|
|
*/ |
|
96
|
|
|
$driver = self::standardizeDriverName($driver); |
|
97
|
|
|
$config = array_merge(self::$config, $config); |
|
98
|
|
|
if (!$driver || $driver === 'Auto') { |
|
99
|
|
|
$driver = self::getAutoClass($config); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$instance = crc32($driver . serialize($config)); |
|
103
|
|
|
if (!isset(self::$instances[ $instance ])) { |
|
104
|
|
|
$badPracticeOmeter[$driver] = 1; |
|
105
|
|
|
if(!$config['ignoreSymfonyNotice'] && interface_exists('Symfony\Component\HttpKernel\KernelInterface') && !class_exists('phpFastCache\Bundle\phpFastCacheBundle')){ |
|
106
|
|
|
trigger_error('A Symfony Bundle to make the PhpFastCache integration more easier is now available here: https://github.com/PHPSocialNetwork/phpfastcache-bundle', E_USER_NOTICE); |
|
107
|
|
|
} |
|
108
|
|
|
$class = self::getNamespacePath() . $driver . '\Driver'; |
|
109
|
|
|
try{ |
|
110
|
|
|
self::$instances[ $instance ] = new $class($config); |
|
111
|
|
|
self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
|
112
|
|
|
}catch(phpFastCacheDriverCheckException $e){ |
|
113
|
|
|
$fallback = self::standardizeDriverName($config['fallback']); |
|
114
|
|
|
if($fallback && $fallback !== $driver){ |
|
115
|
|
|
$class = self::getNamespacePath() . $fallback . '\Driver'; |
|
116
|
|
|
self::$instances[ $instance ] = new $class($config); |
|
117
|
|
|
self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
|
118
|
|
|
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); |
|
119
|
|
|
}else{ |
|
120
|
|
|
throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} else if(++$badPracticeOmeter[$driver] >= 5){ |
|
124
|
|
|
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
|
125
|
|
|
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return self::$instances[ $instance ]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* This method is intended for internal |
|
133
|
|
|
* use only and should not be used for |
|
134
|
|
|
* any external development use the |
|
135
|
|
|
* getInstances() method instead |
|
136
|
|
|
* |
|
137
|
|
|
* @internal |
|
138
|
|
|
* @return ExtendedCacheItemPoolInterface[] |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function getInstances() |
|
141
|
|
|
{ |
|
142
|
|
|
return self::$instances; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* This method is intended for internal |
|
147
|
|
|
* use only and should not be used for |
|
148
|
|
|
* any external development use the |
|
149
|
|
|
* getInstances() method instead |
|
150
|
|
|
* |
|
151
|
|
|
* @internal |
|
152
|
|
|
* @return ExtendedCacheItemPoolInterface[] |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function &getInternalInstances() |
|
155
|
|
|
{ |
|
156
|
|
|
return self::$instances; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $config |
|
161
|
|
|
* @return string |
|
162
|
|
|
* @throws phpFastCacheDriverCheckException |
|
163
|
|
|
*/ |
|
164
|
|
|
public static function getAutoClass($config = []) |
|
165
|
|
|
{ |
|
166
|
|
|
static $autoDriver; |
|
167
|
|
|
|
|
168
|
|
|
if ($autoDriver === null) { |
|
169
|
|
|
foreach (self::getStaticSystemDrivers() as $driver) { |
|
170
|
|
|
try { |
|
171
|
|
|
self::getInstance($driver, $config); |
|
172
|
|
|
$autoDriver = $driver; |
|
173
|
|
|
} catch (phpFastCacheDriverCheckException $e) { |
|
174
|
|
|
continue; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $autoDriver; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $name |
|
184
|
|
|
* @param array $arguments |
|
185
|
|
|
* @return \Psr\Cache\CacheItemPoolInterface |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function __callStatic($name, $arguments) |
|
188
|
|
|
{ |
|
189
|
|
|
$options = (array_key_exists(0, $arguments) && is_array($arguments) ? $arguments[ 0 ] : []); |
|
190
|
|
|
|
|
191
|
|
|
return self::getInstance($name, $options); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return bool |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function clearInstances() |
|
198
|
|
|
{ |
|
199
|
|
|
self::$instances = []; |
|
200
|
|
|
|
|
201
|
|
|
gc_collect_cycles(); |
|
202
|
|
|
return !count(self::$instances); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function getNamespacePath() |
|
209
|
|
|
{ |
|
210
|
|
|
return self::$namespacePath ?: __NAMESPACE__ . '\Drivers\\'; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param string $path |
|
215
|
|
|
*/ |
|
216
|
|
|
public static function setNamespacePath($path) |
|
217
|
|
|
{ |
|
218
|
|
|
self::$namespacePath = $path; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $name string|array |
|
223
|
|
|
* @param mixed $value |
|
224
|
|
|
* @throws \InvalidArgumentException |
|
225
|
|
|
*/ |
|
226
|
|
|
public static function setDefaultConfig($name, $value = null) |
|
227
|
|
|
{ |
|
228
|
|
|
if (is_array($name)) { |
|
229
|
|
|
self::$config = array_merge(self::$config, $name); |
|
|
|
|
|
|
230
|
|
|
} else if (is_string($name)){ |
|
231
|
|
|
self::$config[ $name ] = $value; |
|
232
|
|
|
}else{ |
|
233
|
|
|
throw new \InvalidArgumentException('Invalid variable type: $name'); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return array |
|
239
|
|
|
*/ |
|
240
|
|
|
public static function getDefaultConfig() |
|
241
|
|
|
{ |
|
242
|
|
|
return self::$config; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
public static function getStaticSystemDrivers() |
|
249
|
|
|
{ |
|
250
|
|
|
return [ |
|
251
|
|
|
'Apc', |
|
252
|
|
|
'Apcu', |
|
253
|
|
|
'Couchbase', |
|
254
|
|
|
'Devnull', |
|
255
|
|
|
'Files', |
|
256
|
|
|
'Leveldb', |
|
257
|
|
|
'Memcache', |
|
258
|
|
|
'Memcached', |
|
259
|
|
|
'Memstatic', |
|
260
|
|
|
'Mongodb', |
|
261
|
|
|
'Predis', |
|
262
|
|
|
'Redis', |
|
263
|
|
|
'Ssdb', |
|
264
|
|
|
'Sqlite', |
|
265
|
|
|
'Wincache', |
|
266
|
|
|
'Xcache', |
|
267
|
|
|
]; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @return array |
|
272
|
|
|
*/ |
|
273
|
|
|
public static function getStaticAllDrivers() |
|
274
|
|
|
{ |
|
275
|
|
|
return array_merge(self::getStaticSystemDrivers(), [ |
|
276
|
|
|
'Devtrue', |
|
277
|
|
|
'Devfalse', |
|
278
|
|
|
'Cookie', |
|
279
|
|
|
]); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param string $driverName |
|
284
|
|
|
* @return string |
|
285
|
|
|
*/ |
|
286
|
|
|
public static function standardizeDriverName($driverName) |
|
287
|
|
|
{ |
|
288
|
|
|
return ucfirst(strtolower(trim($driverName))); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..