1 | <?php |
||
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 = '') |
||
190 | |||
191 | /** |
||
192 | * @param $name string|array |
||
193 | * @param mixed $value |
||
194 | * @throws \InvalidArgumentException |
||
195 | */ |
||
196 | public static function setDefaultConfig($name, $value = null) |
||
206 | |||
207 | /** |
||
208 | * @return array |
||
209 | */ |
||
210 | public static function getDefaultConfig() |
||
214 | |||
215 | /** |
||
216 | * @return array |
||
217 | */ |
||
218 | public static function getStaticSystemDrivers() |
||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public static function getStaticAllDrivers() |
||
250 | |||
251 | /** |
||
252 | * @param string $driverName |
||
253 | * @return string |
||
254 | */ |
||
255 | public static function standardizeDriverName($driverName) |
||
259 | } |
||
260 |