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 use CRM_Utils_Cache_NaiveMultipleTrait
; // TODO Consider native implementation.
48 * The host name of the memcached server.
55 * @var int $componentID The optional component ID (so componenets can share the same name space)
57 protected $componentID;
60 * @var array in-memory cache to optimize redundant get()s
62 protected $valueCache;
65 * @var array in-memory cache to optimize redundant get()s
66 * Note: expiresCache[$key]===NULL means cache-miss
68 protected $expiresCache;
78 * @param array $config
79 * An array of configuration params.
82 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
84 * @throws RuntimeException
85 * @return \CRM_Utils_Cache_SqlGroup
87 public function __construct($config) {
88 $this->table
= CRM_Core_DAO_Cache
::getTableName();
89 if (isset($config['group'])) {
90 $this->group
= $config['group'];
93 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
95 if (isset($config['componentID'])) {
96 $this->componentID
= $config['componentID'];
99 $this->componentID
= NULL;
101 $this->valueCache
= array();
102 if (CRM_Utils_Array
::value('prefetch', $config, TRUE)) {
109 * @param mixed $value
110 * @param null|int|\DateInterval $ttl
113 public function set($key, $value, $ttl = NULL) {
114 CRM_Utils_Cache
::assertValidKey($key);
116 $lock = Civi
::lockManager()->acquire("cache.{$this->group}_{$key}._null");
117 if (!$lock->isAcquired()) {
118 throw new \
CRM_Utils_Cache_CacheException("SqlGroup: Failed to acquire lock on cache key.");
121 if (is_int($ttl) && $ttl <= 0) {
122 return $this->delete($key);
125 $dataExists = CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}");
126 $expires = round(microtime(1)) + CRM_Utils_Date
::convertCacheTtl($ttl, self
::DEFAULT_TTL
);
128 $dataSerialized = CRM_Core_BAO_Cache
::encode($value);
130 // This table has a wonky index, so we cannot use REPLACE or
131 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
133 $sql = "UPDATE {$this->table} SET data = %1, created_date = FROM_UNIXTIME(%2), expired_date = FROM_UNIXTIME(%3) WHERE {$this->where($key)}";
135 1 => array($dataSerialized, 'String'),
136 2 => array(time(), 'Positive'),
137 3 => array($expires, 'Positive'),
139 $dao = CRM_Core_DAO
::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
142 $sql = "INSERT INTO {$this->table} (group_name,path,data,created_date,expired_date) VALUES (%1,%2,%3,FROM_UNIXTIME(%4),FROM_UNIXTIME(%5))";
144 1 => [$this->group
, 'String'],
145 2 => [$key, 'String'],
146 3 => [$dataSerialized, 'String'],
147 4 => [time(), 'Positive'],
148 5 => [$expires, 'Positive'],
150 $dao = CRM_Core_DAO
::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
157 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dataSerialized);
158 $this->expiresCache
[$key] = $expires;
164 * @param mixed $default
168 public function get($key, $default = NULL) {
169 CRM_Utils_Cache
::assertValidKey($key);
170 if (!isset($this->expiresCache
[$key]) ||
time() >= $this->expiresCache
[$key]) {
171 $sql = "SELECT path, data, UNIX_TIMESTAMP(expired_date) as expires FROM {$this->table} WHERE " . $this->where($key);
172 $dao = CRM_Core_DAO
::executeQuery($sql);
173 while ($dao->fetch()) {
174 $this->expiresCache
[$key] = $dao->expires
;
175 $this->valueCache
[$key] = CRM_Core_BAO_Cache
::decode($dao->data
);
179 return (isset($this->expiresCache
[$key]) && time() < $this->expiresCache
[$key]) ?
$this->reobjectify($this->valueCache
[$key]) : $default;
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 public function delete($key) {
211 CRM_Utils_Cache
::assertValidKey($key);
212 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}");
213 unset($this->valueCache
[$key]);
214 unset($this->expiresCache
[$key]);
218 public function flush() {
219 CRM_Core_DAO
::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
220 $this->valueCache
= array();
221 $this->expiresCache
= array();
225 public function clear() {
226 return $this->flush();
229 public function prefetch() {
230 $dao = CRM_Core_DAO
::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL));
231 $this->valueCache
= array();
232 $this->expiresCache
= array();
233 while ($dao->fetch()) {
234 $this->valueCache
[$dao->path
] = CRM_Core_BAO_Cache
::decode($dao->data
);
235 $this->expiresCache
[$dao->path
] = $dao->expires
;
240 protected function where($path = NULL) {
242 $clauses[] = ('group_name = "' . CRM_Core_DAO
::escapeString($this->group
) . '"');
244 $clauses[] = ('path = "' . CRM_Core_DAO
::escapeString($path) . '"');
246 return $clauses ?
implode(' AND ', $clauses) : '(1)';