3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
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.
39 class CRM_Utils_Cache_SqlGroup
implements CRM_Utils_Cache_Interface
{
42 const DEFAULT_TTL
= 21600;
44 const TS_FMT
= 'Y-m-d H:i:s';
45 // TODO Consider native implementation.
46 use CRM_Utils_Cache_NaiveMultipleTrait
;
49 * The host name of the memcached server.
58 protected $componentID;
61 * In-memory cache to optimize redundant get()s.
65 protected $valueCache;
68 * In-memory cache to optimize redundant get()s.
71 * Note: expiresCache[$key]===NULL means cache-miss
73 protected $expiresCache;
85 * @param array $config
86 * An array of configuration params.
89 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
91 * @throws RuntimeException
92 * @return \CRM_Utils_Cache_SqlGroup
94 public function __construct($config) {
95 $this->table
= CRM_Core_DAO_Cache
::getTableName();
96 if (isset($config['group'])) {
97 $this->group
= $config['group'];
100 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
102 if (isset($config['componentID'])) {
103 $this->componentID
= $config['componentID'];
106 $this->componentID
= NULL;
108 $this->valueCache
= [];
109 if (CRM_Utils_Array
::value('prefetch', $config, TRUE)) {
116 * @param mixed $value
117 * @param null|int|\DateInterval $ttl
120 public function set($key, $value, $ttl = NULL) {
121 CRM_Utils_Cache
::assertValidKey($key);
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.");
128 if (is_int($ttl) && $ttl <= 0) {
129 return $this->delete($key);
132 $dataExists = CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}");
133 $expires = round(microtime(1)) + CRM_Utils_Date
::convertCacheTtl($ttl, self
::DEFAULT_TTL
);
135 $dataSerialized = CRM_Core_BAO_Cache
::encode($value);
137 // This table has a wonky index, so we cannot use REPLACE or
138 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
140 $sql = "UPDATE {$this->table} SET data = %1, created_date = FROM_UNIXTIME(%2), expired_date = FROM_UNIXTIME(%3) WHERE {$this->where($key)}";
142 1 => [$dataSerialized, 'String'],
143 2 => [time(), 'Positive'],
144 3 => [$expires, 'Positive'],
146 $dao = CRM_Core_DAO
::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
149 $sql = "INSERT INTO {$this->table} (group_name,path,data,created_date,expired_date) VALUES (%1,%2,%3,FROM_UNIXTIME(%4),FROM_UNIXTIME(%5))";
151 1 => [$this->group
, 'String'],
152 2 => [$key, 'String'],
153 3 => [$dataSerialized, 'String'],
154 4 => [time(), 'Positive'],
155 5 => [$expires, 'Positive'],
157 $dao = CRM_Core_DAO
::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
162 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dataSerialized);
163 $this->expiresCache
[$key] = $expires;
169 * @param mixed $default
173 public function get($key, $default = NULL) {
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
);
183 return (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key]) ?
$this->reobjectify($this->valueCache
[$key]) : $default;
186 private function reobjectify($value) {
187 return is_object($value) ?
unserialize(serialize($value)) : $value;
192 * @param null $default
196 public function getFromFrontCache($key, $default = NULL) {
197 if (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key] && $this->valueCache
[$key]) {
198 return $this->reobjectify($this->valueCache
[$key]);
205 public function has($key) {
207 return isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key];
214 public function delete($key) {
215 CRM_Utils_Cache
::assertValidKey($key);
216 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}");
217 unset($this->valueCache
[$key]);
218 unset($this->expiresCache
[$key]);
222 public function flush() {
223 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
224 $this->valueCache
= [];
225 $this->expiresCache
= [];
229 public function clear() {
230 return $this->flush();
233 public function prefetch() {
234 $dao = CRM_Core_DAO
::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL));
235 $this->valueCache
= [];
236 $this->expiresCache
= [];
237 while ($dao->fetch()) {
238 $this->valueCache
[$dao->path
] = CRM_Core_BAO_Cache
::decode($dao->data
);
239 $this->expiresCache
[$dao->path
] = $dao->expires
;
243 protected function where($path = NULL) {
245 $clauses[] = ('group_name = "' . CRM_Core_DAO
::escapeString($this->group
) . '"');
247 $clauses[] = ('path = "' . CRM_Core_DAO
::escapeString($path) . '"');
249 return $clauses ?
implode(' AND ', $clauses) : '(1)';