rcube_cache.php
18.9 KB
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Caching engine |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
+-----------------------------------------------------------------------+
*/
/**
* Interface class for accessing Roundcube cache
*
* @package Framework
* @subpackage Cache
*/
class rcube_cache
{
protected $type;
protected $userid;
protected $prefix;
protected $ttl;
protected $packed;
protected $indexed;
protected $index;
protected $index_update;
protected $cache = [];
protected $updates = [];
protected $exp_records = [];
protected $refresh_time = 0.5; // how often to refresh/save the index and cache entries
protected $debug = false;
protected $max_packet = -1;
const MAX_EXP_LEVEL = 2;
const DATE_FORMAT = 'Y-m-d H:i:s.u';
const DATE_FORMAT_REGEX = '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{1,6}';
/**
* Object factory
*
* @param string $type Engine type ('db', 'memcache', 'apc', 'redis')
* @param int $userid User identifier
* @param string $prefix Key name prefix
* @param string $ttl Expiration time of memcache/apc items
* @param bool $packed Enables/disabled data serialization.
* It's possible to disable data serialization if you're sure
* stored data will be always a safe string
* @param bool $indexed Use indexed cache. Indexed cache is more appropriate for
* storing big data with possibility to remove it by a key prefix.
* Non-indexed cache does not remove data, but flags it for expiration,
* also stores it in memory until close() method is called.
*
* @param rcube_cache Cache object
*/
public static function factory($type, $userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
{
$driver = strtolower($type) ?: 'db';
$class = "rcube_cache_$driver";
if (!$driver || !class_exists($class)) {
rcube::raise_error([
'code' => 600, 'type' => 'db',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Configuration error. Unsupported cache driver: $driver"
],
true, true
);
}
return new $class($userid, $prefix, $ttl, $packed, $indexed);
}
/**
* Object constructor.
*
* @param int $userid User identifier
* @param string $prefix Key name prefix
* @param string $ttl Expiration time of memcache/apc items
* @param bool $packed Enables/disabled data serialization.
* It's possible to disable data serialization if you're sure
* stored data will be always a safe string
* @param bool $indexed Use indexed cache. Indexed cache is more appropriate for
* storing big data with possibility to remove it by key prefix.
* Non-indexed cache does not remove data, but flags it for expiration,
* also stores it in memory until close() method is called.
*/
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
{
$this->userid = (int) $userid;
$this->ttl = min(get_offset_sec($ttl), 2592000);
$this->prefix = $prefix;
$this->packed = $packed;
$this->indexed = $indexed;
}
/**
* Returns cached value.
*
* @param string $key Cache key name
*
* @return mixed Cached value
*/
public function get($key)
{
if (array_key_exists($key, $this->cache)) {
return $this->cache[$key];
}
return $this->read_record($key);
}
/**
* Sets (add/update) value in cache.
*
* @param string $key Cache key name
* @param mixed $data Cache data
*
* @return bool True on success, False on failure
*/
public function set($key, $data)
{
return $this->write_record($key, $data);
}
/**
* @deprecated Use self::get()
*/
public function read($key)
{
return $this->get($key);
}
/**
* @deprecated Use self::set()
*/
public function write($key, $data)
{
return $this->set($key, $data);
}
/**
* Clears the cache.
*
* @param string $key Cache key name or pattern
* @param bool $prefix_mode Enable it to clear all keys starting
* with prefix specified in $key
*/
public function remove($key = null, $prefix_mode = false)
{
// Remove record(s) from the backend
$this->remove_record($key, $prefix_mode);
}
/**
* Remove cache records older than ttl
*/
public function expunge()
{
// to be overwritten by engine class
}
/**
* Remove expired records of all caches
*/
public static function gc()
{
// Only DB cache requires an action to remove expired entries
rcube_cache_db::gc();
}
/**
* Writes the cache back to the DB.
*/
public function close()
{
$this->write_index(true);
$this->index = null;
$this->cache = [];
$this->updates = [];
}
/**
* A helper to build cache key for specified parameters.
*
* @param string $prefix Key prefix (Max. length 64 characters)
* @param array $params Additional parameters
*
* @return string Key name
*/
public static function key_name($prefix, $params = [])
{
$cache_key = $prefix;
if (!empty($params)) {
$func = function($v) {
if (is_array($v)) {
sort($v);
}
return is_string($v) ? $v : serialize($v);
};
$params = array_map($func, $params);
$cache_key .= '.' . md5(implode(':', $params));
}
return $cache_key;
}
/**
* Reads cache entry.
*
* @param string $key Cache key name
*
* @return mixed Cached value
*/
protected function read_record($key)
{
$this->load_index();
// Consistency check (#1490390)
if (is_array($this->index) && !in_array($key, $this->index)) {
// we always check if the key exist in the index
// to have data in consistent state. Keeping the index consistent
// is needed for keys delete operation when we delete all keys or by prefix.
return;
}
$ckey = $this->ckey($key);
$data = $this->get_item($ckey);
if ($this->indexed) {
return $data !== false ? $this->unserialize($data) : null;
}
if ($data !== false) {
$timestamp = 0;
$utc = new DateTimeZone('UTC');
// Extract timestamp from the data entry
if (preg_match('/^(' . self::DATE_FORMAT_REGEX . '):/', $data, $matches)) {
try {
$timestamp = new DateTime($matches[1], $utc);
$data = substr($data, strlen($matches[1]) + 1);
}
catch (Exception $e) {
// invalid date = no timestamp
}
}
// Check if the entry is still valid by comparing with EXP timestamps
// For example for key 'mailboxes.123456789' we check entries:
// 'EXP:*', 'EXP:mailboxes' and 'EXP:mailboxes.123456789'.
if ($timestamp) {
$path = explode('.', "*.$key");
$path_len = min(self::MAX_EXP_LEVEL + 1, count($path));
for ($x = 1; $x <= $path_len; $x++) {
$prefix = implode('.', array_slice($path, 0, $x));
if ($x > 1) {
$prefix = substr($prefix, 2); // remove "*." prefix
}
if (($ts = $this->get_exp_timestamp($prefix)) && $ts > $timestamp) {
$timestamp = 0;
break;
}
}
}
$data = $timestamp ? $this->unserialize($data) : null;
}
else {
$data = null;
}
return $this->cache[$key] = $data;
}
/**
* Writes single cache record into DB.
*
* @param string $key Cache key name
* @param mixed $data Serialized cache data
*
* @return bool True on success, False on failure
*/
protected function write_record($key, $data)
{
if ($this->indexed) {
$result = $this->store_record($key, $data);
if ($result) {
$this->load_index();
$this->index[] = $key;
if (!$this->index_update) {
$this->index_update = time();
}
}
}
else {
// In this mode we do not save the entry to the database immediately
// It's because we have cases where the same entry is updated
// multiple times in one request (e.g. 'messagecount' entry rcube_imap).
$this->updates[$key] = new DateTime('now', new DateTimeZone('UTC'));
$this->cache[$key] = $data;
$result = true;
}
$this->write_index();
return $result;
}
/**
* Deletes the cache record(s).
*
* @param string $key Cache key name or pattern
* @param boolean $prefix_mode Enable it to clear all keys starting
* with prefix specified in $key
*/
protected function remove_record($key = null, $prefix_mode = false)
{
if ($this->indexed) {
return $this->remove_record_indexed($key, $prefix_mode);
}
// "Remove" all keys
if ($key === null) {
$ts = new DateTime('now', new DateTimeZone('UTC'));
$this->add_item($this->ekey('*'), $ts->format(self::DATE_FORMAT));
$this->cache = [];
}
// "Remove" keys by name prefix
else if ($prefix_mode) {
$ts = new DateTime('now', new DateTimeZone('UTC'));
$prefix = implode('.', array_slice(explode('.', trim($key, '. ')), 0, self::MAX_EXP_LEVEL));
$this->add_item($this->ekey($prefix), $ts->format(self::DATE_FORMAT));
foreach (array_keys($this->cache) as $k) {
if (strpos($k, $key) === 0) {
$this->cache[$k] = null;
}
}
}
// Remove one key by name
else {
$this->delete_item($this->ckey($key));
$this->cache[$key] = null;
}
}
/**
* @see self::remove_record()
*/
protected function remove_record_indexed($key = null, $prefix_mode = false)
{
$this->load_index();
// Remove all keys
if ($key === null) {
foreach ($this->index as $key) {
$this->delete_item($this->ckey($key));
if (!$this->index_update) {
$this->index_update = time();
}
}
$this->index = [];
}
// Remove keys by name prefix
else if ($prefix_mode) {
foreach ($this->index as $idx => $k) {
if (strpos($k, $key) === 0) {
$this->delete_item($this->ckey($k));
unset($this->index[$idx]);
if (!$this->index_update) {
$this->index_update = time();
}
}
}
}
// Remove one key by name
else {
$this->delete_item($this->ckey($key));
if (($idx = array_search($key, $this->index)) !== false) {
unset($this->index[$idx]);
if (!$this->index_update) {
$this->index_update = time();
}
}
}
$this->write_index();
}
/**
* Writes the index entry as well as updated entries into memcache/apc/redis DB.
*/
protected function write_index($force = null)
{
// Write updated/new entries when needed
if (!$this->indexed) {
$need_update = $force === true;
if (!$need_update && !empty($this->updates)) {
$now = new DateTime('now', new DateTimeZone('UTC'));
$need_update = floatval(min($this->updates)->format('U.u')) < floatval($now->format('U.u')) - $this->refresh_time;
}
if ($need_update) {
foreach ($this->updates as $key => $ts) {
if (isset($this->cache[$key])) {
$this->store_record($key, $this->cache[$key], $ts);
}
}
$this->updates = [];
}
}
// Write index entry when needed
else {
$need_update = $this->index_update && $this->index !== null
&& ($force === true || $this->index_update > time() - $this->refresh_time);
if ($need_update) {
$index = serialize(array_values(array_unique($this->index)));
$this->add_item($this->ikey(), $index);
$this->index_update = null;
$this->index = null;
}
}
}
/**
* Gets the index entry from memcache/apc/redis DB.
*/
protected function load_index()
{
if (!$this->indexed) {
return;
}
if ($this->index !== null) {
return;
}
$data = $this->get_item($this->ikey());
$this->index = $data ? unserialize($data) : [];
}
/**
* Write data entry into cache
*/
protected function store_record($key, $data, $ts = null)
{
$value = $this->serialize($data);
if (!$this->indexed) {
if (!$ts) {
$ts = new DateTime('now', new DateTimeZone('UTC'));
}
$value = $ts->format(self::DATE_FORMAT) . ':' . $value;
}
$size = strlen($value);
// don't attempt to write too big data sets
if ($size > $this->max_packet_size()) {
trigger_error("rcube_cache: max_packet_size ($this->max_packet) exceeded for key $key. Tried to write $size bytes", E_USER_WARNING);
return false;
}
return $this->add_item($this->ckey($key), $value);
}
/**
* Fetches cache entry.
*
* @param string $key Cache internal key name
*
* @return mixed Cached value
*/
protected function get_item($key)
{
// to be overwritten by engine class
}
/**
* Adds entry into memcache/apc/redis DB.
*
* @param string $key Cache internal key name
* @param mixed $data Serialized cache data
*
* @param bool True on success, False on failure
*/
protected function add_item($key, $data)
{
// to be overwritten by engine class
}
/**
* Deletes entry from memcache/apc/redis DB.
*
* @param string $key Cache internal key name
*
* @param bool True on success, False on failure
*/
protected function delete_item($key)
{
// to be overwritten by engine class
}
/**
* Get EXP:<key> record value from cache
*/
protected function get_exp_timestamp($key)
{
if (!array_key_exists($key, $this->exp_records)) {
$data = $this->get_item($this->ekey($key));
$this->exp_records[$key] = $data ? new DateTime($data, new DateTimeZone('UTC')) : null;
}
return $this->exp_records[$key];
}
/**
* Creates per-user index cache key name (for memcache, apc, redis)
*
* @return string Cache key
*/
protected function ikey()
{
$key = $this->prefix . 'INDEX';
if ($this->userid) {
$key = $this->userid . ':' . $key;
}
return $key;
}
/**
* Creates per-user cache key name (for memcache, apc, redis)
*
* @param string $key Cache key name
*
* @return string Cache key
*/
protected function ckey($key)
{
$key = $this->prefix . ':' . $key;
if ($this->userid) {
$key = $this->userid . ':' . $key;
}
return $key;
}
/**
* Creates per-user cache key name for expiration time entry
*
* @param string $key Cache key name
*
* @return string Cache key
*/
protected function ekey($key, $prefix = null)
{
$key = $this->prefix . 'EXP:' . $key;
if ($this->userid) {
$key = $this->userid . ':' . $key;
}
return $key;
}
/**
* Serializes data for storing
*/
protected function serialize($data)
{
return $this->packed ? serialize($data) : $data;
}
/**
* Unserializes serialized data
*/
protected function unserialize($data)
{
return $this->packed ? @unserialize($data) : $data;
}
/**
* Determine the maximum size for cache data to be written
*/
protected function max_packet_size()
{
if ($this->max_packet < 0) {
$config = rcube::get_instance()->config;
$max_packet = $config->get($this->type . '_max_allowed_packet');
$this->max_packet = parse_bytes($max_packet) ?: 2097152; // default/max is 2 MB
}
return $this->max_packet;
}
/**
* Write memcache/apc/redis debug info to the log
*/
protected function debug($type, $key, $data = null, $result = null)
{
$line = strtoupper($type) . ' ' . $key;
if ($data !== null) {
$line .= ' ' . ($this->packed ? $data : serialize($data));
}
rcube::debug($this->type, $line, $result);
}
}