Merge pull request #23912 from mlutfy/reportVars
[civicrm-core.git] / CRM / Utils / Cache / ArrayCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Utils_Cache_ArrayCache
20 */
21 class CRM_Utils_Cache_ArrayCache implements CRM_Utils_Cache_Interface {
22
23 use CRM_Utils_Cache_NaiveMultipleTrait;
24 // TODO Native implementation
25 use CRM_Utils_Cache_NaiveHasTrait;
26
27 const DEFAULT_TIMEOUT = 3600;
28
29 /**
30 * The cache storage container, an in memory array by default
31 * @var array
32 */
33 protected $_cache;
34
35 protected $_expires;
36
37 /**
38 * Constructor.
39 *
40 * @param array $config
41 * An array of configuration params.
42 *
43 * @return \CRM_Utils_Cache_ArrayCache
44 */
45 public function __construct($config) {
46 $this->_cache = [];
47 $this->_expires = [];
48 }
49
50 /**
51 * @param string $key
52 * @param mixed $value
53 * @param null|int|\DateInterval $ttl
54 * @return bool
55 * @throws \Psr\SimpleCache\InvalidArgumentException
56 */
57 public function set($key, $value, $ttl = NULL) {
58 CRM_Utils_Cache::assertValidKey($key);
59 $this->_cache[$key] = $this->reobjectify($value);
60 $this->_expires[$key] = CRM_Utils_Date::convertCacheTtlToExpires($ttl, self::DEFAULT_TIMEOUT);
61 return TRUE;
62 }
63
64 /**
65 * @param string $key
66 * @param mixed $default
67 *
68 * @return mixed
69 * @throws \Psr\SimpleCache\InvalidArgumentException
70 */
71 public function get($key, $default = NULL) {
72 CRM_Utils_Cache::assertValidKey($key);
73 if (isset($this->_expires[$key]) && is_numeric($this->_expires[$key]) && $this->_expires[$key] <= time()) {
74 return $default;
75 }
76 if (array_key_exists($key, $this->_cache)) {
77 return $this->reobjectify($this->_cache[$key]);
78 }
79 return $default;
80 }
81
82 /**
83 * @param string $key
84 * @return bool
85 * @throws \Psr\SimpleCache\InvalidArgumentException
86 */
87 public function delete($key) {
88 CRM_Utils_Cache::assertValidKey($key);
89
90 unset($this->_cache[$key]);
91 unset($this->_expires[$key]);
92 return TRUE;
93 }
94
95 public function flush() {
96 unset($this->_cache);
97 unset($this->_expires);
98 $this->_cache = [];
99 return TRUE;
100 }
101
102 public function clear() {
103 return $this->flush();
104 }
105
106 private function reobjectify($value) {
107 if (is_object($value)) {
108 return unserialize(serialize($value));
109 }
110 if (is_array($value)) {
111 foreach ($value as $p) {
112 if (is_object($p)) {
113 return unserialize(serialize($value));
114 }
115 }
116 }
117 return $value;
118 }
119
120 /**
121 * @param string $key
122 * @return int|null
123 */
124 public function getExpires($key) {
125 return $this->_expires[$key] ?? NULL;
126 }
127
128 }