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);
164 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dataSerialized);
165 $this->expiresCache
[$key] = $expires;
171 * @param mixed $default
175 public function get($key, $default = NULL) {
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
);
186 return (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key]) ?
$this->reobjectify($this->valueCache
[$key]) : $default;
189 private function reobjectify($value) {
190 return is_object($value) ?
unserialize(serialize($value)) : $value;
195 * @param null $default
199 public function getFromFrontCache($key, $default = NULL) {
200 if (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key] && $this->valueCache
[$key]) {
201 return $this->reobjectify($this->valueCache
[$key]);
208 public function has($key) {
210 return isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key];
217 public function delete($key) {
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]);
225 public function flush() {
226 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
227 $this->valueCache
= [];
228 $this->expiresCache
= [];
232 public function clear() {
233 return $this->flush();
236 public function prefetch() {
237 $dao = CRM_Core_DAO
::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL));
238 $this->valueCache
= [];
239 $this->expiresCache
= [];
240 while ($dao->fetch()) {
241 $this->valueCache
[$dao->path
] = CRM_Core_BAO_Cache
::decode($dao->data
);
242 $this->expiresCache
[$dao->path
] = $dao->expires
;
247 protected function where($path = NULL) {
249 $clauses[] = ('group_name = "' . CRM_Core_DAO
::escapeString($this->group
) . '"');
251 $clauses[] = ('path = "' . CRM_Core_DAO
::escapeString($path) . '"');
253 return $clauses ?
implode(' AND ', $clauses) : '(1)';