1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Koch Framework |
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
6
|
|
|
* |
7
|
|
|
* This file is part of "Koch Framework". |
8
|
|
|
* |
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
10
|
|
|
* |
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Koch\Cache\Adapter; |
26
|
|
|
|
27
|
|
|
use Koch\Cache\AbstractCache; |
28
|
|
|
use Koch\Cache\CacheInterface; |
29
|
|
|
use Koch\Functions\Functions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Koch Framework - Cache Handler for APC (Alternative PHP Cache). |
33
|
|
|
* |
34
|
|
|
* APC is a) an opcache and b) a memory based cache. |
35
|
|
|
* |
36
|
|
|
* Use this class only on PHP versions below PHP 5.5.0. |
37
|
|
|
* |
38
|
|
|
* As of PHP 5.5.0 using APC is no longer required, because |
39
|
|
|
* PHP ships an Opcode Cache (formerly Zend Optimizer+) by default. |
40
|
|
|
* For userland caching APCu is in the making (APC User Cache). |
41
|
|
|
* |
42
|
|
|
* @link http://de3.php.net/manual/de/ref.apc.php |
43
|
|
|
*/ |
44
|
|
|
class Apc extends AbstractCache implements CacheInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param array $options |
50
|
|
|
*/ |
51
|
|
|
public function __construct($options = []) |
52
|
|
|
{ |
53
|
|
|
if (extension_loaded('apc') === false) { |
54
|
|
|
throw new \Koch\Exception\Exception( |
55
|
|
|
'The PHP extension APC (Alternative PHP Cache) is not loaded. You may enable it in "php.ini"!' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$enabled = ini_get('apc.enabled'); |
60
|
|
|
if (PHP_SAPI === 'cli') { |
61
|
|
|
$enabled = $enabled && (bool) ini_get('apc.enable_cli'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($enabled === false) { |
65
|
|
|
throw new \Koch\Exception\Exception( |
66
|
|
|
'The PHP extension APC (Alternative PHP Cache) is not loaded.' . |
67
|
|
|
"You may enable it with 'apc.enabled=1' and 'apc.enable_cli=1'!" |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
parent::__construct($options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Read a key from the cache. |
76
|
|
|
* |
77
|
|
|
* @param string $key Identifier for the data |
78
|
|
|
* |
79
|
|
|
* @return bool True if the data was successfully fetched from the cache, false on failure |
80
|
|
|
*/ |
81
|
|
|
public function fetch($key) |
82
|
|
|
{ |
83
|
|
|
return apc_fetch($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Stores data by key into cache. |
88
|
|
|
* |
89
|
|
|
* @param string $key Identifier for the data |
90
|
|
|
* @param string $data Data to be cached |
91
|
|
|
* @param int $ttl How long to cache the data, in minutes. |
|
|
|
|
92
|
|
|
* @param bool $overwrite If overwrite true, key will be overwritten. |
93
|
|
|
* |
94
|
|
|
* @return bool True if the data was successfully cached, false on failure |
95
|
|
|
*/ |
96
|
|
|
public function store($key, $data, $ttl = null, $overwrite = false) |
97
|
|
|
{ |
98
|
|
|
if (null === $ttl) { |
99
|
|
|
$ttl = $this->options['ttl']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($key === null) { |
103
|
|
|
return false; |
104
|
|
|
} elseif ($overwrite === false) { |
105
|
|
|
return apc_add($key, $data, $ttl); |
106
|
|
|
} else { // overwrite |
107
|
|
|
|
108
|
|
|
return apc_store($key, $data, $ttl); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Removes a stored variable from the. |
114
|
|
|
* |
115
|
|
|
* @link http://php.net/manual/en/function.apc-delete.php |
116
|
|
|
* |
117
|
|
|
* @param string $keys Identifier for the data |
118
|
|
|
* |
119
|
|
|
* @return bool Returns true on success or false on failure. |
120
|
|
|
*/ |
121
|
|
|
public function delete($keys) |
122
|
|
|
{ |
123
|
|
|
$keys = (array) $keys; |
124
|
|
|
$keys_deleted = 0; |
|
|
|
|
125
|
|
|
|
126
|
|
|
foreach ($keys as $key) { |
127
|
|
|
if (true === apc_delete($key)) { |
128
|
|
|
++$keys_deleted; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return (bool) $keys_deleted; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Clears the APC cache. |
137
|
|
|
* |
138
|
|
|
* @link http://php.net/manual/en/function.apc-clear-cache.php |
139
|
|
|
* |
140
|
|
|
* @param string $cache_type [optional] all, user, opcode <p> |
141
|
|
|
* If cache_type is "user", the user cache will be cleared; |
142
|
|
|
* otherwise, the system cache (cached files) will be cleared. </p> |
143
|
|
|
* |
144
|
|
|
* @return bool Returns true on success or false on failure. |
145
|
|
|
*/ |
146
|
|
|
public function clear($cache_type = 'user') |
147
|
|
|
{ |
148
|
|
|
if (extension_loaded('apcu')) { |
149
|
|
|
return apc_clear_cache(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($cache_type === 'all') { |
|
|
|
|
153
|
|
|
apc_clear_cache(); |
154
|
|
|
apc_clear_cache('user'); |
155
|
|
|
apc_clear_cache('opcode'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return apc_clear_cache($cache_type); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Checks if a key exists in the cache. |
163
|
|
|
* |
164
|
|
|
* @param string $key Identifier for the data |
165
|
|
|
* |
166
|
|
|
* @return bool true|false |
167
|
|
|
*/ |
168
|
|
|
public function contains($key) |
169
|
|
|
{ |
170
|
|
|
return apc_exists($key); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get stats and usage Informations for display from APC |
175
|
|
|
* 1. Shared Memory Allocation |
176
|
|
|
* 2. Cache Infos / Meta-Data. |
177
|
|
|
*/ |
178
|
|
|
public function stats() |
179
|
|
|
{ |
180
|
|
|
$info = []; |
181
|
|
|
// Retrieve APC Version |
182
|
|
|
$info['version'] = phpversion('apc'); |
183
|
|
|
$info['phpversion'] = phpversion(); |
184
|
|
|
|
185
|
|
|
/* |
186
|
|
|
* ======================================================== |
187
|
|
|
* Retrieves APC's Shared Memory Allocation information |
188
|
|
|
* ======================================================== |
189
|
|
|
*/ |
190
|
|
|
$info['sma_info'] = []; |
191
|
|
|
|
192
|
|
|
if (true === function_exists('apc_sma_info')) { |
193
|
|
|
$info['sma_info'] = apc_sma_info(true); |
194
|
|
|
|
195
|
|
|
// Calculate "APC Memory Size" (Number of Segments * Size of Segment) |
196
|
|
|
$memsize = $info['sma_info']['num_seg'] * $info['sma_info']['seg_size']; |
197
|
|
|
$info['sma_info']['mem_size'] = $memsize; |
198
|
|
|
|
199
|
|
|
// Calculate "APC Memory Usage" ( mem_size - avail_mem ) |
200
|
|
|
$memusage = $info['sma_info']['mem_size'] - $info['sma_info']['avail_mem']; |
201
|
|
|
$info['sma_info']['mem_used'] = $memusage; |
202
|
|
|
|
203
|
|
|
// Calculate "APC Free Memory Percentage" ( mem_size*100/mem_used ) |
204
|
|
|
$memsize_total = $info['sma_info']['avail_mem'] * 100; |
|
|
|
|
205
|
|
|
$avail_mem_percent = sprintf('(%.1f%%)', $memsize_total / $info['sma_info']['mem_size']); |
|
|
|
|
206
|
|
|
$info['sma_info']['mem_avail_percentage'] = $avail_mem_percent; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (true === function_exists('apc_cache_info')) { |
210
|
|
|
|
211
|
|
|
// Retrieves cached information and meta-data from APC's data store |
212
|
|
|
$info['cache_info'] = apc_cache_info(); |
213
|
|
|
|
214
|
|
|
#\Koch\Debug\Debug::printR(apc_cache_info()); |
|
|
|
|
215
|
|
|
$info['cache_info']['cached_files'] = count($info['cache_info']['cache_list']); |
216
|
|
|
$info['cache_info']['deleted_files'] = count($info['cache_info']['deleted_list']); |
217
|
|
|
|
218
|
|
|
/* |
219
|
|
|
* ======================================================== |
220
|
|
|
* System Cache Informations |
221
|
|
|
* ======================================================== |
222
|
|
|
*/ |
223
|
|
|
$info['system_cache_info'] = apc_cache_info('system', false); // set "false" for details |
224
|
|
|
// Calculate "APC Hit Rate Percentage" |
225
|
|
|
$hits = ($info['system_cache_info']['num_hits'] + $info['system_cache_info']['num_misses']); |
226
|
|
|
|
227
|
|
|
// div by zero fix |
228
|
|
|
if ($hits === 0) { |
229
|
|
|
$hits = 1; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$hit_rate_percentage = $info['system_cache_info']['num_hits'] * 100 / $hits; |
|
|
|
|
233
|
|
|
$info['system_cache_info']['hit_rate_percentage'] = sprintf('(%.1f%%)', $hit_rate_percentage); |
|
|
|
|
234
|
|
|
|
235
|
|
|
// Calculate "APC Miss Rate Percentage" |
236
|
|
|
$miss_percentage = $info['system_cache_info']['num_misses'] * 100 / $hits; |
|
|
|
|
237
|
|
|
$info['system_cache_info']['miss_rate_percentage'] = sprintf('(%.1f%%)', $miss_percentage); |
|
|
|
|
238
|
|
|
$info['system_cache_info']['files_cached'] = count($info['system_cache_info']['cache_list']); |
239
|
|
|
$info['system_cache_info']['files_deleted'] = count($info['system_cache_info']['deleted_list']); |
240
|
|
|
|
241
|
|
|
// Request Rate (hits, misses) / cache requests/second |
242
|
|
|
$start_time = (time() - $info['system_cache_info']['start_time']); |
|
|
|
|
243
|
|
|
|
244
|
|
|
// div by zero fix |
245
|
|
|
if ($start_time === 0) { |
|
|
|
|
246
|
|
|
$start_time = 1; |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$rate = (($info['system_cache_info']['num_hits'] + $info['system_cache_info']['num_misses']) / $start_time); |
|
|
|
|
250
|
|
|
$info['system_cache_info']['req_rate'] = sprintf('%.2f', $rate); |
251
|
|
|
|
252
|
|
|
$hit_rate = ($info['system_cache_info']['num_hits']) / $start_time; |
|
|
|
|
253
|
|
|
$info['system_cache_info']['hit_rate'] = sprintf('%.2f', $hit_rate); |
|
|
|
|
254
|
|
|
|
255
|
|
|
$miss_rate = ($info['system_cache_info']['num_misses'] / $start_time); |
|
|
|
|
256
|
|
|
$info['system_cache_info']['miss_rate'] = sprintf('%.2f', $miss_rate); |
|
|
|
|
257
|
|
|
|
258
|
|
|
$insert_rate = (($info['system_cache_info']['num_inserts']) / $start_time); |
|
|
|
|
259
|
|
|
$info['system_cache_info']['insert_rate'] = sprintf('%.2f', $insert_rate); |
|
|
|
|
260
|
|
|
|
261
|
|
|
// size |
262
|
|
|
if (isset($info['system_cache_info']['mem_size']) and $info['system_cache_info']['mem_size'] > 0) { |
|
|
|
|
263
|
|
|
$info['system_cache_info']['size_files'] = Functions::getSize($info['system_cache_info']['mem_size']); |
264
|
|
|
} else { |
265
|
|
|
$info['system_cache_info']['size_files'] = 0; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$info['settings'] = ini_get_all('apc'); |
270
|
|
|
|
271
|
|
|
/* |
272
|
|
|
* ini_get_all array mod: for each accessvalue |
273
|
|
|
* add the name of the PHP ACCESS CONSTANTS as 'accessname' |
274
|
|
|
* @todo: cleanup? |
275
|
|
|
*/ |
276
|
|
|
foreach ($info['settings'] as $key => $value) { |
277
|
|
|
foreach ($value as $key2 => $value2) { |
278
|
|
|
if ($key2 === 'access') { |
279
|
|
|
$name = ''; |
280
|
|
|
|
281
|
|
|
// accessvalue => constantname |
282
|
|
|
if ($value2 === '1') { |
283
|
|
|
$name = 'PHP_INI_USER'; |
284
|
|
|
} |
285
|
|
|
if ($value2 === '2') { |
286
|
|
|
$name = 'PHP_INI_PERDIR'; |
287
|
|
|
} |
288
|
|
|
if ($value2 === '4') { |
289
|
|
|
$name = 'PHP_INI_SYSTEM'; |
290
|
|
|
} |
291
|
|
|
if ($value2 === '7') { |
292
|
|
|
$name = 'PHP_INI_ALL'; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// add accessname to the original array |
296
|
|
|
$info['settings'][$key]['accessname'] = $name; |
297
|
|
|
unset($name); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
#$info['sma_info']['size_vars'] = Functions::getsize($cache_user['mem_size']); |
|
|
|
|
303
|
|
|
|
304
|
|
|
return $info; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.