Merge pull request #23912 from mlutfy/reportVars
[civicrm-core.git] / CRM / Utils / Cache / SqlGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This caching provider stores all cached items as a "group" in the
20 * "civicrm_cache" table. The entire 'group' may be prefetched when
21 * instantiating the cache provider.
22 */
23 class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
24
25 // 6*60*60
26 const DEFAULT_TTL = 21600;
27
28 const TS_FMT = 'Y-m-d H:i:s';
29 // TODO Consider native implementation.
30 use CRM_Utils_Cache_NaiveMultipleTrait;
31
32 /**
33 * The host name of the memcached server.
34 *
35 * @var string
36 */
37 protected $group;
38
39 /**
40 * @var int
41 */
42 protected $componentID;
43
44 /**
45 * In-memory cache to optimize redundant get()s.
46 *
47 * @var array
48 */
49 protected $valueCache;
50
51 /**
52 * In-memory cache to optimize redundant get()s.
53 *
54 * @var array
55 * Note: expiresCache[$key]===NULL means cache-miss
56 */
57 protected $expiresCache;
58
59 /**
60 * Table.
61 *
62 * @var string
63 */
64 protected $table;
65
66 /**
67 * Constructor.
68 *
69 * @param array $config
70 * An array of configuration params.
71 * - group: string
72 * - componentID: int
73 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
74 *
75 * @throws RuntimeException
76 * @return \CRM_Utils_Cache_SqlGroup
77 */
78 public function __construct($config) {
79 $this->table = CRM_Core_DAO_Cache::getTableName();
80 if (isset($config['group'])) {
81 $this->group = $config['group'];
82 }
83 else {
84 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
85 }
86 if (isset($config['componentID'])) {
87 $this->componentID = $config['componentID'];
88 }
89 else {
90 $this->componentID = NULL;
91 }
92 $this->valueCache = [];
93 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
94 $this->prefetch();
95 }
96 }
97
98 /**
99 * @param string $key
100 * @param mixed $value
101 * @param null|int|\DateInterval $ttl
102 *
103 * @return bool
104 *
105 * @throws \CRM_Core_Exception
106 * @throws \CRM_Utils_Cache_CacheException
107 * @throws \CRM_Utils_Cache_InvalidArgumentException
108 */
109 public function set($key, $value, $ttl = NULL) {
110 CRM_Utils_Cache::assertValidKey($key);
111
112 $lock = Civi::lockManager()->acquire("cache.{$this->group}_{$key}._null");
113 if (!$lock->isAcquired()) {
114 throw new \CRM_Utils_Cache_CacheException("SqlGroup: Failed to acquire lock on cache key.");
115 }
116
117 if (is_int($ttl) && $ttl <= 0) {
118 $result = $this->delete($key);
119 $lock->release();
120 return $result;
121 }
122
123 $dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}");
124 $expires = round(microtime(1)) + CRM_Utils_Date::convertCacheTtl($ttl, self::DEFAULT_TTL);
125
126 $dataSerialized = CRM_Core_BAO_Cache::encode($value);
127
128 // This table has a wonky index, so we cannot use REPLACE or
129 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
130 if ($dataExists) {
131 $sql = "UPDATE {$this->table} SET data = %1, created_date = FROM_UNIXTIME(%2), expired_date = FROM_UNIXTIME(%3) WHERE {$this->where($key)}";
132 $args = [
133 1 => [$dataSerialized, 'String'],
134 2 => [time(), 'Positive'],
135 3 => [$expires, 'Positive'],
136 ];
137 CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
138 }
139 else {
140 $sql = "INSERT INTO {$this->table} (group_name,path,data,created_date,expired_date) VALUES (%1,%2,%3,FROM_UNIXTIME(%4),FROM_UNIXTIME(%5))";
141 $args = [
142 1 => [(string) $this->group, 'String'],
143 2 => [$key, 'String'],
144 3 => [$dataSerialized, 'String'],
145 4 => [time(), 'Positive'],
146 5 => [$expires, 'Positive'],
147 ];
148 CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
149 }
150
151 $lock->release();
152
153 $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dataSerialized);
154 $this->expiresCache[$key] = $expires;
155 return TRUE;
156 }
157
158 /**
159 * @param string $key
160 * @param mixed $default
161 *
162 * @return mixed
163 *
164 * @throws \CRM_Utils_Cache_InvalidArgumentException
165 */
166 public function get($key, $default = NULL) {
167 CRM_Utils_Cache::assertValidKey($key);
168 if (!isset($this->expiresCache[$key]) || time() >= $this->expiresCache[$key]) {
169 $sql = "SELECT path, data, UNIX_TIMESTAMP(expired_date) as expires FROM {$this->table} WHERE " . $this->where($key);
170 $dao = CRM_Core_DAO::executeQuery($sql);
171 while ($dao->fetch()) {
172 $this->expiresCache[$key] = $dao->expires;
173 $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dao->data);
174 }
175 }
176 return (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]) ? $this->reobjectify($this->valueCache[$key]) : $default;
177 }
178
179 /**
180 * @param mixed $value
181 *
182 * @return object
183 */
184 private function reobjectify($value) {
185 return is_object($value) ? unserialize(serialize($value)) : $value;
186 }
187
188 /**
189 * @param string $key
190 * @param mixed $default
191 *
192 * @return mixed
193 */
194 public function getFromFrontCache($key, $default = NULL) {
195 if (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key] && $this->valueCache[$key]) {
196 return $this->reobjectify($this->valueCache[$key]);
197 }
198 else {
199 return $default;
200 }
201 }
202
203 public function has($key) {
204 $this->get($key);
205 return isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key];
206 }
207
208 /**
209 * @param string $key
210 *
211 * @return bool
212 * @throws \CRM_Utils_Cache_InvalidArgumentException
213 */
214 public function delete($key) {
215 CRM_Utils_Cache::assertValidKey($key);
216 // If we are triggering a deletion of a prevNextCache key in the civicrm_cache tabl
217 // Alssure that the relevant prev_next_cache values are also removed.
218 if ($this->group == CRM_Utils_Cache::cleanKey('CiviCRM Search PrevNextCache')) {
219 Civi::service('prevnext')->deleteItem(NULL, $key);
220 }
221 CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}");
222 unset($this->valueCache[$key]);
223 unset($this->expiresCache[$key]);
224 return TRUE;
225 }
226
227 public function flush() {
228 if ($this->group == CRM_Utils_Cache::cleanKey('CiviCRM Search PrevNextCache') &&
229 Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
230 Civi::service('prevnext')->cleanup();
231 }
232 else {
233 CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
234 }
235 $this->valueCache = [];
236 $this->expiresCache = [];
237 return TRUE;
238 }
239
240 public function clear() {
241 return $this->flush();
242 }
243
244 public function prefetch() {
245 $dao = CRM_Core_DAO::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL));
246 $this->valueCache = [];
247 $this->expiresCache = [];
248 while ($dao->fetch()) {
249 $this->valueCache[$dao->path] = CRM_Core_BAO_Cache::decode($dao->data);
250 $this->expiresCache[$dao->path] = $dao->expires;
251 }
252 }
253
254 protected function where($path = NULL) {
255 $clauses = [];
256 $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($this->group) . '"');
257 if ($path) {
258 $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"');
259 }
260 return $clauses ? implode(' AND ', $clauses) : '(1)';
261 }
262
263 }