Merge pull request #12392 from madhavimalgaonkar/issue-221
[civicrm-core.git] / CRM / Utils / Cache / SqlGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
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
41 // 6*60*60
42 const DEFAULT_TTL = 21600;
43
44 const TS_FMT = 'Y-m-d H:i:s';
45 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
46
47 /**
48 * The host name of the memcached server.
49 *
50 * @var string
51 */
52 protected $group;
53
54 /**
55 * @var int $componentID The optional component ID (so componenets can share the same name space)
56 */
57 protected $componentID;
58
59 /**
60 * @var array in-memory cache to optimize redundant get()s
61 */
62 protected $valueCache;
63
64 /**
65 * @var array in-memory cache to optimize redundant get()s
66 * Note: expiresCache[$key]===NULL means cache-miss
67 */
68 protected $expiresCache;
69
70 /**
71 * @var string
72 */
73 protected $table;
74
75 /**
76 * Constructor.
77 *
78 * @param array $config
79 * An array of configuration params.
80 * - group: string
81 * - componentID: int
82 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
83 *
84 * @throws RuntimeException
85 * @return \CRM_Utils_Cache_SqlGroup
86 */
87 public function __construct($config) {
88 $this->table = CRM_Core_DAO_Cache::getTableName();
89 if (isset($config['group'])) {
90 $this->group = $config['group'];
91 }
92 else {
93 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
94 }
95 if (isset($config['componentID'])) {
96 $this->componentID = $config['componentID'];
97 }
98 else {
99 $this->componentID = NULL;
100 }
101 $this->valueCache = array();
102 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
103 $this->prefetch();
104 }
105 }
106
107 /**
108 * @param string $key
109 * @param mixed $value
110 * @param null|int|\DateInterval $ttl
111 * @return bool
112 */
113 public function set($key, $value, $ttl = NULL) {
114 CRM_Utils_Cache::assertValidKey($key);
115
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.");
119 }
120
121 if (is_int($ttl) && $ttl <= 0) {
122 return $this->delete($key);
123 }
124
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);
127
128 $dataSerialized = CRM_Core_BAO_Cache::encode($value);
129
130 // This table has a wonky index, so we cannot use REPLACE or
131 // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
132 if ($dataExists) {
133 $sql = "UPDATE {$this->table} SET data = %1, created_date = FROM_UNIXTIME(%2), expired_date = FROM_UNIXTIME(%3) WHERE {$this->where($key)}";
134 $args = array(
135 1 => array($dataSerialized, 'String'),
136 2 => array(time(), 'Positive'),
137 3 => array($expires, 'Positive'),
138 );
139 $dao = CRM_Core_DAO::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
140 }
141 else {
142 $sql = "INSERT INTO {$this->table} (group_name,path,data,created_date,expired_date) VALUES (%1,%2,%3,FROM_UNIXTIME(%4),FROM_UNIXTIME(%5))";
143 $args = array(
144 1 => [$this->group, 'String'],
145 2 => [$key, 'String'],
146 3 => [$dataSerialized, 'String'],
147 4 => [time(), 'Positive'],
148 5 => [$expires, 'Positive'],
149 );
150 $dao = CRM_Core_DAO::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE);
151 }
152
153 $lock->release();
154
155 $dao->free();
156
157 $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dataSerialized);
158 $this->expiresCache[$key] = $expires;
159 return TRUE;
160 }
161
162 /**
163 * @param string $key
164 * @param mixed $default
165 *
166 * @return mixed
167 */
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);
176 }
177 $dao->free();
178 }
179 return (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]) ? $this->reobjectify($this->valueCache[$key]) : $default;
180 }
181
182 private function reobjectify($value) {
183 return is_object($value) ? unserialize(serialize($value)) : $value;
184 }
185
186 /**
187 * @param $key
188 * @param null $default
189 *
190 * @return mixed
191 */
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]);
195 }
196 else {
197 return $default;
198 }
199 }
200
201 public function has($key) {
202 $this->get($key);
203 return isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key];
204 }
205
206 /**
207 * @param string $key
208 * @return bool
209 */
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]);
215 return TRUE;
216 }
217
218 public function flush() {
219 CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}");
220 $this->valueCache = array();
221 $this->expiresCache = array();
222 return TRUE;
223 }
224
225 public function clear() {
226 return $this->flush();
227 }
228
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;
236 }
237 $dao->free();
238 }
239
240 protected function where($path = NULL) {
241 $clauses = array();
242 $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($this->group) . '"');
243 if ($path) {
244 $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"');
245 }
246 return $clauses ? implode(' AND ', $clauses) : '(1)';
247 }
248
249 }