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