Commit | Line | Data |
---|---|---|
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 | */ | |
39 | class 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 | ||
162 | $dao->free(); | |
163 | ||
164 | $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dataSerialized); | |
165 | $this->expiresCache[$key] = $expires; | |
858451a9 | 166 | return TRUE; |
6a488035 TO |
167 | } |
168 | ||
5bc392e6 EM |
169 | /** |
170 | * @param string $key | |
2da67cc5 | 171 | * @param mixed $default |
5bc392e6 EM |
172 | * |
173 | * @return mixed | |
174 | */ | |
2da67cc5 | 175 | public function get($key, $default = NULL) { |
be5eea0d TO |
176 | CRM_Utils_Cache::assertValidKey($key); |
177 | if (!isset($this->expiresCache[$key]) || time() >= $this->expiresCache[$key]) { | |
178 | $sql = "SELECT path, data, UNIX_TIMESTAMP(expired_date) as expires FROM {$this->table} WHERE " . $this->where($key); | |
179 | $dao = CRM_Core_DAO::executeQuery($sql); | |
180 | while ($dao->fetch()) { | |
181 | $this->expiresCache[$key] = $dao->expires; | |
182 | $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dao->data); | |
183 | } | |
184 | $dao->free(); | |
6a488035 | 185 | } |
be5eea0d TO |
186 | return (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]) ? $this->reobjectify($this->valueCache[$key]) : $default; |
187 | } | |
188 | ||
189 | private function reobjectify($value) { | |
190 | return is_object($value) ? unserialize(serialize($value)) : $value; | |
6a488035 TO |
191 | } |
192 | ||
5bc392e6 EM |
193 | /** |
194 | * @param $key | |
195 | * @param null $default | |
196 | * | |
197 | * @return mixed | |
198 | */ | |
00be9182 | 199 | public function getFromFrontCache($key, $default = NULL) { |
be5eea0d TO |
200 | if (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key] && $this->valueCache[$key]) { |
201 | return $this->reobjectify($this->valueCache[$key]); | |
202 | } | |
203 | else { | |
204 | return $default; | |
205 | } | |
206 | } | |
207 | ||
208 | public function has($key) { | |
209 | $this->get($key); | |
210 | return isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]; | |
20b015e1 TO |
211 | } |
212 | ||
5bc392e6 EM |
213 | /** |
214 | * @param string $key | |
eec321a4 | 215 | * @return bool |
5bc392e6 | 216 | */ |
00be9182 | 217 | public function delete($key) { |
be5eea0d TO |
218 | CRM_Utils_Cache::assertValidKey($key); |
219 | CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}"); | |
220 | unset($this->valueCache[$key]); | |
221 | unset($this->expiresCache[$key]); | |
eec321a4 | 222 | return TRUE; |
6a488035 TO |
223 | } |
224 | ||
00be9182 | 225 | public function flush() { |
be5eea0d | 226 | CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}"); |
be2fb01f CW |
227 | $this->valueCache = []; |
228 | $this->expiresCache = []; | |
124e5288 | 229 | return TRUE; |
6a488035 TO |
230 | } |
231 | ||
c31de879 TO |
232 | public function clear() { |
233 | return $this->flush(); | |
234 | } | |
235 | ||
00be9182 | 236 | public function prefetch() { |
be5eea0d | 237 | $dao = CRM_Core_DAO::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL)); |
be2fb01f CW |
238 | $this->valueCache = []; |
239 | $this->expiresCache = []; | |
be5eea0d TO |
240 | while ($dao->fetch()) { |
241 | $this->valueCache[$dao->path] = CRM_Core_BAO_Cache::decode($dao->data); | |
242 | $this->expiresCache[$dao->path] = $dao->expires; | |
243 | } | |
244 | $dao->free(); | |
245 | } | |
246 | ||
247 | protected function where($path = NULL) { | |
be2fb01f | 248 | $clauses = []; |
be5eea0d TO |
249 | $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($this->group) . '"'); |
250 | if ($path) { | |
251 | $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"'); | |
252 | } | |
253 | return $clauses ? implode(' AND ', $clauses) : '(1)'; | |
6a488035 | 254 | } |
96025800 | 255 | |
6a488035 | 256 | } |