Merge pull request #15838 from demeritcowboy/getcasereport-split
[civicrm-core.git] / CRM / Utils / Cache / ArrayCache.php
CommitLineData
6a488035 1<?php
50bfb460
SB
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
50bfb460 5 | |
bc77d7c0
TO
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 |
50bfb460
SB
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
50bfb460 16 */
5bc392e6
EM
17
18/**
c33f1df1 19 * Class CRM_Utils_Cache_ArrayCache
5bc392e6 20 */
c33f1df1 21class CRM_Utils_Cache_ArrayCache implements CRM_Utils_Cache_Interface {
6a488035 22
0d64c8fa 23 use CRM_Utils_Cache_NaiveMultipleTrait;
6714d8d2
SL
24 // TODO Native implementation
25 use CRM_Utils_Cache_NaiveHasTrait;
0d64c8fa 26
b5d3f3c5
TO
27 const DEFAULT_TIMEOUT = 3600;
28
6a488035
TO
29 /**
30 * The cache storage container, an in memory array by default
6714d8d2 31 * @var array
6a488035 32 */
1606b7e9 33 protected $_cache;
6a488035 34
b5d3f3c5
TO
35 protected $_expires;
36
6a488035 37 /**
fe482240 38 * Constructor.
6a488035 39 *
77855840
TO
40 * @param array $config
41 * An array of configuration params.
6a488035 42 *
c33f1df1 43 * @return \CRM_Utils_Cache_ArrayCache
6a488035 44 */
00be9182 45 public function __construct($config) {
be2fb01f
CW
46 $this->_cache = [];
47 $this->_expires = [];
6a488035
TO
48 }
49
5bc392e6
EM
50 /**
51 * @param string $key
52 * @param mixed $value
858451a9
TO
53 * @param null|int|\DateInterval $ttl
54 * @return bool
b5d3f3c5 55 * @throws \Psr\SimpleCache\InvalidArgumentException
5bc392e6 56 */
858451a9 57 public function set($key, $value, $ttl = NULL) {
b5d3f3c5
TO
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);
858451a9 61 return TRUE;
6a488035
TO
62 }
63
5bc392e6
EM
64 /**
65 * @param string $key
2da67cc5 66 * @param mixed $default
5bc392e6
EM
67 *
68 * @return mixed
b5d3f3c5 69 * @throws \Psr\SimpleCache\InvalidArgumentException
5bc392e6 70 */
2da67cc5 71 public function get($key, $default = NULL) {
b5d3f3c5
TO
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;
6a488035
TO
80 }
81
5bc392e6
EM
82 /**
83 * @param string $key
eec321a4 84 * @return bool
b5d3f3c5 85 * @throws \Psr\SimpleCache\InvalidArgumentException
5bc392e6 86 */
00be9182 87 public function delete($key) {
b5d3f3c5
TO
88 CRM_Utils_Cache::assertValidKey($key);
89
6a488035 90 unset($this->_cache[$key]);
b5d3f3c5 91 unset($this->_expires[$key]);
eec321a4 92 return TRUE;
6a488035
TO
93 }
94
00be9182 95 public function flush() {
6a488035 96 unset($this->_cache);
b5d3f3c5 97 unset($this->_expires);
be2fb01f 98 $this->_cache = [];
124e5288 99 return TRUE;
6a488035 100 }
96025800 101
c31de879
TO
102 public function clear() {
103 return $this->flush();
104 }
105
b5d3f3c5 106 private function reobjectify($value) {
3768d7a0
TO
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;
b5d3f3c5
TO
118 }
119
b510643f
TO
120 /**
121 * @param string $key
122 * @return int|null
123 */
124 public function getExpires($key) {
125 return $this->_expires[$key] ?: NULL;
126 }
127
6a488035 128}