forked from e-oz/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemcacheObject.php
504 lines (456 loc) · 12.3 KB
/
MemcacheObject.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
namespace Jamm\Memory;
class MemcacheObject extends MemoryObject implements IMemoryStorage
{
protected $prefix = 'K'; //because I love my wife Katya :)
/** @var string */
protected $lock_key_prefix;
/** @var string $ttl_table_name array (key=>ttl) */
protected $ttl_table_name;
/** @var string $tags_table_name array (key=>array(tags)) */
protected $tags_table_name;
/** @var IMemcacheDecorator */
protected $memcache;
const lock_key_prefix = '.lock_key.';
const ttl_table_prefix = '.ttl.';
const tags_table_prefix = '.tags.';
/**
* @param string $prefix Symbol "." will be replaced to "_"
* @param string $host
* @param int $port
*/
public function __construct($prefix = '', $host = 'localhost', $port = 11211)
{
$this->setMemcacheObject($host, $port);
if (!empty($prefix))
{
$this->prefix = str_replace('.', '_', $prefix).'.';
}
$this->lock_key_prefix = self::lock_key_prefix.$this->prefix;
$this->ttl_table_name = self::ttl_table_prefix.$this->prefix;
$this->tags_table_name = self::tags_table_prefix.$this->prefix;
}
protected function setMemcacheObject($host = 'localhost', $port = 11211)
{
$this->memcache = new \Memcache();
if (!$this->memcache->connect($host, $port))
{
$this->ReportError('memcache not connected', __LINE__);
}
}
/**
* 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;
}
$k = (string)$k;
$ttl = $this->ttl_to_expiration($ttl);
if (!$this->memcache->add($this->prefix.$k, $v, null, $ttl))
{
$reason = 'key already exists';
if (strlen($this->prefix.$k) > 250) $reason = 'key length should be <250';
elseif (strlen(serialize($v)) > 1048576) $reason = 'size of value should be <1Mb';
$this->ReportError('can not add key: '.$reason, __LINE__);
return false;
}
$ttl_table = $this->read_TTL_table();
$ttl_table[$k] = $ttl;
$this->save_TTL_table($ttl_table);
if (!empty($tags)) $this->set_tags($k, $tags);
return true;
}
protected function ttl_to_expiration($ttl)
{
$ttl = intval($ttl);
if ($ttl <= 0 || $ttl > self::max_ttl) $ttl = self::max_ttl;
return $ttl+time();
}
protected function &read_TTL_table()
{
$ttl_table = $this->memcache->get($this->ttl_table_name);
if (empty($ttl_table)) $ttl_table = array();
return $ttl_table;
}
/**
* Delete key or array of keys from storage
* @param string|array $key - keys
* @return boolean|array - if array of keys was passed, on error will be returned array of not deleted keys, or 'true' on success.
*/
public function del($key)
{
if (empty($key))
{
$this->ReportError('empty key are not allowed', __LINE__);
return false;
}
$tags_table = $this->memcache->get($this->tags_table_name);
if (empty($tags_table)) $tags_table = array();
$tags_table_changed = false;
$ttl_table = $this->read_TTL_table();
$ttl_table_changed = false;
$r = true;
if (is_array($key))
{
foreach ($key as $k)
{
$k = (string)$k;
$this->memcache->delete($this->prefix.$k);
$this->memcache->delete($this->lock_key_prefix.$k);
if (array_key_exists($k, $tags_table))
{
unset($tags_table[$k]);
$tags_table_changed = true;
}
if (array_key_exists($k, $ttl_table))
{
unset($ttl_table[$k]);
$ttl_table_changed = true;
}
}
}
else
{
$r = $this->memcache->delete($this->prefix.$key);
if (array_key_exists($key, $tags_table))
{
unset($tags_table[$key]);
$tags_table_changed = true;
}
if (array_key_exists($key, $ttl_table))
{
unset($ttl_table[$key]);
$ttl_table_changed = true;
}
$this->memcache->delete($this->lock_key_prefix.$key);
}
if ($tags_table_changed) $this->memcache->set($this->tags_table_name, $tags_table, null, time()+self::max_ttl);
if ($ttl_table_changed) $this->save_TTL_table($ttl_table);
return $r;
}
/**
* 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);
$tags_table = $this->memcache->get($this->tags_table_name);
if (empty($tags_table)) return false;
$tags_table_changed = false;
foreach ($tags_table as $key => $key_tags)
{
$intersect = array_intersect($tags, $key_tags);
if (!empty($intersect))
{
$this->memcache->delete($this->prefix.$key);
unset($tags_table[$key]);
$tags_table_changed = true;
}
}
if ($tags_table_changed) $this->memcache->set($this->tags_table_name, $tags_table, null, time()+self::max_ttl);
return true;
}
/**
* Delete old (by ttl) variables from storage
* @return boolean
*/
public function del_old()
{
$ttl_table = $this->read_TTL_table();
if (empty($ttl_table)) return true;
$t = time();
$changed = false;
foreach ($ttl_table as $key => $ttl)
{
if ($ttl < $t)
{
$this->memcache->delete($this->prefix.$key);
unset($ttl_table[$key]);
$changed = true;
}
}
if ($changed) $this->memcache->set($this->ttl_table_name, $ttl_table, null, time()+self::max_ttl);
return true;
}
/** Return array of all stored keys */
public function get_keys()
{
return array_keys($this->read_TTL_table());
}
/**
* 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 = $this->memcache->get($this->prefix.$key);
if (empty($value))
{
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))
{
if ($by_value > 0) return $this->memcache->increment($this->prefix.$key, $by_value);
else
{
$value += $by_value;
if ($this->save($key, $value)) return $value;
else return false;
}
}
else
{
//append() method not used to not break compatibility with Memcache
$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.
* @param mixed $key
* @param mixed $auto_unlocker_variable - pass empty, just declared variable
*/
public function lock_key($key, &$auto_unlocker_variable)
{
$r = $this->memcache->add($this->lock_key_prefix.$key, 1, null, self::key_lock_time);
if (!$r) return false;
$auto_unlocker_variable = new KeyAutoUnlocker(array($this, 'unlock_key'));
$auto_unlocker_variable->key = $key;
return true;
}
/**
* 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] = $this->memcache->get($this->prefix.$key);
if ($data[$key]===false || $data[$key]===null)
{
unset($data[$key]);
continue;
}
if ($return_ttl)
{
$ttl_left[$key] = $this->getKeyTTL($key);
if ($ttl_left <= 0)
{
unset($data[$key]);
$this->del($key);
}
}
}
}
else
{
$data = $this->memcache->get($this->prefix.$k);
if ($data===false || $data===null)
{
if (strlen($this->prefix.$k) > 250) $this->ReportError('Length of key should be <250', __LINE__);
return false;
}
if ($ttl_left!==-1)
{
$ttl_left = $this->getKeyTTL($k);
if ($ttl_left <= 0) //key expired
{
$data = false;
$this->del($k);
}
}
}
return $data;
}
/**
* Save variable in memory storage
*
* @param string $k - key
* @param mixed $v - value
* @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;
}
$k = (string)$k;
$ttl = $this->ttl_to_expiration($ttl);
if (false===$this->memcache->set($this->prefix.$k, $v, 0, $ttl))
{
$reason = $this->prefix.$k;
if (strlen($this->prefix.$k) > 250) $reason = 'key length should be <250';
elseif (strlen(serialize($v)) > 1048576) $reason = 'size of value should be <1Mb';
$this->ReportError('can not add key: '.$reason, __LINE__);
$this->ReportError('memcache can not store key: '.$reason, __LINE__);
return false;
}
$ttl_table = $this->read_TTL_table();
if (!isset($ttl_table[$k]) || $ttl_table[$k]!=$ttl)
{
$ttl_table[$k] = $ttl;
$this->save_TTL_table($ttl_table);
}
if (!empty($tags)) $this->set_tags($k, $tags);
return true;
}
/**
* Select from storage via callback function
* Only values of 'array' type will be selected
* @param callback $fx ($value_array,$key)
* @param bool $get_array
* @return mixed
*/
public function select_fx($fx, $get_array = false)
{
$arr = array();
$keys = $this->get_keys();
if (empty($keys)) return false;
foreach ($keys as $index)
{
$s = $this->read($index);
if (!is_array($s)) continue;
if ($fx($s, $index)===true)
{
if (!$get_array) return $s;
else $arr[$index] = $s;
}
}
if (!$get_array || empty($arr)) return false;
else return $arr;
}
/**
* 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 $this->memcache->delete($this->lock_key_prefix.$auto_unlocker->key);
}
/**
* @param string $key
* @param array|string $tags
* @param int $ttl
* @return bool
*/
public function set_tags($key, $tags)
{
if (!is_array($tags))
{
if (is_scalar($tags)) $tags = array($tags);
else $tags = array();
}
if (!empty($tags))
{
$key = (string)$key;
$tags_table = $this->memcache->get($this->tags_table_name);
if (empty($tags_table)) $tags_table = array();
if (!array_key_exists($key, $tags_table) || $tags!=$tags_table[$key])
{
$tags_table[$key] = $tags;
return $this->memcache->set($this->tags_table_name, $tags_table, null, time()+self::max_ttl);
}
else return true;
}
return false;
}
public function getKeyTTL($key)
{
$ttl_table = $this->read_TTL_table();
$key = (string)$key;
if (!array_key_exists($key, $ttl_table)) return false;
else return ($ttl_table[$key]-time());
}
/**
* @param array $table
* @return void
*/
protected function save_TTL_table($table)
{
if (!empty($table))
{
$t = time();
foreach ($table as $key => $ttl)
{
if ($ttl < $t) unset($table[$key]);
}
}
if (false===$this->memcache->set($this->ttl_table_name, $table, null, time()+self::max_ttl))
{
$this->ReportError('memcache can not save ttl table', __LINE__);
}
}
/**
* @return array
*/
public function get_stat()
{
return $this->memcache->getStats();
}
}