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 * 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.
23 class CRM_Utils_Cache_SqlGroup
implements CRM_Utils_Cache_Interface
{
26 const DEFAULT_TTL
= 21600;
28 const TS_FMT
= 'Y-m-d H:i:s';
29 // TODO Consider native implementation.
30 use CRM_Utils_Cache_NaiveMultipleTrait
;
33 * The host name of the memcached server.
42 protected $componentID;
45 * In-memory cache to optimize redundant get()s.
49 protected $valueCache;
52 * In-memory cache to optimize redundant get()s.
55 * Note: expiresCache[$key]===NULL means cache-miss
57 protected $expiresCache;
69 * @param array $config
70 * An array of configuration params.
73 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
75 * @throws RuntimeException
76 * @return \CRM_Utils_Cache_SqlGroup
78 public function __construct($config) {
79 $this->table
= CRM_Core_DAO_Cache
::getTableName();
80 if (isset($config['group'])) {
81 $this->group
= $config['group'];
84 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
86 if (isset($config['componentID'])) {
87 $this->componentID
= $config['componentID'];
90 $this->componentID
= NULL;
92 $this->valueCache
= [];
93 if (CRM_Utils_Array
::value('prefetch', $config, TRUE)) {
100 * @param mixed $value
101 * @param null|int|\DateInterval $ttl
105 * @throws \CRM_Core_Exception
106 * @throws \CRM_Utils_Cache_CacheException
107 * @throws \CRM_Utils_Cache_InvalidArgumentException
109 public function set($key, $value, $ttl = NULL) {
110 CRM_Utils_Cache
::assertValidKey($key);
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.");
117 if (is_int($ttl) && $ttl <= 0) {
118 return $this->delete($key);
121 $dataExists = CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}");
122 $expires = round(microtime(1)) + CRM_Utils_Date
::convertCacheTtl($ttl, self
::DEFAULT_TTL
);
124 $dataSerialized = CRM_Core_BAO_Cache
::encode($value);
126 // This table has a wonky index, so we cannot use REPLACE or
127 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
129 $sql = "UPDATE {$this->table} SET data = %1, created_date = FROM_UNIXTIME(%2), expired_date = FROM_UNIXTIME(%3) WHERE {$this->where($key)}";
131 1 => [$dataSerialized, 'String'],
132 2 => [time(), 'Positive'],
133 3 => [$expires, 'Positive'],
135 CRM_Core_DAO
::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
138 $sql = "INSERT INTO {$this->table} (group_name,path,data,created_date,expired_date) VALUES (%1,%2,%3,FROM_UNIXTIME(%4),FROM_UNIXTIME(%5))";
140 1 => [(string) $this->group
, 'String'],
141 2 => [$key, 'String'],
142 3 => [$dataSerialized, 'String'],
143 4 => [time(), 'Positive'],
144 5 => [$expires, 'Positive'],
146 CRM_Core_DAO
::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
151 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dataSerialized);
152 $this->expiresCache
[$key] = $expires;
158 * @param mixed $default
162 * @throws \CRM_Utils_Cache_InvalidArgumentException
164 public function get($key, $default = NULL) {
165 CRM_Utils_Cache
::assertValidKey($key);
166 if (!isset($this->expiresCache
[$key]) ||
time() >= $this->expiresCache
[$key]) {
167 $sql = "SELECT path, data, UNIX_TIMESTAMP(expired_date) as expires FROM {$this->table} WHERE " . $this->where($key);
168 $dao = CRM_Core_DAO
::executeQuery($sql);
169 while ($dao->fetch()) {
170 $this->expiresCache
[$key] = $dao->expires
;
171 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dao->data
);
174 return (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key]) ?
$this->reobjectify($this->valueCache
[$key]) : $default;
178 * @param mixed $value
182 private function reobjectify($value) {
183 return is_object($value) ?
unserialize(serialize($value)) : $value;
188 * @param null $default
192 public function getFromFrontCache($key, $default = NULL) {
193 if (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key] && $this->valueCache
[$key]) {
194 return $this->reobjectify($this->valueCache
[$key]);
201 public function has($key) {
203 return isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key];
210 * @throws \CRM_Utils_Cache_InvalidArgumentException
212 public function delete($key) {
213 CRM_Utils_Cache
::assertValidKey($key);
214 // If we are triggering a deletion of a prevNextCache key in the civicrm_cache tabl
215 // Alssure that the relevant prev_next_cache values are also removed.
216 if ($this->group
== CRM_Utils_Cache
::cleanKey('CiviCRM Search PrevNextCache')) {
217 Civi
::service('prevnext')->deleteItem(NULL, $key);
219 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}");
220 unset($this->valueCache
[$key]);
221 unset($this->expiresCache
[$key]);
225 public function flush() {
226 if ($this->group
== CRM_Utils_Cache
::cleanKey('CiviCRM Search PrevNextCache') &&
227 Civi
::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql
) {
228 Civi
::service('prevnext')->cleanup();
231 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
233 $this->valueCache
= [];
234 $this->expiresCache
= [];
238 public function clear() {
239 return $this->flush();
242 public function prefetch() {
243 $dao = CRM_Core_DAO
::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL));
244 $this->valueCache
= [];
245 $this->expiresCache
= [];
246 while ($dao->fetch()) {
247 $this->valueCache
[$dao->path
] = CRM_Core_BAO_Cache
::decode($dao->data
);
248 $this->expiresCache
[$dao->path
] = $dao->expires
;
252 protected function where($path = NULL) {
254 $clauses[] = ('group_name = "' . CRM_Core_DAO
::escapeString($this->group
) . '"');
256 $clauses[] = ('path = "' . CRM_Core_DAO
::escapeString($path) . '"');
258 return $clauses ?
implode(' AND ', $clauses) : '(1)';