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