Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
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 | ||
be5eea0d TO |
41 | // 6*60*60 |
42 | const DEFAULT_TTL = 21600; | |
43 | ||
44 | const TS_FMT = 'Y-m-d H:i:s'; | |
0d64c8fa TO |
45 | use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. |
46 | ||
6a488035 | 47 | /** |
fe482240 | 48 | * The host name of the memcached server. |
6a488035 TO |
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; | |
f813f78e | 58 | |
6a488035 TO |
59 | /** |
60 | * @var array in-memory cache to optimize redundant get()s | |
61 | */ | |
be5eea0d TO |
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; | |
6a488035 TO |
74 | |
75 | /** | |
fe482240 | 76 | * Constructor. |
6a488035 | 77 | * |
77855840 TO |
78 | * @param array $config |
79 | * An array of configuration params. | |
6a488035 TO |
80 | * - group: string |
81 | * - componentID: int | |
82 | * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE | |
83 | * | |
77b97be7 EM |
84 | * @throws RuntimeException |
85 | * @return \CRM_Utils_Cache_SqlGroup | |
6a488035 | 86 | */ |
00be9182 | 87 | public function __construct($config) { |
be5eea0d | 88 | $this->table = CRM_Core_DAO_Cache::getTableName(); |
6a488035 TO |
89 | if (isset($config['group'])) { |
90 | $this->group = $config['group']; | |
0db6c3e1 TO |
91 | } |
92 | else { | |
6a488035 TO |
93 | throw new RuntimeException("Cannot construct SqlGroup cache: missing group"); |
94 | } | |
95 | if (isset($config['componentID'])) { | |
96 | $this->componentID = $config['componentID']; | |
0db6c3e1 TO |
97 | } |
98 | else { | |
6a488035 TO |
99 | $this->componentID = NULL; |
100 | } | |
be5eea0d | 101 | $this->valueCache = array(); |
6a488035 TO |
102 | if (CRM_Utils_Array::value('prefetch', $config, TRUE)) { |
103 | $this->prefetch(); | |
104 | } | |
105 | } | |
106 | ||
5bc392e6 EM |
107 | /** |
108 | * @param string $key | |
109 | * @param mixed $value | |
858451a9 TO |
110 | * @param null|int|\DateInterval $ttl |
111 | * @return bool | |
5bc392e6 | 112 | */ |
858451a9 | 113 | public function set($key, $value, $ttl = NULL) { |
be5eea0d TO |
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 | ||
13ca7ebf TO |
121 | if (is_int($ttl) && $ttl <= 0) { |
122 | return $this->delete($key); | |
123 | } | |
124 | ||
be5eea0d | 125 | $dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}"); |
13ca7ebf | 126 | $expires = round(microtime(1)) + CRM_Utils_Date::convertCacheTtl($ttl, self::DEFAULT_TTL); |
be5eea0d TO |
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); | |
858451a9 | 151 | } |
be5eea0d TO |
152 | |
153 | $lock->release(); | |
154 | ||
155 | $dao->free(); | |
156 | ||
157 | $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dataSerialized); | |
158 | $this->expiresCache[$key] = $expires; | |
858451a9 | 159 | return TRUE; |
6a488035 TO |
160 | } |
161 | ||
5bc392e6 EM |
162 | /** |
163 | * @param string $key | |
2da67cc5 | 164 | * @param mixed $default |
5bc392e6 EM |
165 | * |
166 | * @return mixed | |
167 | */ | |
2da67cc5 | 168 | public function get($key, $default = NULL) { |
be5eea0d TO |
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(); | |
6a488035 | 178 | } |
be5eea0d TO |
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; | |
6a488035 TO |
184 | } |
185 | ||
5bc392e6 EM |
186 | /** |
187 | * @param $key | |
188 | * @param null $default | |
189 | * | |
190 | * @return mixed | |
191 | */ | |
00be9182 | 192 | public function getFromFrontCache($key, $default = NULL) { |
be5eea0d TO |
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]; | |
20b015e1 TO |
204 | } |
205 | ||
5bc392e6 EM |
206 | /** |
207 | * @param string $key | |
eec321a4 | 208 | * @return bool |
5bc392e6 | 209 | */ |
00be9182 | 210 | public function delete($key) { |
be5eea0d TO |
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]); | |
eec321a4 | 215 | return TRUE; |
6a488035 TO |
216 | } |
217 | ||
00be9182 | 218 | public function flush() { |
be5eea0d TO |
219 | CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}"); |
220 | $this->valueCache = array(); | |
221 | $this->expiresCache = array(); | |
124e5288 | 222 | return TRUE; |
6a488035 TO |
223 | } |
224 | ||
c31de879 TO |
225 | public function clear() { |
226 | return $this->flush(); | |
227 | } | |
228 | ||
00be9182 | 229 | public function prefetch() { |
be5eea0d TO |
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)'; | |
6a488035 | 247 | } |
96025800 | 248 | |
6a488035 | 249 | } |