(dev/core#179) Redis - Only send AUTH if there's a password
[civicrm-core.git] / CRM / Utils / Cache / SqlGroup.php
CommitLineData
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 */
39class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface {
40
41 /**
fe482240 42 * The host name of the memcached server.
6a488035
TO
43 *
44 * @var string
45 */
46 protected $group;
47
48 /**
49 * @var int $componentID The optional component ID (so componenets can share the same name space)
50 */
51 protected $componentID;
f813f78e 52
6a488035
TO
53 /**
54 * @var array in-memory cache to optimize redundant get()s
55 */
56 protected $frontCache;
57
58 /**
fe482240 59 * Constructor.
6a488035 60 *
77855840
TO
61 * @param array $config
62 * An array of configuration params.
6a488035
TO
63 * - group: string
64 * - componentID: int
65 * - prefetch: bool, whether to preemptively read the entire cache group; default: TRUE
66 *
77b97be7
EM
67 * @throws RuntimeException
68 * @return \CRM_Utils_Cache_SqlGroup
6a488035 69 */
00be9182 70 public function __construct($config) {
6a488035
TO
71 if (isset($config['group'])) {
72 $this->group = $config['group'];
0db6c3e1
TO
73 }
74 else {
6a488035
TO
75 throw new RuntimeException("Cannot construct SqlGroup cache: missing group");
76 }
77 if (isset($config['componentID'])) {
78 $this->componentID = $config['componentID'];
0db6c3e1
TO
79 }
80 else {
6a488035
TO
81 $this->componentID = NULL;
82 }
83 $this->frontCache = array();
84 if (CRM_Utils_Array::value('prefetch', $config, TRUE)) {
85 $this->prefetch();
86 }
87 }
88
5bc392e6
EM
89 /**
90 * @param string $key
91 * @param mixed $value
92 */
00be9182 93 public function set($key, &$value) {
6a488035
TO
94 CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID);
95 $this->frontCache[$key] = $value;
96 }
97
5bc392e6
EM
98 /**
99 * @param string $key
100 *
101 * @return mixed
102 */
00be9182 103 public function get($key) {
353ffa53 104 if (!array_key_exists($key, $this->frontCache)) {
6a488035
TO
105 $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID);
106 }
107 return $this->frontCache[$key];
108 }
109
5bc392e6
EM
110 /**
111 * @param $key
112 * @param null $default
113 *
114 * @return mixed
115 */
00be9182 116 public function getFromFrontCache($key, $default = NULL) {
20b015e1
TO
117 return CRM_Utils_Array::value($key, $this->frontCache, $default);
118 }
119
5bc392e6
EM
120 /**
121 * @param string $key
122 */
00be9182 123 public function delete($key) {
6a488035
TO
124 CRM_Core_BAO_Cache::deleteGroup($this->group, $key);
125 unset($this->frontCache[$key]);
126 }
127
00be9182 128 public function flush() {
6a488035
TO
129 CRM_Core_BAO_Cache::deleteGroup($this->group);
130 $this->frontCache = array();
131 }
132
00be9182 133 public function prefetch() {
6a488035
TO
134 $this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID);
135 }
96025800 136
6a488035 137}