forked from e-oz/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APCObject.php
420 lines (390 loc) · 10.7 KB
/
APCObject.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
namespace Jamm\Memory;
class APCObject extends MemoryObject implements IMemoryStorage
{
protected $prefix = 'K'; //because I love my wife Katya :)
protected $lock_key_prefix;
protected $defragmentation_prefix;
protected $tags_prefix;
const lock_key_prefix = '.lock_key.';
const defragmentation_prefix = '.clean.';
const tags_prefix = '.tags.';
const apc_arr_key = 'key';
const apc_arr_ctime = 'creation_time';
const apc_arr_ttl = 'ttl';
const apc_arr_value = 'value';
const apc_arr_atime = 'access_time';
/**
* @param string $prefix
*/
public function __construct($prefix = '')
{
if (!empty($prefix))
{
$this->prefix = str_replace('.', '_', $prefix).'.';
}
$this->lock_key_prefix = self::lock_key_prefix.$this->prefix;
$this->defragmentation_prefix = self::defragmentation_prefix;
$this->tags_prefix = self::tags_prefix.$this->prefix;
}
/**
* Add value to memory storage, only if this key does not exists (or false will be returned).
*
* @param string $k
* @param mixed $v
* @param int $ttl
* @param array|string $tags
* @return boolean
*/
public function add($k, $v, $ttl = 259200, $tags = NULL)
{
if (empty($k))
{
$this->ReportError('empty keys are not allowed', __LINE__);
return false;
}
$add = apc_add($this->prefix.$k, $v, intval($ttl));
if (!$add)
{
$this->ReportError('key already exists', __LINE__);
return false;
}
if (!empty($tags)) $this->set_tags($k, $tags, $ttl);
return true;
}
/**
* Associate tags with keys
* @param string $key
* @param string|array $tags
* @param int $ttl
* @return bool
*/
public function set_tags($key, $tags, $ttl = self::max_ttl)
{
if (!is_array($tags))
{
if (is_scalar($tags)) $tags = array($tags);
else $tags = array();
}
if (!empty($tags))
{
return apc_store($this->tags_prefix.$key, $tags, intval($ttl));
}
return false;
}
/**
* Save variable in memory storage
*
* @param string $k
* @param mixed $v
* @param int $ttl - time to live (store) in seconds
* @param array|string $tags - array of tags for this key
* @return bool
*/
public function save($k, $v, $ttl = 259200, $tags = NULL)
{
if (empty($k))
{
$this->ReportError('empty keys are not allowed', __LINE__);
return false;
}
static $cleaned = false;
if (!$cleaned)
{
$this->del_old();
$cleaned = true;
}
if (!apc_store($this->prefix.$k, $v, intval($ttl)))
{
$this->ReportError('apc can not store key', __LINE__);
return false;
}
if (!empty($tags)) $this->set_tags($k, $tags, $ttl);
return true;
}
/**
* Returns, how many seconds left till key expiring.
* @param $key
* @return int
*/
public function getKeyTTL($key)
{
$i = new \APCIterator('user', '/^'.$this->prefix.$key.'$/', APC_ITER_TTL+APC_ITER_CTIME, 1);
foreach ($i as $key)
{
if ($key[self::apc_arr_ttl]!=0) return (($key[self::apc_arr_ctime]+$key[self::apc_arr_ttl])-time());
else return self::max_ttl;
}
return self::max_ttl;
}
/**
* Read data from memory storage
*
* @param string|array $k (string or array of string keys)
* @param mixed $ttl_left = (ttl - time()) of key. Use to exclude dog-pile effect, with lock/unlock_key methods.
* @return mixed
*/
public function read($k, &$ttl_left = -1)
{
if (empty($k))
{
$this->ReportError('empty key are not allowed', __LINE__);
return NULL;
}
if (is_array($k))
{
$data = array();
$return_ttl = ($ttl_left!==-1 ? true : false);
$ttl_left = array();
foreach ($k as $key)
{
$key = (string)$key;
$data[$key] = apc_fetch($this->prefix.$key, $success);
if (!$success)
{
unset($data[$key]);
continue;
}
if ($return_ttl) $ttl_left[$key] = $this->getKeyTTL($key);
}
}
else
{
$data = apc_fetch($this->prefix.$k, $success);
if (!$success)
{
$this->ReportError('apc can not fetch key '.$k.' (or not exists)', __LINE__);
return false;
}
if ($ttl_left!==-1)
{
$ttl_left = $this->getKeyTTL($k);
if ($ttl_left < 0) $data = false; //key expired
}
}
return $data;
}
/** Return array of all stored keys */
public function get_keys()
{
$map = array();
$l = strlen($this->prefix);
$i = new \APCIterator('user', '/^'.$this->prefix.'/', APC_ITER_KEY);
foreach ($i as $item)
{
$map[] = substr($item[self::apc_arr_key], $l);
}
return $map;
}
/**
* Delete key or array of keys from storage
* @param string|array $k
* @return boolean
*/
public function del($k)
{
if (empty($k))
{
$this->ReportError('empty key are not allowed', __LINE__);
return false;
}
if (is_array($k))
{
$todel = array();
foreach ($k as $key)
{
$todel[] = $this->prefix.$key;
if (apc_exists($this->tags_prefix.$key)) $todel[] = $this->tags_prefix.$key;
if (apc_exists($this->lock_key_prefix.$key)) $todel[] = $this->lock_key_prefix.$key;
}
$r = apc_delete($todel);
if (empty($r)) return true;
else return $r;
}
else
{
if (apc_exists($this->tags_prefix.$k)) apc_delete($this->tags_prefix.$k);
if (apc_exists($this->lock_key_prefix.$k)) apc_delete($this->lock_key_prefix.$k);
return apc_delete($this->prefix.$k);
}
}
/**
* Delete old (by ttl) variables from storage
* It's very important function to prevent APC's cache fragmentation.
* @return boolean
*/
public function del_old()
{
$t = time();
$apc_user_info = apc_cache_info('user', true);
$apc_ttl = 0;
if (!empty($apc_user_info['ttl']))
{
$apc_ttl = $apc_user_info['ttl']/2;
$check_period = $apc_ttl;
}
if (empty($check_period) || $check_period > 1800) $check_period = 1800;
$ittl = new \APCIterator('user', '/^'.$this->defragmentation_prefix.'$/', APC_ITER_ATIME, 1);
$cttl = $ittl->current();
$previous_cleaning = $cttl[self::apc_arr_atime];
if (empty($previous_cleaning) || ($t-$previous_cleaning) > $check_period)
{
apc_store($this->defragmentation_prefix, $t, $check_period);
$todel = array();
$i = new \APCIterator('user', null, APC_ITER_TTL+APC_ITER_KEY+APC_ITER_CTIME+APC_ITER_ATIME);
foreach ($i as $key)
{
if ($key[self::apc_arr_ttl] > 0 && ($t-$key[self::apc_arr_ctime]) > $key[self::apc_arr_ttl]) $todel[] = $key[self::apc_arr_key];
else
{
//this code is necessary to prevent deletion variables from cache by apc.ttl (they deletes not by their ttl+ctime, but apc.ttl+atime)
if ($apc_ttl > 0 && (($t-$key[self::apc_arr_atime]) > $apc_ttl)) apc_fetch($key[self::apc_arr_key]);
}
}
if (!empty($todel))
{
$r = apc_delete($todel);
if (!empty($r)) return $r;
else return true;
}
}
return true;
}
/**
* Delete keys by tags
*
* @param array|string $tags - tag or array of tags
* @return boolean
*/
public function del_by_tags($tags)
{
if (!is_array($tags)) $tags = array($tags);
$todel = array();
$l = strlen($this->tags_prefix);
$i = new \APCIterator('user', '/^'.$this->tags_prefix.'/', APC_ITER_KEY+APC_ITER_VALUE);
foreach ($i as $key_tags)
{
if (is_array($key_tags[self::apc_arr_value]))
{
$intersect = array_intersect($tags, $key_tags[self::apc_arr_value]);
if (!empty($intersect)) $todel[] = substr($key_tags[self::apc_arr_key], $l);
}
}
if (!empty($todel)) return $this->del($todel);
return true;
}
/**
* Select from storage via callback function
*
* @param callback $fx ($value_array,$key)
* @param bool $get_array
* @return mixed
*/
public function select_fx($fx, $get_array = false)
{
$arr = array();
$l = strlen($this->prefix);
$i = new \APCIterator('user', '/^'.$this->prefix.'/', APC_ITER_KEY+APC_ITER_VALUE);
foreach ($i as $item)
{
if (!is_array($item[self::apc_arr_value])) continue;
$s = $item[self::apc_arr_value];
$index = substr($item[self::apc_arr_key], $l);
if ($fx($s, $index)===true)
{
if (!$get_array) return $s;
else $arr[$index] = $s;
}
}
if (!$get_array || empty($arr)) return false;
else return $arr;
}
/**
* Increment value of key
* @param string $key
* @param mixed $by_value
* if stored value is array:
* if $by_value is value in array, new element will be pushed to the end of array,
* if $by_value is key=>value array, key=>value pair will be added (or updated)
* @param int $limit_keys_count - maximum count of elements (used only if stored value is array)
* @return int|string|array new value of key
*/
public function increment($key, $by_value = 1, $limit_keys_count = 0)
{
if (empty($key))
{
$this->ReportError('empty keys are not allowed', __LINE__);
return false;
}
$value = apc_fetch($this->prefix.$key, $success);
if (!$success)
{
if ($this->save($key, $by_value)) return $by_value;
else return false;
}
if (is_array($value))
{
if ($limit_keys_count > 0 && (count($value) > $limit_keys_count)) $value = array_slice($value, $limit_keys_count*(-1)+1);
if (is_array($by_value))
{
$set_key = key($by_value);
if (!empty($set_key)) $value[$set_key] = $by_value[$set_key];
else $value[] = $by_value[0];
}
else $value[] = $by_value;
if ($this->save($key, $value)) return $value;
else return false;
}
elseif (is_numeric($value) && is_numeric($by_value)) return apc_inc($this->prefix.$key, $by_value);
else
{
$value .= $by_value;
if ($this->save($key, $value)) return $value;
else return false;
}
}
/**
* Get exclusive mutex for key. Key will be still accessible to read and write, but
* another process can exclude dog-pile effect, if before updating the key he will try to get this mutex.
* Example:
* Process 1 reads key simultaneously with Process 2.
* Value of this key are too old, so Process 1 going to refresh it. Simultaneously with Process 2.
* But both of them trying to lock_key, and Process 1 only will refresh value of key (taking it from database, e.g.),
* and Process 2 can decide, what he want to do - use old value and not spent time to database, or something else.
* @param mixed $key
* @param mixed $auto_unlocker_variable - pass empty, just declared variable
*/
public function lock_key($key, &$auto_unlocker_variable)
{
$r = apc_add($this->lock_key_prefix.$key, 1, self::key_lock_time);
if (!$r) return false;
$auto_unlocker_variable = new KeyAutoUnlocker(array($this, 'unlock_key'));
$auto_unlocker_variable->key = $key;
return true;
}
/**
* Unlock key, locked by method 'lock_key'
* @param KeyAutoUnlocker $auto_unlocker
* @return bool
*/
public function unlock_key(KeyAutoUnlocker $auto_unlocker)
{
if (empty($auto_unlocker->key))
{
$this->ReportError('autoUnlocker should be passed', __LINE__);
return false;
}
$auto_unlocker->revoke();
return apc_delete($this->lock_key_prefix.$auto_unlocker->key);
}
/**
* @return array
*/
public function get_stat()
{
return array(
'system' => apc_cache_info('', true),
'user' => apc_cache_info('user', true)
);
}
}