1 | <?php |
||
12 | class Apc implements CacheInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $settings = array(); |
||
18 | private $isAvailable = null; |
||
19 | |||
20 | const SETTING_CACHE_TYPE = 'SETTING_CACHE_TYPE'; |
||
21 | const SETTING_CACHE_TTL = 'SETTING_CACHE_TTL'; |
||
22 | const SETTING_CACHE_PREFIX = 'SETTING_CACHE_PREFIX'; |
||
23 | |||
24 | const CACHE_TTL_DEFAULT = 0; |
||
25 | |||
26 | const CACHE_TYPE_USER = 'user'; |
||
27 | const CACHE_TYPE_FILE_HITS = 'filehits'; |
||
28 | const CACHE_TYPE_OP_CODE = 'opcode'; |
||
29 | |||
30 | /** |
||
31 | * constructor of File |
||
32 | * @param array $settings |
||
33 | */ |
||
34 | 22 | public function __construct($settings = array()) |
|
38 | |||
39 | /** |
||
40 | * Can define settings of the component |
||
41 | * @param array $settings |
||
42 | * @return $this |
||
43 | */ |
||
44 | 22 | public function setSettings($settings = array()) |
|
49 | |||
50 | /** |
||
51 | * Get defined value for specified cache key |
||
52 | * @param string $key |
||
53 | * @return mixed |
||
54 | * @throws Exception |
||
55 | */ |
||
56 | 3 | public function get($key) |
|
69 | |||
70 | /** |
||
71 | * @param string $key |
||
72 | * @param bool &$success |
||
73 | * @return mixed |
||
74 | * @codeCoverageIgnore |
||
75 | */ |
||
76 | protected function apcFetch($key, &$success) |
||
80 | |||
81 | /** |
||
82 | * Check whether key exists or not |
||
83 | * @param string $key |
||
84 | * @return bool |
||
85 | * @throws Exception |
||
86 | */ |
||
87 | 2 | public function has($key) |
|
95 | |||
96 | /** |
||
97 | * @param string $key |
||
98 | * @return bool|\string[] |
||
99 | * @codeCoverageIgnore |
||
100 | */ |
||
101 | protected function apcExists($key) |
||
105 | |||
106 | /** |
||
107 | * Delete a specified key in cache |
||
108 | * @param string $key |
||
109 | * @return $this |
||
110 | * @throws Exception |
||
111 | */ |
||
112 | 5 | public function remove($key) |
|
113 | { |
||
114 | 5 | if (!$this->isAvailable()) { |
|
115 | 1 | throw new Exception('APC is not available'); |
|
116 | } |
||
117 | 4 | $key = $this->getCacheKey($key); |
|
118 | 4 | if ($this->getCacheType() == self::CACHE_TYPE_OP_CODE) { |
|
119 | 2 | $success = $this->apcDeleteFile($key); |
|
120 | } else { |
||
121 | 2 | $success = $this->apcDelete($key); |
|
122 | } |
||
123 | 4 | if (!$success) { |
|
124 | 2 | throw new Exception('Unable to delete key from cache APC'); |
|
125 | } |
||
126 | 2 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $key |
||
131 | * @return bool|\string[] |
||
132 | * @codeCoverageIgnore |
||
133 | */ |
||
134 | protected function apcDelete($key) |
||
138 | |||
139 | /** |
||
140 | * @param string $key |
||
141 | * @return bool|\string[] |
||
142 | * @codeCoverageIgnore |
||
143 | */ |
||
144 | protected function apcDeleteFile($key) |
||
148 | |||
149 | /** |
||
150 | * Define a value in cache for a specified name |
||
151 | * @param string $key |
||
152 | * @param mixed $value |
||
153 | * @return $this |
||
154 | * @throws Exception |
||
155 | */ |
||
156 | 3 | public function set($key, $value) |
|
170 | |||
171 | /** |
||
172 | * @param string $key |
||
173 | * @param mixed $value |
||
174 | * @param int $ttl |
||
175 | * @return bool |
||
176 | * @codeCoverageIgnore |
||
177 | */ |
||
178 | protected function apcAdd($key, $value, $ttl = 0) |
||
182 | |||
183 | /** |
||
184 | * @param string|array $key |
||
185 | * @param mixed $value |
||
186 | * @param int $ttl |
||
187 | * @return array|bool |
||
188 | * @codeCoverageIgnore |
||
189 | */ |
||
190 | protected function apcStore($key, $value, $ttl = 0) |
||
194 | |||
195 | /** |
||
196 | * Clear specified cache type |
||
197 | * @return bool |
||
198 | * @throws Exception |
||
199 | */ |
||
200 | 3 | public function clear() |
|
207 | |||
208 | /** |
||
209 | * @param string $cacheType |
||
210 | * @return bool |
||
211 | * @codeCoverageIgnore |
||
212 | */ |
||
213 | protected function apcClearCache($cacheType = '') |
||
217 | |||
218 | /** |
||
219 | * Get information for specified cache type |
||
220 | * @param bool $limited (default false) If limited is TRUE, the return value will exclude the individual list |
||
221 | * of cache entries. |
||
222 | * This is useful when trying to optimize calls for statistics gathering. |
||
223 | * @return array|bool |
||
224 | * @throws Exception |
||
225 | */ |
||
226 | 3 | public function info($limited = false) |
|
233 | |||
234 | /** |
||
235 | * @param string $cacheType |
||
236 | * @param bool|false $limited |
||
237 | * @return array|bool |
||
238 | * @codeCoverageIgnore |
||
239 | */ |
||
240 | protected function apcCacheInfo($cacheType, $limited = false) |
||
244 | |||
245 | /** |
||
246 | * Define a setting |
||
247 | * @param string $name |
||
248 | * @param mixed $value |
||
249 | * @return $this |
||
250 | */ |
||
251 | 3 | public function setSetting($name, $value) |
|
256 | |||
257 | /** |
||
258 | * Retrieve a defined setting |
||
259 | * @param string $name |
||
260 | * @return mixed |
||
261 | */ |
||
262 | 13 | public function getSetting($name) |
|
266 | |||
267 | /** |
||
268 | * Get defined cache type |
||
269 | * @return string |
||
270 | */ |
||
271 | 8 | private function getCacheType() |
|
279 | |||
280 | /** |
||
281 | * Check whether apc is available |
||
282 | * @return bool |
||
283 | */ |
||
284 | 1 | public function isAvailable() |
|
285 | { |
||
286 | 1 | if (is_null($this->isAvailable)) { |
|
287 | 1 | $iniValue = ini_get('apc.enabled'); |
|
288 | 1 | $iniEnabled = ($iniValue == 1 || strtolower($iniValue) == 'on'); |
|
289 | 1 | $this->isAvailable = function_exists('apc_clear_cache') && $iniEnabled; |
|
290 | } |
||
291 | 1 | return $this->isAvailable; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Get cache key with prefix |
||
296 | * @param string $key |
||
297 | * @return string |
||
298 | */ |
||
299 | 8 | protected function getCacheKey($key) |
|
304 | } |
||
305 |