3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Class CRM_Utils_Cache_FastArrayDecorator
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:
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.
49 * But... it is twice as fast (on high-volume reads).
51 * Ex: $cache = new CRM_Utils_Cache_FastArrayDecorator(new CRM_Utils_Cache_Redis(...));
53 * @see CRM_Utils_Cache_ArrayDecorator
55 class CRM_Utils_Cache_FastArrayDecorator
implements CRM_Utils_Cache_Interface
{
57 // TODO Consider native implementation.
58 use CRM_Utils_Cache_NaiveMultipleTrait
;
62 * Default time-to-live (seconds) for cache items that don't have a TTL.
64 protected $defaultTimeout;
67 * @var CRM_Utils_Cache_Interface
73 * Array(string $cacheKey => mixed $cacheValue).
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.
83 public function __construct(\CRM_Utils_Cache_Interface
$delegate, $defaultTimeout = 3600) {
84 $this->defaultTimeout
= $defaultTimeout;
85 $this->delegate
= $delegate;
88 public function set($key, $value, $ttl = NULL) {
89 if (is_int($ttl) && $ttl <= 0) {
90 return $this->delete($key);
93 if ($this->delegate
->set($key, $value, $ttl)) {
94 $this->values
[$key] = $value;
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];
108 $nack = CRM_Utils_Cache
::nack();
109 $value = $this->delegate
->get($key, $nack);
110 if ($value === $nack) {
114 $this->values
[$key] = $value;
118 public function delete($key) {
119 CRM_Utils_Cache
::assertValidKey($key);
120 unset($this->values
[$key]);
121 return $this->delegate
->delete($key);
124 public function flush() {
125 return $this->clear();
128 public function clear() {
130 return $this->delegate
->clear();
133 public function has($key) {
134 return $this->delegate
->has($key);