Merge pull request #12296 from eileenmcnaughton/greeting
[civicrm-core.git] / CRM / Utils / Cache / Memcache.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 class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
34
35 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
36
37 const DEFAULT_HOST = 'localhost';
38 const DEFAULT_PORT = 11211;
39 const DEFAULT_TIMEOUT = 3600;
40 const DEFAULT_PREFIX = '';
41
42 /**
43 * If another process clears namespace, we'll find out in ~5 sec.
44 */
45 const NS_LOCAL_TTL = 5;
46
47 /**
48 * The host name of the memcached server.
49 *
50 * @var string
51 */
52 protected $_host = self::DEFAULT_HOST;
53
54 /**
55 * The port on which to connect on.
56 *
57 * @var int
58 */
59 protected $_port = self::DEFAULT_PORT;
60
61 /**
62 * The default timeout to use.
63 *
64 * @var int
65 */
66 protected $_timeout = self::DEFAULT_TIMEOUT;
67
68 /**
69 * The prefix prepended to cache keys.
70 *
71 * If we are using the same memcache instance for multiple CiviCRM
72 * installs, we must have a unique prefix for each install to prevent
73 * the keys from clobbering each other.
74 *
75 * @var string
76 */
77 protected $_prefix = self::DEFAULT_PREFIX;
78
79 /**
80 * The actual memcache object.
81 *
82 * @var Memcache
83 */
84 protected $_cache;
85
86 /**
87 * @var NULL|array
88 *
89 * This is the effective prefix. It may be bumped up whenever the dataset is flushed.
90 *
91 * @see https://github.com/memcached/memcached/wiki/ProgrammingTricks#deleting-by-namespace
92 */
93 protected $_truePrefix = NULL;
94
95 /**
96 * Constructor.
97 *
98 * @param array $config
99 * An array of configuration params.
100 *
101 * @return \CRM_Utils_Cache_Memcache
102 */
103 public function __construct($config) {
104 if (isset($config['host'])) {
105 $this->_host = $config['host'];
106 }
107 if (isset($config['port'])) {
108 $this->_port = $config['port'];
109 }
110 if (isset($config['timeout'])) {
111 $this->_timeout = $config['timeout'];
112 }
113 if (isset($config['prefix'])) {
114 $this->_prefix = $config['prefix'];
115 }
116
117 $this->_cache = new Memcache();
118
119 if (!$this->_cache->connect($this->_host, $this->_port)) {
120 // dont use fatal here since we can go in an infinite loop
121 echo 'Could not connect to Memcached server';
122 CRM_Utils_System::civiExit();
123 }
124 }
125
126 /**
127 * @param $key
128 * @param $value
129 * @param null|int|\DateInterval $ttl
130 *
131 * @return bool
132 */
133 public function set($key, $value, $ttl = NULL) {
134 CRM_Utils_Cache::assertValidKey($key);
135 if (is_int($ttl) && $ttl <= 0) {
136 return $this->delete($key);
137 }
138 $expires = CRM_Utils_Date::convertCacheTtlToExpires($ttl, $this->_timeout);
139 return $this->_cache->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires);
140 }
141
142 /**
143 * @param $key
144 * @param mixed $default
145 *
146 * @return mixed
147 */
148 public function get($key, $default = NULL) {
149 CRM_Utils_Cache::assertValidKey($key);
150 $result = $this->_cache->get($this->getTruePrefix() . $key);
151 return ($result === FALSE) ? $default : unserialize($result);
152 }
153
154 /**
155 * @param string $key
156 *
157 * @return bool
158 * @throws \Psr\SimpleCache\CacheException
159 */
160 public function has($key) {
161 CRM_Utils_Cache::assertValidKey($key);
162 $result = $this->_cache->get($this->getTruePrefix() . $key);
163 return ($result !== FALSE);
164 }
165
166
167 /**
168 * @param $key
169 *
170 * @return bool
171 */
172 public function delete($key) {
173 CRM_Utils_Cache::assertValidKey($key);
174 $this->_cache->delete($this->getTruePrefix() . $key);
175 return TRUE;
176 }
177
178 /**
179 * @return bool
180 */
181 public function flush() {
182 $this->_truePrefix = NULL;
183 $this->_cache->delete($this->_prefix);
184 return TRUE;
185 }
186
187 public function clear() {
188 return $this->flush();
189 }
190
191 protected function getTruePrefix() {
192 if ($this->_truePrefix === NULL || $this->_truePrefix['expires'] < time()) {
193 $key = $this->_prefix;
194 $value = $this->_cache->get($key);
195 if ($value === FALSE) {
196 $value = uniqid();
197 $this->_cache->set($key, $value, FALSE, 0); // Indefinite.
198 }
199 $this->_truePrefix = [
200 'value' => $value,
201 'expires' => time() + self::NS_LOCAL_TTL,
202 ];
203 }
204 return $this->_prefix . $this->_truePrefix['value'] . '/';
205 }
206
207 }