3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Utils_Cache_ArrayCache
21 class CRM_Utils_Cache_ArrayCache
implements CRM_Utils_Cache_Interface
{
23 use CRM_Utils_Cache_NaiveMultipleTrait
;
24 // TODO Native implementation
25 use CRM_Utils_Cache_NaiveHasTrait
;
27 const DEFAULT_TIMEOUT
= 3600;
30 * The cache storage container, an in memory array by default
40 * @param array $config
41 * An array of configuration params.
43 * @return \CRM_Utils_Cache_ArrayCache
45 public function __construct($config) {
53 * @param null|int|\DateInterval $ttl
55 * @throws \Psr\SimpleCache\InvalidArgumentException
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
);
66 * @param mixed $default
69 * @throws \Psr\SimpleCache\InvalidArgumentException
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()) {
76 if (array_key_exists($key, $this->_cache
)) {
77 return $this->reobjectify($this->_cache
[$key]);
85 * @throws \Psr\SimpleCache\InvalidArgumentException
87 public function delete($key) {
88 CRM_Utils_Cache
::assertValidKey($key);
90 unset($this->_cache
[$key]);
91 unset($this->_expires
[$key]);
95 public function flush() {
97 unset($this->_expires
);
102 public function clear() {
103 return $this->flush();
106 private function reobjectify($value) {
107 if (is_object($value)) {
108 return unserialize(serialize($value));
110 if (is_array($value)) {
111 foreach ($value as $p) {
113 return unserialize(serialize($value));
124 public function getExpires($key) {
125 return $this->_expires
[$key] ?
: NULL;