(dev/core#174) Add concrete classes for cache exceptions
[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 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
42 use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation
43
44 /**
45 * The host name of the memcached server.
46 *
47 * @var string
48 */
49 protected $group;
50
51 /**
52 * @var int $componentID The optional component ID (so componenets can share the same name space)
53 */
54 protected $componentID;
55
56 /**
57 * @var array in-memory cache to optimize redundant get()s
58 */
59 protected $frontCache;
60
61 /**
62 * Constructor.
63 *
64 * @param array $config
65 * An array of configuration params.
66 * - group: string
67 * - componentID: int
68 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
69 *
70 * @throws RuntimeException
71 * @return \CRM_Utils_Cache_SqlGroup
72 */
73 public function __construct($config) {
74 if (isset($config['group'])) {
75 $this->group = $config['group'];
76 }
77 else {
78 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
79 }
80 if (isset($config['componentID'])) {
81 $this->componentID = $config['componentID'];
82 }
83 else {
84 $this->componentID = NULL;
85 }
86 $this->frontCache = array();
87 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
88 $this->prefetch();
89 }
90 }
91
92 /**
93 * @param string $key
94 * @param mixed $value
95 * @param null|int|\DateInterval $ttl
96 * @return bool
97 */
98 public function set($key, $value, $ttl = NULL) {
99 if ($ttl !== NULL) {
100 throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL");
101 }
102 CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID);
103 $this->frontCache[$key] = $value;
104 return TRUE;
105 }
106
107 /**
108 * @param string $key
109 * @param mixed $default
110 *
111 * @return mixed
112 */
113 public function get($key, $default = NULL) {
114 if ($default !== NULL) {
115 throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default");
116 }
117 if (!array_key_exists($key, $this->frontCache)) {
118 $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID);
119 }
120 return $this->frontCache[$key];
121 }
122
123 /**
124 * @param $key
125 * @param null $default
126 *
127 * @return mixed
128 */
129 public function getFromFrontCache($key, $default = NULL) {
130 return CRM_Utils_Array::value($key, $this->frontCache, $default);
131 }
132
133 /**
134 * @param string $key
135 * @return bool
136 */
137 public function delete($key) {
138 CRM_Core_BAO_Cache::deleteGroup($this->group, $key, FALSE);
139 CRM_Core_BAO_Cache::$_cache = NULL; // FIXME: remove multitier cache
140 CRM_Utils_Cache::singleton()->flush(); // FIXME: remove multitier cache
141 unset($this->frontCache[$key]);
142 return TRUE;
143 }
144
145 public function flush() {
146 CRM_Core_BAO_Cache::deleteGroup($this->group, NULL, FALSE);
147 CRM_Core_BAO_Cache::$_cache = NULL; // FIXME: remove multitier cache
148 CRM_Utils_Cache::singleton()->flush(); // FIXME: remove multitier cache
149 $this->frontCache = array();
150 return TRUE;
151 }
152
153 public function clear() {
154 return $this->flush();
155 }
156
157 public function prefetch() {
158 $this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID);
159 }
160
161 }