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