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