3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Cache_Memcache
implements CRM_Utils_Cache_Interface
{
19 // TODO Consider native implementation.
20 use CRM_Utils_Cache_NaiveMultipleTrait
;
22 const DEFAULT_HOST
= 'localhost';
23 const DEFAULT_PORT
= 11211;
24 const DEFAULT_TIMEOUT
= 3600;
25 const DEFAULT_PREFIX
= '';
28 * If another process clears namespace, we'll find out in ~5 sec.
30 const NS_LOCAL_TTL
= 5;
33 * The host name of the memcached server.
37 protected $_host = self
::DEFAULT_HOST
;
40 * The port on which to connect on.
44 protected $_port = self
::DEFAULT_PORT
;
47 * The default timeout to use.
51 protected $_timeout = self
::DEFAULT_TIMEOUT
;
54 * The prefix prepended to cache keys.
56 * If we are using the same memcache instance for multiple CiviCRM
57 * installs, we must have a unique prefix for each install to prevent
58 * the keys from clobbering each other.
62 protected $_prefix = self
::DEFAULT_PREFIX
;
65 * The actual memcache object.
74 * This is the effective prefix. It may be bumped up whenever the dataset is flushed.
76 * @see https://github.com/memcached/memcached/wiki/ProgrammingTricks#deleting-by-namespace
78 protected $_truePrefix = NULL;
83 * @param array $config
84 * An array of configuration params.
86 * @return \CRM_Utils_Cache_Memcache
88 public function __construct($config) {
89 if (isset($config['host'])) {
90 $this->_host
= $config['host'];
92 if (isset($config['port'])) {
93 $this->_port
= $config['port'];
95 if (isset($config['timeout'])) {
96 $this->_timeout
= $config['timeout'];
98 if (isset($config['prefix'])) {
99 $this->_prefix
= $config['prefix'];
102 $this->_cache
= new Memcache();
104 if (!$this->_cache
->connect($this->_host
, $this->_port
)) {
105 // dont use fatal here since we can go in an infinite loop
106 echo 'Could not connect to Memcached server';
107 CRM_Utils_System
::civiExit();
114 * @param null|int|\DateInterval $ttl
118 public function set($key, $value, $ttl = NULL) {
119 CRM_Utils_Cache
::assertValidKey($key);
120 if (is_int($ttl) && $ttl <= 0) {
121 return $this->delete($key);
123 $expires = CRM_Utils_Date
::convertCacheTtlToExpires($ttl, $this->_timeout
);
124 return $this->_cache
->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires);
129 * @param mixed $default
133 public function get($key, $default = NULL) {
134 CRM_Utils_Cache
::assertValidKey($key);
135 $result = $this->_cache
->get($this->getTruePrefix() . $key);
136 return ($result === FALSE) ?
$default : unserialize($result);
143 * @throws \Psr\SimpleCache\CacheException
145 public function has($key) {
146 CRM_Utils_Cache
::assertValidKey($key);
147 $result = $this->_cache
->get($this->getTruePrefix() . $key);
148 return ($result !== FALSE);
156 public function delete($key) {
157 CRM_Utils_Cache
::assertValidKey($key);
158 $this->_cache
->delete($this->getTruePrefix() . $key);
165 public function flush() {
166 $this->_truePrefix
= NULL;
167 $this->_cache
->delete($this->_prefix
);
171 public function clear() {
172 return $this->flush();
175 protected function getTruePrefix() {
176 if ($this->_truePrefix
=== NULL ||
$this->_truePrefix
['expires'] < time()) {
177 $key = $this->_prefix
;
178 $value = $this->_cache
->get($key);
179 if ($value === FALSE) {
182 $this->_cache
->set($key, $value, FALSE, 0);
184 $this->_truePrefix
= [
186 'expires' => time() + self
::NS_LOCAL_TTL
,
189 return $this->_prefix
. $this->_truePrefix
['value'] . '/';