Merge pull request #14694 from eileenmcnaughton/cust_field_bulk
[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 // TODO Consider native implementation.
58 use CRM_Utils_Cache_NaiveMultipleTrait;
59
60 /**
61 * @var int
62 * Default time-to-live (seconds) for cache items that don't have a TTL.
63 */
64 protected $defaultTimeout;
65
66 /**
67 * @var CRM_Utils_Cache_Interface
68 */
69 private $delegate;
70
71 /**
72 * @var array
73 * Array(string $cacheKey => mixed $cacheValue).
74 */
75 private $values = [];
76
77 /**
78 * CRM_Utils_Cache_FastArrayDecorator constructor.
79 * @param \CRM_Utils_Cache_Interface $delegate
80 * @param int $defaultTimeout
81 * Default number of seconds each cache-item should endure.
82 */
83 public function __construct(\CRM_Utils_Cache_Interface $delegate, $defaultTimeout = 3600) {
84 $this->defaultTimeout = $defaultTimeout;
85 $this->delegate = $delegate;
86 }
87
88 public function set($key, $value, $ttl = NULL) {
89 if (is_int($ttl) && $ttl <= 0) {
90 return $this->delete($key);
91 }
92
93 if ($this->delegate->set($key, $value, $ttl)) {
94 $this->values[$key] = $value;
95 return TRUE;
96 }
97 else {
98 return FALSE;
99 }
100 }
101
102 public function get($key, $default = NULL) {
103 CRM_Utils_Cache::assertValidKey($key);
104 if (array_key_exists($key, $this->values)) {
105 return $this->values[$key];
106 }
107
108 $nack = CRM_Utils_Cache::nack();
109 $value = $this->delegate->get($key, $nack);
110 if ($value === $nack) {
111 return $default;
112 }
113
114 $this->values[$key] = $value;
115 return $value;
116 }
117
118 public function delete($key) {
119 CRM_Utils_Cache::assertValidKey($key);
120 unset($this->values[$key]);
121 return $this->delegate->delete($key);
122 }
123
124 public function flush() {
125 return $this->clear();
126 }
127
128 public function clear() {
129 $this->values = [];
130 return $this->delegate->clear();
131 }
132
133 public function has($key) {
134 return $this->delegate->has($key);
135 }
136
137 }