Commit | Line | Data |
---|---|---|
62ea328c TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
62ea328c | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
62ea328c TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
62ea328c TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Class CRM_Utils_Cache_FastArrayDecorator | |
20 | * | |
21 | * Like CRM_Utils_Cache_ArrayDecorator, this creates a two-tier cache. | |
22 | * But it's... faster. The speed improvements are achieved by sacrificing | |
23 | * compliance with PSR-16. Specific trade-offs: | |
24 | * | |
25 | * 1. TTL values are not tracked locally. Any data cached locally will stay | |
26 | * active until the instance is destroyed (i.e. until the request ends). | |
27 | * You won't notice this is you have short-lived requests and long-lived caches. | |
28 | * 2. If you store an *object* in the local cache, the same object instance | |
29 | * will be used through the end of the request. If you modify a property | |
30 | * of the object, the change will endure within the current pageview but | |
31 | * will not pass-through to the persistent cache. | |
32 | * | |
33 | * But... it is twice as fast (on high-volume reads). | |
34 | * | |
35 | * Ex: $cache = new CRM_Utils_Cache_FastArrayDecorator(new CRM_Utils_Cache_Redis(...)); | |
36 | * | |
37 | * @see CRM_Utils_Cache_ArrayDecorator | |
38 | */ | |
39 | class CRM_Utils_Cache_FastArrayDecorator implements CRM_Utils_Cache_Interface { | |
40 | ||
6714d8d2 SL |
41 | // TODO Consider native implementation. |
42 | use CRM_Utils_Cache_NaiveMultipleTrait; | |
62ea328c TO |
43 | |
44 | /** | |
45 | * @var int | |
46 | * Default time-to-live (seconds) for cache items that don't have a TTL. | |
47 | */ | |
48 | protected $defaultTimeout; | |
49 | ||
50 | /** | |
51 | * @var CRM_Utils_Cache_Interface | |
52 | */ | |
53 | private $delegate; | |
54 | ||
55 | /** | |
56 | * @var array | |
57 | * Array(string $cacheKey => mixed $cacheValue). | |
58 | */ | |
59 | private $values = []; | |
60 | ||
61 | /** | |
62 | * CRM_Utils_Cache_FastArrayDecorator constructor. | |
63 | * @param \CRM_Utils_Cache_Interface $delegate | |
64 | * @param int $defaultTimeout | |
65 | * Default number of seconds each cache-item should endure. | |
66 | */ | |
67 | public function __construct(\CRM_Utils_Cache_Interface $delegate, $defaultTimeout = 3600) { | |
68 | $this->defaultTimeout = $defaultTimeout; | |
69 | $this->delegate = $delegate; | |
70 | } | |
71 | ||
72 | public function set($key, $value, $ttl = NULL) { | |
73 | if (is_int($ttl) && $ttl <= 0) { | |
74 | return $this->delete($key); | |
75 | } | |
76 | ||
77 | if ($this->delegate->set($key, $value, $ttl)) { | |
78 | $this->values[$key] = $value; | |
79 | return TRUE; | |
80 | } | |
81 | else { | |
82 | return FALSE; | |
83 | } | |
84 | } | |
85 | ||
86 | public function get($key, $default = NULL) { | |
8a8cbada | 87 | CRM_Utils_Cache::assertValidKey($key); |
62ea328c TO |
88 | if (array_key_exists($key, $this->values)) { |
89 | return $this->values[$key]; | |
90 | } | |
91 | ||
92 | $nack = CRM_Utils_Cache::nack(); | |
93 | $value = $this->delegate->get($key, $nack); | |
94 | if ($value === $nack) { | |
95 | return $default; | |
96 | } | |
97 | ||
98 | $this->values[$key] = $value; | |
99 | return $value; | |
100 | } | |
101 | ||
102 | public function delete($key) { | |
8a8cbada | 103 | CRM_Utils_Cache::assertValidKey($key); |
62ea328c TO |
104 | unset($this->values[$key]); |
105 | return $this->delegate->delete($key); | |
106 | } | |
107 | ||
108 | public function flush() { | |
109 | return $this->clear(); | |
110 | } | |
111 | ||
112 | public function clear() { | |
113 | $this->values = []; | |
114 | return $this->delegate->clear(); | |
115 | } | |
116 | ||
117 | public function has($key) { | |
118 | return $this->delegate->has($key); | |
119 | } | |
120 | ||
121 | } |