[NFC] various code cleanup on CRM_Contact_BAO_Query
[civicrm-core.git] / CRM / Utils / Cache / Memcache.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035 32 */
9f49773e 33class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
0d64c8fa 34
6714d8d2
SL
35 // TODO Consider native implementation.
36 use CRM_Utils_Cache_NaiveMultipleTrait;
0d64c8fa 37
353ffa53
TO
38 const DEFAULT_HOST = 'localhost';
39 const DEFAULT_PORT = 11211;
6a488035 40 const DEFAULT_TIMEOUT = 3600;
353ffa53 41 const DEFAULT_PREFIX = '';
6a488035 42
fbbfd6dd
TO
43 /**
44 * If another process clears namespace, we'll find out in ~5 sec.
45 */
46 const NS_LOCAL_TTL = 5;
47
6a488035 48 /**
fe482240 49 * The host name of the memcached server.
6a488035
TO
50 *
51 * @var string
52 */
53 protected $_host = self::DEFAULT_HOST;
54
55 /**
fe482240 56 * The port on which to connect on.
6a488035
TO
57 *
58 * @var int
59 */
60 protected $_port = self::DEFAULT_PORT;
61
62 /**
fe482240 63 * The default timeout to use.
6a488035
TO
64 *
65 * @var int
66 */
67 protected $_timeout = self::DEFAULT_TIMEOUT;
68
69 /**
70 * The prefix prepended to cache keys.
71 *
72 * If we are using the same memcache instance for multiple CiviCRM
73 * installs, we must have a unique prefix for each install to prevent
74 * the keys from clobbering each other.
75 *
76 * @var string
77 */
78 protected $_prefix = self::DEFAULT_PREFIX;
79
80 /**
fe482240 81 * The actual memcache object.
6a488035 82 *
41e0c250 83 * @var Memcache
6a488035
TO
84 */
85 protected $_cache;
86
fbbfd6dd
TO
87 /**
88 * @var NULL|array
89 *
90 * This is the effective prefix. It may be bumped up whenever the dataset is flushed.
91 *
92 * @see https://github.com/memcached/memcached/wiki/ProgrammingTricks#deleting-by-namespace
93 */
94 protected $_truePrefix = NULL;
95
6a488035 96 /**
fe482240 97 * Constructor.
6a488035 98 *
77855840
TO
99 * @param array $config
100 * An array of configuration params.
6a488035 101 *
77b97be7 102 * @return \CRM_Utils_Cache_Memcache
6a488035 103 */
00be9182 104 public function __construct($config) {
6a488035
TO
105 if (isset($config['host'])) {
106 $this->_host = $config['host'];
107 }
108 if (isset($config['port'])) {
109 $this->_port = $config['port'];
110 }
111 if (isset($config['timeout'])) {
112 $this->_timeout = $config['timeout'];
113 }
114 if (isset($config['prefix'])) {
115 $this->_prefix = $config['prefix'];
116 }
117
118 $this->_cache = new Memcache();
119
120 if (!$this->_cache->connect($this->_host, $this->_port)) {
121 // dont use fatal here since we can go in an infinite loop
122 echo 'Could not connect to Memcached server';
123 CRM_Utils_System::civiExit();
124 }
125 }
126
5bc392e6
EM
127 /**
128 * @param $key
129 * @param $value
858451a9 130 * @param null|int|\DateInterval $ttl
5bc392e6
EM
131 *
132 * @return bool
133 */
858451a9 134 public function set($key, $value, $ttl = NULL) {
fbbfd6dd
TO
135 CRM_Utils_Cache::assertValidKey($key);
136 if (is_int($ttl) && $ttl <= 0) {
137 return $this->delete($key);
6a488035 138 }
fbbfd6dd
TO
139 $expires = CRM_Utils_Date::convertCacheTtlToExpires($ttl, $this->_timeout);
140 return $this->_cache->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires);
6a488035
TO
141 }
142
5bc392e6
EM
143 /**
144 * @param $key
2da67cc5 145 * @param mixed $default
5bc392e6
EM
146 *
147 * @return mixed
148 */
2da67cc5 149 public function get($key, $default = NULL) {
fbbfd6dd
TO
150 CRM_Utils_Cache::assertValidKey($key);
151 $result = $this->_cache->get($this->getTruePrefix() . $key);
152 return ($result === FALSE) ? $default : unserialize($result);
6a488035
TO
153 }
154
fbbfd6dd
TO
155 /**
156 * @param string $key
157 *
158 * @return bool
159 * @throws \Psr\SimpleCache\CacheException
160 */
161 public function has($key) {
162 CRM_Utils_Cache::assertValidKey($key);
163 $result = $this->_cache->get($this->getTruePrefix() . $key);
164 return ($result !== FALSE);
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();
6714d8d2
SL
197 // Indefinite.
198 $this->_cache->set($key, $value, FALSE, 0);
fbbfd6dd
TO
199 }
200 $this->_truePrefix = [
201 'value' => $value,
202 'expires' => time() + self::NS_LOCAL_TTL,
203 ];
204 }
205 return $this->_prefix . $this->_truePrefix['value'] . '/';
206 }
207
6a488035 208}