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 use CRM_Utils_Cache_NaiveMultipleTrait
; // TODO Consider native implementation.
61 * Default time-to-live (seconds) for cache items that don't have a TTL.
63 protected $defaultTimeout;
66 * @var CRM_Utils_Cache_Interface
72 * Array(string $cacheKey => mixed $cacheValue).
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.
82 public function __construct(\CRM_Utils_Cache_Interface
$delegate, $defaultTimeout = 3600) {
83 $this->defaultTimeout
= $defaultTimeout;
84 $this->delegate
= $delegate;
87 public function set($key, $value, $ttl = NULL) {
88 if (is_int($ttl) && $ttl <= 0) {
89 return $this->delete($key);
92 if ($this->delegate
->set($key, $value, $ttl)) {
93 $this->values
[$key] = $value;
101 public function get($key, $default = NULL) {
102 if (array_key_exists($key, $this->values
)) {
103 return $this->values
[$key];
106 $nack = CRM_Utils_Cache
::nack();
107 $value = $this->delegate
->get($key, $nack);
108 if ($value === $nack) {
112 $this->values
[$key] = $value;
116 public function delete($key) {
117 unset($this->values
[$key]);
118 return $this->delegate
->delete($key);
121 public function flush() {
122 return $this->clear();
125 public function clear() {
127 return $this->delegate
->clear();
130 public function has($key) {
131 return $this->delegate
->has($key);