Merge pull request #11923 from lcdservices/GL-44
[civicrm-core.git] / CRM / Utils / Cache / Memcache.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 32 */
9f49773e 33class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
0d64c8fa
TO
34
35 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
36
353ffa53
TO
37 const DEFAULT_HOST = 'localhost';
38 const DEFAULT_PORT = 11211;
6a488035 39 const DEFAULT_TIMEOUT = 3600;
353ffa53 40 const DEFAULT_PREFIX = '';
6a488035 41
fbbfd6dd
TO
42 /**
43 * If another process clears namespace, we'll find out in ~5 sec.
44 */
45 const NS_LOCAL_TTL = 5;
46
6a488035 47 /**
fe482240 48 * The host name of the memcached server.
6a488035
TO
49 *
50 * @var string
51 */
52 protected $_host = self::DEFAULT_HOST;
53
54 /**
fe482240 55 * The port on which to connect on.
6a488035
TO
56 *
57 * @var int
58 */
59 protected $_port = self::DEFAULT_PORT;
60
61 /**
fe482240 62 * The default timeout to use.
6a488035
TO
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 /**
fe482240 80 * The actual memcache object.
6a488035 81 *
41e0c250 82 * @var Memcache
6a488035
TO
83 */
84 protected $_cache;
85
fbbfd6dd
TO
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
6a488035 95 /**
fe482240 96 * Constructor.
6a488035 97 *
77855840
TO
98 * @param array $config
99 * An array of configuration params.
6a488035 100 *
77b97be7 101 * @return \CRM_Utils_Cache_Memcache
6a488035 102 */
00be9182 103 public function __construct($config) {
6a488035
TO
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
5bc392e6
EM
126 /**
127 * @param $key
128 * @param $value
858451a9 129 * @param null|int|\DateInterval $ttl
5bc392e6
EM
130 *
131 * @return bool
132 */
858451a9 133 public function set($key, $value, $ttl = NULL) {
fbbfd6dd
TO
134 CRM_Utils_Cache::assertValidKey($key);
135 if (is_int($ttl) && $ttl <= 0) {
136 return $this->delete($key);
6a488035 137 }
fbbfd6dd
TO
138 $expires = CRM_Utils_Date::convertCacheTtlToExpires($ttl, $this->_timeout);
139 return $this->_cache->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires);
6a488035
TO
140 }
141
5bc392e6
EM
142 /**
143 * @param $key
2da67cc5 144 * @param mixed $default
5bc392e6
EM
145 *
146 * @return mixed
147 */
2da67cc5 148 public function get($key, $default = NULL) {
fbbfd6dd
TO
149 CRM_Utils_Cache::assertValidKey($key);
150 $result = $this->_cache->get($this->getTruePrefix() . $key);
151 return ($result === FALSE) ? $default : unserialize($result);
6a488035
TO
152 }
153
fbbfd6dd
TO
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
5bc392e6
EM
167 /**
168 * @param $key
169 *
eec321a4 170 * @return bool
5bc392e6 171 */
00be9182 172 public function delete($key) {
fbbfd6dd
TO
173 CRM_Utils_Cache::assertValidKey($key);
174 $this->_cache->delete($this->getTruePrefix() . $key);
175 return TRUE;
6a488035
TO
176 }
177
5bc392e6 178 /**
124e5288 179 * @return bool
5bc392e6 180 */
00be9182 181 public function flush() {
fbbfd6dd
TO
182 $this->_truePrefix = NULL;
183 $this->_cache->delete($this->_prefix);
184 return TRUE;
6a488035 185 }
96025800 186
c31de879
TO
187 public function clear() {
188 return $this->flush();
189 }
190
fbbfd6dd
TO
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
6a488035 207}