1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Provides caching functionality for APC and APCu |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Cache |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\cache; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Provides caching functionality for APC and APCu |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Cache |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
class Driver_APC extends Base |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Creates the cache handler object |
33
|
|
|
* |
34
|
|
|
* @param array $config Configuration details as an array |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
* |
38
|
|
|
* @return \csphere\core\cache\Driver_APC |
39
|
|
|
**/ |
|
|
|
|
40
|
|
|
|
41
|
|
|
public function __construct(array $config) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($config); |
44
|
|
|
|
45
|
|
|
if (!function_exists('apc_exists')) { |
46
|
|
|
|
47
|
|
|
throw new \Exception('Extension "apc" outdated or not found'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Clears the cache content |
53
|
|
|
* |
54
|
|
|
* @return boolean |
55
|
|
|
**/ |
|
|
|
|
56
|
|
|
|
57
|
|
|
public function clear() |
58
|
|
|
{ |
59
|
|
|
// Workaround for APCu version below 4.0.2 |
60
|
|
|
$reflection = new \ReflectionFunction('apc_clear_cache'); |
61
|
|
|
$parameters = $reflection->getParameters(); |
62
|
|
|
|
63
|
|
|
if ($parameters[0]->getName() == 'info') { |
|
|
|
|
64
|
|
|
|
65
|
|
|
apc_clear_cache(); |
66
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
|
69
|
|
|
apc_clear_cache('user'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Removes a cached key |
77
|
|
|
* |
78
|
|
|
* @param string $key Name of the key |
79
|
|
|
* @param int $ttl Time to life used for the key |
80
|
|
|
* |
81
|
|
|
* @return boolean |
82
|
|
|
**/ |
|
|
|
|
83
|
|
|
|
84
|
|
|
public function delete($key, $ttl = 0) |
85
|
|
|
{ |
86
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
87
|
|
|
|
88
|
|
|
if (apc_exists($token)) { |
89
|
|
|
|
90
|
|
|
apc_delete($token); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a formatted array with statistics |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
**/ |
|
|
|
|
101
|
|
|
|
102
|
|
|
public function info() |
103
|
|
|
{ |
104
|
|
|
$info = parent::info(); |
105
|
|
|
|
106
|
|
|
$stats = apc_cache_info('user'); |
107
|
|
|
$keys = isset($stats['nentries']) ? $stats['nentries'] : ''; |
108
|
|
|
|
109
|
|
|
if (empty($keys) && isset ($stats['cache_list'])) { |
|
|
|
|
110
|
|
|
|
111
|
|
|
$keys = count($stats['cache_list']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$info['version'] = phpversion('apc'); |
115
|
|
|
$info['client'] = ''; |
116
|
|
|
$info['server'] = ''; |
117
|
|
|
$info['keys'] = $keys; |
118
|
|
|
|
119
|
|
|
// Check for apcu |
120
|
|
|
if (extension_loaded('apcu')) { |
121
|
|
|
|
122
|
|
|
$info['client'] = 'apcu ' . phpversion('apcu'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $info; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns a formatted array with all keys and additional information |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
**/ |
|
|
|
|
133
|
|
|
|
134
|
|
|
public function keys() |
135
|
|
|
{ |
136
|
|
|
$form = []; |
137
|
|
|
|
138
|
|
|
$info = apc_cache_info('user'); |
139
|
|
|
$list = isset($info['cache_list']) ? $info['cache_list'] : []; |
140
|
|
|
|
141
|
|
|
foreach ($list AS $num => $data) { |
142
|
|
|
|
143
|
|
|
// Key is named "key" in APCu, but "info" in APC |
144
|
|
|
$key = isset($data['key']) ? $data['key'] : $data['info']; |
145
|
|
|
$handle = $key . ' (' . $num . ')'; |
146
|
|
|
|
147
|
|
|
$form[$handle] = ['name' => $handle, 'time' => $data['mtime'], |
148
|
|
|
'size' => $data['mem_size']]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
ksort($form); |
152
|
|
|
|
153
|
|
|
return array_values($form); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Fetches the desired key |
158
|
|
|
* |
159
|
|
|
* @param string $key Name of the key |
160
|
|
|
* @param int $ttl Time to life used for the key |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
**/ |
|
|
|
|
164
|
|
|
|
165
|
|
|
public function load($key, $ttl = 0) |
166
|
|
|
{ |
167
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
168
|
|
|
|
169
|
|
|
if (apc_exists($token)) { |
170
|
|
|
|
171
|
|
|
return apc_fetch($token); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return false; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Stores the key with its value in the cache |
179
|
|
|
* |
180
|
|
|
* @param string $key Name of the key |
181
|
|
|
* @param array $value Content to be stored |
182
|
|
|
* @param int $ttl Time to life used for the key |
183
|
|
|
* |
184
|
|
|
* @return boolean |
185
|
|
|
**/ |
|
|
|
|
186
|
|
|
|
187
|
|
|
public function save($key, $value, $ttl = 0) |
188
|
|
|
{ |
189
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
190
|
|
|
|
191
|
|
|
apc_store($token, $value, $ttl); |
192
|
|
|
|
193
|
|
|
$this->log($key); |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.