Merge pull request #13498 from francescbassas/patch-18
[civicrm-core.git] / CRM / Utils / Cache / FastArrayDecorator.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Class CRM_Utils_Cache_FastArrayDecorator
36 *
37 * Like CRM_Utils_Cache_ArrayDecorator, this creates a two-tier cache.
38 * But it's... faster. The speed improvements are achieved by sacrificing
39 * compliance with PSR-16. Specific trade-offs:
40 *
41 * 1. TTL values are not tracked locally. Any data cached locally will stay
42 * active until the instance is destroyed (i.e. until the request ends).
43 * You won't notice this is you have short-lived requests and long-lived caches.
44 * 2. If you store an *object* in the local cache, the same object instance
45 * will be used through the end of the request. If you modify a property
46 * of the object, the change will endure within the current pageview but
47 * will not pass-through to the persistent cache.
48 *
49 * But... it is twice as fast (on high-volume reads).
50 *
51 * Ex: $cache = new CRM_Utils_Cache_FastArrayDecorator(new CRM_Utils_Cache_Redis(...));
52 *
53 * @see CRM_Utils_Cache_ArrayDecorator
54 */
55 class CRM_Utils_Cache_FastArrayDecorator implements CRM_Utils_Cache_Interface {
56
57 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
58
59 /**
60 * @var int
61 * Default time-to-live (seconds) for cache items that don't have a TTL.
62 */
63 protected $defaultTimeout;
64
65 /**
66 * @var CRM_Utils_Cache_Interface
67 */
68 private $delegate;
69
70 /**
71 * @var array
72 * Array(string $cacheKey => mixed $cacheValue).
73 */
74 private $values = [];
75
76 /**
77 * CRM_Utils_Cache_FastArrayDecorator constructor.
78 * @param \CRM_Utils_Cache_Interface $delegate
79 * @param int $defaultTimeout
80 * Default number of seconds each cache-item should endure.
81 */
82 public function __construct(\CRM_Utils_Cache_Interface $delegate, $defaultTimeout = 3600) {
83 $this->defaultTimeout = $defaultTimeout;
84 $this->delegate = $delegate;
85 }
86
87 public function set($key, $value, $ttl = NULL) {
88 if (is_int($ttl) && $ttl <= 0) {
89 return $this->delete($key);
90 }
91
92 if ($this->delegate->set($key, $value, $ttl)) {
93 $this->values[$key] = $value;
94 return TRUE;
95 }
96 else {
97 return FALSE;
98 }
99 }
100
101 public function get($key, $default = NULL) {
102 if (array_key_exists($key, $this->values)) {
103 return $this->values[$key];
104 }
105
106 $nack = CRM_Utils_Cache::nack();
107 $value = $this->delegate->get($key, $nack);
108 if ($value === $nack) {
109 return $default;
110 }
111
112 $this->values[$key] = $value;
113 return $value;
114 }
115
116 public function delete($key) {
117 unset($this->values[$key]);
118 return $this->delegate->delete($key);
119 }
120
121 public function flush() {
122 return $this->clear();
123 }
124
125 public function clear() {
126 $this->values = [];
127 return $this->delegate->clear();
128 }
129
130 public function has($key) {
131 return $this->delegate->has($key);
132 }
133
134 }