3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 * The host name of the memcached server.
49 * @var int $componentID The optional component ID (so componenets can share the same name space)
51 protected $componentID;
54 * @var array in-memory cache to optimize redundant get()s
56 protected $frontCache;
61 * @param array $config
62 * An array of configuration params.
65 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
67 * @throws RuntimeException
68 * @return \CRM_Utils_Cache_SqlGroup
70 public function __construct($config) {
71 if (isset($config['group'])) {
72 $this->group
= $config['group'];
75 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
77 if (isset($config['componentID'])) {
78 $this->componentID
= $config['componentID'];
81 $this->componentID
= NULL;
83 $this->frontCache
= array();
84 if (CRM_Utils_Array
::value('prefetch', $config, TRUE)) {
92 * @param null|int|\DateInterval $ttl
95 public function set($key, $value, $ttl = NULL) {
97 throw new \
RuntimeException("FIXME: " . __CLASS__
. "::set() should support non-NULL TTL");
99 CRM_Core_BAO_Cache
::setItem($value, $this->group
, $key, $this->componentID
);
100 $this->frontCache
[$key] = $value;
106 * @param mixed $default
110 public function get($key, $default = NULL) {
111 if ($default !== NULL) {
112 throw new \
RuntimeException("FIXME: " . __CLASS__
. "::get() only supports NULL default");
114 if (!array_key_exists($key, $this->frontCache
)) {
115 $this->frontCache
[$key] = CRM_Core_BAO_Cache
::getItem($this->group
, $key, $this->componentID
);
117 return $this->frontCache
[$key];
122 * @param null $default
126 public function getFromFrontCache($key, $default = NULL) {
127 return CRM_Utils_Array
::value($key, $this->frontCache
, $default);
133 public function delete($key) {
134 CRM_Core_BAO_Cache
::deleteGroup($this->group
, $key, FALSE);
135 CRM_Core_BAO_Cache
::$_cache = NULL; // FIXME: remove multitier cache
136 CRM_Utils_Cache
::singleton()->flush(); // FIXME: remove multitier cache
137 unset($this->frontCache
[$key]);
140 public function flush() {
141 CRM_Core_BAO_Cache
::deleteGroup($this->group
, NULL, FALSE);
142 CRM_Core_BAO_Cache
::$_cache = NULL; // FIXME: remove multitier cache
143 CRM_Utils_Cache
::singleton()->flush(); // FIXME: remove multitier cache
144 $this->frontCache
= array();
147 public function prefetch() {
148 $this->frontCache
= CRM_Core_BAO_Cache
::getItems($this->group
, $this->componentID
);