-
Notifications
You must be signed in to change notification settings - Fork 0
/
APCExtended.php
343 lines (325 loc) · 10.5 KB
/
APCExtended.php
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* APC Extended - Static class to extend APC functions
*
* @package APC Extended
* @author Nick Adams <[email protected]>
* @copyright 2012 Nick Adams.
* @link http://iamtelephone.com
* @license http://opensource.org/licenses/MIT MIT License
* @version 1.0.0
*/
namespace Telephone\Cache;
class APC
{
/**
* Check whether APC is enabled and at least v3.1.1
*
* @return boolean True if APC is enabled and >= v3.1.1
*/
public static function enabled()
{
if (ini_get('apc.enabled')
&& version_compare(phpversion('apc'), '3.1.1', '>=')
) {
return true;
}
return false;
}
/**
* Check whether APC key exists
*
* @return string True if variable exists
*/
public static function exists($key)
{
if (apc_exists($key)) {
return true;
}
return false;
}
/**
* Retrieve data/value stored in APC
* - Object is returned, unless an array was stored
*
* @param string $key Key name
* @return array|boolean|object Stored data/value on success
*/
public static function fetch($key)
{
if (self::exists($key)) {
$apc = json_decode(apc_fetch($key));
// check for array
if ($apc->type == 'array') {
$apc = json_decode(apc_fetch($key), true);
return $apc['data'];
}
return $apc->data;
}
return false;
}
/**
* Store data/value within APC
* - If $overwrite is off, the function will perfrom like apc_add() and only
* store if the key does not exist
*
* @param string $key Key name
* @param mixed $value Variable/Value to store
* @param integer $ttl Seconds until expiry
* @param boolean $overwrite If false and APC key exists funtion will not
* overwrite
* @return boolean True on success
*/
public static function store($key, $value, $ttl = 0, $overwrite = false)
{
if (self::exists($key) && !$overwrite) {
return false;
}
$apc = array('data' => $value);
($ttl !== 0) ? $apc['ttl'] = (time() + $ttl) : $apc['ttl'] = 0;
$apc['type'] = gettype($value);
return apc_store($key, json_encode($apc), $ttl);
}
/**
* Update APC key keep original TTL intact
*
* @param string $key Key name
* @param array $value New variable/value
* @return boolean|string Updated value or true on success
*/
public static function update($key, $value)
{
if (self::exists($key)) {
$apc = json_decode(apc_fetch($key));
if (strpos($value, ':') === 3) {
// increase/decrease key
(strpos($value, 'dec:') !== false)
? $apc->data -= substr($value, 4)
: $apc->data += substr($value, 4);
// return new value
if (self::store($key, $apc->data, ($apc->ttl - time()), true)) {
return $apc->data;
}
} else {
// update key
return self::store($key, $value, ($apc->ttl - time()), true);
}
}
return false;
}
/**
* Delete APC key
*
* @param string $key Key name
* @return boolean True on success
*/
public static function delete($key)
{
if (self::exists($key)) {
return apc_delete($key);
}
return false;
}
/**
* Increase APC key by defined integer (default = 1)
*
* @param string $key Key name
* @param integer $increment Integer to increase key by
* @return boolean|string Return updated value on success
*/
public static function inc($key, $increment = 1)
{
return self::update($key, 'inc:' . $increment);
}
/**
* Decrease APC key by defined integer (default = 1)
*
* @param string $key Key name
* @param integer $increment Integer to decrease key by
* @return boolean|string Return updated value on success
*/
public static function dec($key, $increment = 1)
{
return self::update($key, 'dec:' . $increment);
}
/**
* Return time till expiry in either seconds or Unix timestamp
* - Set $ttl to 'time' to return Unix timestamp
*
* @param string $key APC key
* @param string|integer $ttl Define ttl variable instead of fetching
* @return boolean|integer TTL in seconds or Unix timestamp
* - 0 = no ttl
*/
public static function ttl($key, $ttl = null)
{
if (self::exists($key)) {
$apc = json_decode(apc_fetch($key));
if ($ttl == 'time') {
// return Unix timestamp
return $apc->ttl;
}
$ttl = $apc->ttl;
} else {
return false;
}
// return seconds or 0 (integer)
return ($ttl == 0) ? 0 : ($ttl - time());
}
/**
* Fetch APC key/s in 'user' scope based on a PCRE
* (Perl Compatible Regular Expression)
*
* PHP memory can easily run out if returning the value of a lot of keys
*
* @param string $regex PCRE pattern to search for
* - E.g '/^match$/'
* @param boolean $returnValue True to return an object with
* matched key/s and values
* @return boolean|object Object with matched key/s
*/
public static function rSearch($regex, $returnValue = false)
{
$search = new \stdClass();
if ($returnValue) {
foreach (new \APCIterator('user', $regex, 34, 100, 1) as $val) {
$apc = json_decode($val['value']);
$search->$val['key'] = $apc->data;
}
} else {
foreach (new \APCIterator('user', $regex, 2, 100, 1) as $val) {
$search->$val['key'] = $val['key'];
}
}
if ($returnValue && empty($search)) {
return false;
}
return $search;
}
/**
* Delete APC key/s in 'user' scope based on a PCRE
* (Perl Compatible Regular Expression)
* - Function only removes non-expired keys
*
* @param string $regex PCRE pattern to search for. E.g '/^match$/'
* @return integer Number of keys removed
*/
public static function rDelete($regex)
{
$keys = (array) self::rSearch($regex);
$check = apc_delete($keys);
if (is_array($check)) {
if (apc_delete($keys)) {
$return = true;
}
} elseif ($check === true) {
$return = true;
}
return (isset($return)) ? count($keys) : 0;
}
/**
* Iterate through APC 'user' cache and remove expired entries
*
* @return integer Number of keys removed
*/
public static function expired()
{
// APC Iterator will not return expired. Use apc_cache_info()
$apc = apc_cache_info('user');
$time = time();
$a = 0;
foreach ($apc['cache_list'] as $val) {
if ($val['ttl'] && ($val['ttl'] + $val['creation_time']) < $time) {
if (apc_delete($val['info'])) {
$a++;
}
}
}
return $a;
}
/**
* Iterate through APC and remove unpopular files/keys
* -Function only removes non-expired files/keys
*
* apc_delete_file() will not remove expired files
* To avoid this problem, set "apc.ttl" to '0' in apc.ini
*
* @param string $type Define which cache to purge: 'user' or 'opcode'
* - Default = 'user'
* @param integer $hits Minimum number of hits. If item has been serverd
* less than $hits, delete from cache
* - Default = 10
* @return integer Number of files/keys removed
*/
public static function purge($type = 'user', $hits = 10)
{
$a = 0;
// iterate files
if ($type == 'opcode') {
$info = apc_cache_info('opcode');
foreach ($info['cache_list'] as $file) {
if ($file['num_hits'] < $hits) {
if (apc_delete_file($file['filename'])) {
$a++;
}
}
}
}
// iterate user cache
elseif ($type == 'user') {
$iterator = new \APCIterator('user', null, APC_ITER_NUM_HITS);
foreach ($iterator as $key => $val) {
if ($val['num_hits'] < $hits) {
if (apc_delete($key)) {
$a++;
}
}
}
}
return $a;
}
/**
* Clear APC cache
*
* @param string $type Define cache to clear: 'user', 'opcode', or 'all'
* - Default = 'user'
* @return boolean True on success
*/
public static function flush($type = 'user')
{
if ($type == 'user' || $type == 'opcode') {
return apc_clear_cache($type);
} elseif ($type == 'all') {
if ((apc_clear_cache('user')) && (apc_clear_cache('opcode'))) {
return true;
}
return false;
}
}
/**
* Return APCs total memory (all segments)
*
* @return string Formatted string to one decimal: '32.0 MB'
*/
public static function totalMemory()
{
$mem = apc_sma_info(true);
return sprintf('%.1f%s', ($mem['num_seg'] * $mem['seg_size'])
/ pow(1024, 2), ' MB');
}
/**
* Return the free memory within APC in either 'MB' or a percentage
*
* @param boolean $percentage True to return percentage
* @return string Formatted string to one decimal:
* - '10.0 MB' OR - '60%'
*/
public static function freeMemory($percentage = false)
{
$mem = apc_sma_info(true);
return ($percentage)
? sprintf('%.1f%s', $mem['avail_mem'] / ($mem['num_seg']
* $mem['seg_size']) * 100, '%')
: sprintf('%.1f%s', $mem['avail_mem'] / pow(1024, 2), ' MB');
}
}