3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 https://civicrm.org/licensing
35 * Class CRM_Utils_Cache_ArrayCache
37 class CRM_Utils_Cache_ArrayCache
implements CRM_Utils_Cache_Interface
{
39 use CRM_Utils_Cache_NaiveMultipleTrait
;
40 // TODO Native implementation
41 use CRM_Utils_Cache_NaiveHasTrait
;
43 const DEFAULT_TIMEOUT
= 3600;
46 * The cache storage container, an in memory array by default
56 * @param array $config
57 * An array of configuration params.
59 * @return \CRM_Utils_Cache_ArrayCache
61 public function __construct($config) {
69 * @param null|int|\DateInterval $ttl
71 * @throws \Psr\SimpleCache\InvalidArgumentException
73 public function set($key, $value, $ttl = NULL) {
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
);
82 * @param mixed $default
85 * @throws \Psr\SimpleCache\InvalidArgumentException
87 public function get($key, $default = NULL) {
88 CRM_Utils_Cache
::assertValidKey($key);
89 if (isset($this->_expires
[$key]) && is_numeric($this->_expires
[$key]) && $this->_expires
[$key] <= time()) {
92 if (array_key_exists($key, $this->_cache
)) {
93 return $this->reobjectify($this->_cache
[$key]);
101 * @throws \Psr\SimpleCache\InvalidArgumentException
103 public function delete($key) {
104 CRM_Utils_Cache
::assertValidKey($key);
106 unset($this->_cache
[$key]);
107 unset($this->_expires
[$key]);
111 public function flush() {
112 unset($this->_cache
);
113 unset($this->_expires
);
118 public function clear() {
119 return $this->flush();
122 private function reobjectify($value) {
123 if (is_object($value)) {
124 return unserialize(serialize($value));
126 if (is_array($value)) {
127 foreach ($value as $p) {
129 return unserialize(serialize($value));
140 public function getExpires($key) {
141 return $this->_expires
[$key] ?
: NULL;