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\Cache\ExtendedCacheItemPoolInterface; |
18
|
|
|
use phpFastCache\Core\DriverAbstract; |
19
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class CacheManager |
23
|
|
|
* @package phpFastCache |
24
|
|
|
* |
25
|
|
|
* @method static DriverAbstract Apc() Apc($config = []) Return a driver "apc" instance |
26
|
|
|
* @method static DriverAbstract Cookie() Cookie($config = []) Return a driver "cookie" instance |
27
|
|
|
* @method static DriverAbstract Files() Files($config = []) Return a driver "files" instance |
28
|
|
|
* @method static DriverAbstract Memcache() Memcache($config = []) Return a driver "memcache" instance |
29
|
|
|
* @method static DriverAbstract Memcached() Memcached($config = []) Return a driver "memcached" instance |
30
|
|
|
* @method static DriverAbstract Predis() Predis($config = []) Return a driver "predis" instance |
31
|
|
|
* @method static DriverAbstract Redis() Redis($config = []) Return a driver "redis" instance |
32
|
|
|
* @method static DriverAbstract Sqlite() Sqlite($config = []) Return a driver "sqlite" instance |
33
|
|
|
* @method static DriverAbstract Ssdb() Ssdb($config = []) Return a driver "ssdb" instance |
34
|
|
|
* @method static DriverAbstract Wincache() Wincache($config = []) Return a driver "wincache" instance |
35
|
|
|
* @method static DriverAbstract Xcache() Xcache($config = []) Return a driver "xcache" instance |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
class CacheManager |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public static $ReadHits = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
public static $WriteHits = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected static $config = [ |
54
|
|
|
'securityKey' => 'auto',// The securityKey that will be used to create sub-directory |
55
|
|
|
'htaccess' => true,// Auto-generate .htaccess if tit is missing |
56
|
|
|
'default_chmod' => 0777, // 0777 recommended |
57
|
|
|
'path' => '',// if not set will be the value of sys_get_temp_dir() |
58
|
|
|
'fallback' => false, //Fall back when old driver is not support |
59
|
|
|
'limited_memory_each_object' => 4096, // maximum size (bytes) of object store in memory |
60
|
|
|
'compress_data' => false, // compress stored data, if the backend supports it |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected static $namespacePath; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected static $instances = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $driver |
75
|
|
|
* @param array $config |
76
|
|
|
* @return ExtendedCacheItemPoolInterface |
77
|
|
|
*/ |
78
|
|
|
public static function getInstance($driver = 'auto', $config = []) |
79
|
|
|
{ |
80
|
|
|
static $badPracticeOmeter = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @todo: Standardize a method for driver name |
84
|
|
|
*/ |
85
|
|
|
$driver = self::standardizeDriverName($driver); |
86
|
|
|
$config = array_merge(self::$config, $config); |
87
|
|
|
if (!$driver || $driver === 'Auto') { |
88
|
|
|
$driver = self::getAutoClass($config); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$instance = crc32($driver . serialize($config)); |
92
|
|
|
if (!isset(self::$instances[ $instance ])) { |
93
|
|
|
$badPracticeOmeter[$driver] = 1; |
94
|
|
|
$class = self::getNamespacePath() . $driver . '\Driver'; |
95
|
|
|
try{ |
96
|
|
|
self::$instances[ $instance ] = new $class($config); |
97
|
|
|
}catch(phpFastCacheDriverCheckException $e){ |
98
|
|
|
$fallback = self::standardizeDriverName($config['fallback']); |
99
|
|
|
if($fallback && $fallback !== $driver){ |
100
|
|
|
$class = self::getNamespacePath() . $fallback . '\Driver'; |
101
|
|
|
self::$instances[ $instance ] = new $class($config); |
102
|
|
|
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); |
103
|
|
|
}else{ |
104
|
|
|
throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} else if(++$badPracticeOmeter[$driver] >= 5){ |
108
|
|
|
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
109
|
|
|
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return self::$instances[ $instance ]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $config |
117
|
|
|
* @return string |
118
|
|
|
* @throws phpFastCacheDriverCheckException |
119
|
|
|
*/ |
120
|
|
|
public static function getAutoClass($config = []) |
121
|
|
|
{ |
122
|
|
|
static $autoDriver; |
123
|
|
|
|
124
|
|
|
if ($autoDriver === null) { |
125
|
|
|
foreach (self::getStaticSystemDrivers() as $driver) { |
126
|
|
|
try { |
127
|
|
|
self::getInstance($driver, $config); |
128
|
|
|
$autoDriver = $driver; |
129
|
|
|
} catch (phpFastCacheDriverCheckException $e) { |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $autoDriver; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $name |
140
|
|
|
* @param array $arguments |
141
|
|
|
* @return \Psr\Cache\CacheItemPoolInterface |
142
|
|
|
*/ |
143
|
|
|
public static function __callStatic($name, $arguments) |
144
|
|
|
{ |
145
|
|
|
$options = (array_key_exists(0, $arguments) && is_array($arguments) ? $arguments[ 0 ] : []); |
146
|
|
|
|
147
|
|
|
return self::getInstance($name, $options); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public static function clearInstances() |
154
|
|
|
{ |
155
|
|
|
foreach (self::$instances as &$instance) { |
156
|
|
|
unset($instance); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
gc_collect_cycles(); |
160
|
|
|
return !count(self::$instances); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function getNamespacePath() |
167
|
|
|
{ |
168
|
|
|
return self::$namespacePath ?: __NAMESPACE__ . '\Drivers\\'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $path |
173
|
|
|
*/ |
174
|
|
|
public static function setNamespacePath($path) |
175
|
|
|
{ |
176
|
|
|
self::$namespacePath = $path; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $name |
181
|
|
|
* @param string $value |
182
|
|
|
* @deprecated Method "setup" is deprecated and will be removed in 5.1. Use method "setDefaultConfig" instead. |
183
|
|
|
* @throws \InvalidArgumentException |
184
|
|
|
*/ |
185
|
|
|
public static function setup($name, $value = '') |
186
|
|
|
{ |
187
|
|
|
trigger_error('Method "setup" is deprecated and will be removed in 5.1. Use method "setDefaultConfig" instead.'); |
188
|
|
|
self::setDefaultConfig($name, $value); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param $name string|array |
193
|
|
|
* @param mixed $value |
194
|
|
|
* @throws \InvalidArgumentException |
195
|
|
|
*/ |
196
|
|
|
public static function setDefaultConfig($name, $value = null) |
197
|
|
|
{ |
198
|
|
|
if (is_array($name)) { |
199
|
|
|
self::$config = array_merge(self::$config, $name); |
200
|
|
|
} else if (is_string($name)){ |
201
|
|
|
self::$config[ $name ] = $value; |
202
|
|
|
}else{ |
203
|
|
|
throw new \InvalidArgumentException('Invalid variable type: $name'); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public static function getDefaultConfig() |
211
|
|
|
{ |
212
|
|
|
return self::$config; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public static function getStaticSystemDrivers() |
219
|
|
|
{ |
220
|
|
|
return [ |
221
|
|
|
'Sqlite', |
222
|
|
|
'Files', |
223
|
|
|
'Apc', |
224
|
|
|
'Apcu', |
225
|
|
|
'Memcache', |
226
|
|
|
'Memcached', |
227
|
|
|
'Couchbase', |
228
|
|
|
'Mongodb', |
229
|
|
|
'Predis', |
230
|
|
|
'Redis', |
231
|
|
|
'Ssdb', |
232
|
|
|
'Leveldb', |
233
|
|
|
'Wincache', |
234
|
|
|
'Xcache', |
235
|
|
|
'Devnull', |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
public static function getStaticAllDrivers() |
243
|
|
|
{ |
244
|
|
|
return array_merge(self::getStaticSystemDrivers(), [ |
245
|
|
|
'Devtrue', |
246
|
|
|
'Devfalse', |
247
|
|
|
'Cookie', |
248
|
|
|
]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $driverName |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public static function standardizeDriverName($driverName) |
256
|
|
|
{ |
257
|
|
|
return ucfirst(strtolower(trim($driverName))); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|