Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
50bfb460 SB |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
50bfb460 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
50bfb460 SB |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
50bfb460 | 32 | */ |
5bc392e6 EM |
33 | |
34 | /** | |
35 | * Class CRM_Utils_Cache_Arraycache | |
36 | */ | |
6a488035 TO |
37 | class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { |
38 | ||
0d64c8fa | 39 | use CRM_Utils_Cache_NaiveMultipleTrait; |
6714d8d2 SL |
40 | // TODO Native implementation |
41 | use CRM_Utils_Cache_NaiveHasTrait; | |
0d64c8fa | 42 | |
b5d3f3c5 TO |
43 | const DEFAULT_TIMEOUT = 3600; |
44 | ||
6a488035 TO |
45 | /** |
46 | * The cache storage container, an in memory array by default | |
6714d8d2 | 47 | * @var array |
6a488035 | 48 | */ |
1606b7e9 | 49 | protected $_cache; |
6a488035 | 50 | |
b5d3f3c5 TO |
51 | protected $_expires; |
52 | ||
6a488035 | 53 | /** |
fe482240 | 54 | * Constructor. |
6a488035 | 55 | * |
77855840 TO |
56 | * @param array $config |
57 | * An array of configuration params. | |
6a488035 | 58 | * |
77b97be7 | 59 | * @return \CRM_Utils_Cache_Arraycache |
6a488035 | 60 | */ |
00be9182 | 61 | public function __construct($config) { |
be2fb01f CW |
62 | $this->_cache = []; |
63 | $this->_expires = []; | |
6a488035 TO |
64 | } |
65 | ||
5bc392e6 EM |
66 | /** |
67 | * @param string $key | |
68 | * @param mixed $value | |
858451a9 TO |
69 | * @param null|int|\DateInterval $ttl |
70 | * @return bool | |
b5d3f3c5 | 71 | * @throws \Psr\SimpleCache\InvalidArgumentException |
5bc392e6 | 72 | */ |
858451a9 | 73 | public function set($key, $value, $ttl = NULL) { |
b5d3f3c5 TO |
74 | CRM_Utils_Cache::assertValidKey($key); |
75 | $this->_cache[$key] = $this->reobjectify($value); | |
76 | $this->_expires[$key] = CRM_Utils_Date::convertCacheTtlToExpires($ttl, self::DEFAULT_TIMEOUT); | |
858451a9 | 77 | return TRUE; |
6a488035 TO |
78 | } |
79 | ||
5bc392e6 EM |
80 | /** |
81 | * @param string $key | |
2da67cc5 | 82 | * @param mixed $default |
5bc392e6 EM |
83 | * |
84 | * @return mixed | |
b5d3f3c5 | 85 | * @throws \Psr\SimpleCache\InvalidArgumentException |
5bc392e6 | 86 | */ |
2da67cc5 | 87 | public function get($key, $default = NULL) { |
b5d3f3c5 TO |
88 | CRM_Utils_Cache::assertValidKey($key); |
89 | if (isset($this->_expires[$key]) && is_numeric($this->_expires[$key]) && $this->_expires[$key] <= time()) { | |
90 | return $default; | |
91 | } | |
92 | if (array_key_exists($key, $this->_cache)) { | |
93 | return $this->reobjectify($this->_cache[$key]); | |
94 | } | |
95 | return $default; | |
6a488035 TO |
96 | } |
97 | ||
5bc392e6 EM |
98 | /** |
99 | * @param string $key | |
eec321a4 | 100 | * @return bool |
b5d3f3c5 | 101 | * @throws \Psr\SimpleCache\InvalidArgumentException |
5bc392e6 | 102 | */ |
00be9182 | 103 | public function delete($key) { |
b5d3f3c5 TO |
104 | CRM_Utils_Cache::assertValidKey($key); |
105 | ||
6a488035 | 106 | unset($this->_cache[$key]); |
b5d3f3c5 | 107 | unset($this->_expires[$key]); |
eec321a4 | 108 | return TRUE; |
6a488035 TO |
109 | } |
110 | ||
00be9182 | 111 | public function flush() { |
6a488035 | 112 | unset($this->_cache); |
b5d3f3c5 | 113 | unset($this->_expires); |
be2fb01f | 114 | $this->_cache = []; |
124e5288 | 115 | return TRUE; |
6a488035 | 116 | } |
96025800 | 117 | |
c31de879 TO |
118 | public function clear() { |
119 | return $this->flush(); | |
120 | } | |
121 | ||
b5d3f3c5 | 122 | private function reobjectify($value) { |
3768d7a0 TO |
123 | if (is_object($value)) { |
124 | return unserialize(serialize($value)); | |
125 | } | |
126 | if (is_array($value)) { | |
127 | foreach ($value as $p) { | |
128 | if (is_object($p)) { | |
129 | return unserialize(serialize($value)); | |
130 | } | |
131 | } | |
132 | } | |
133 | return $value; | |
b5d3f3c5 TO |
134 | } |
135 | ||
b510643f TO |
136 | /** |
137 | * @param string $key | |
138 | * @return int|null | |
139 | */ | |
140 | public function getExpires($key) { | |
141 | return $this->_expires[$key] ?: NULL; | |
142 | } | |
143 | ||
6a488035 | 144 | } |