3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Utils_Cache_Memcache
implements CRM_Utils_Cache_Interface
{
35 use CRM_Utils_Cache_NaiveMultipleTrait
; // TODO Consider native implementation.
37 const DEFAULT_HOST
= 'localhost';
38 const DEFAULT_PORT
= 11211;
39 const DEFAULT_TIMEOUT
= 3600;
40 const DEFAULT_PREFIX
= '';
43 * If another process clears namespace, we'll find out in ~5 sec.
45 const NS_LOCAL_TTL
= 5;
48 * The host name of the memcached server.
52 protected $_host = self
::DEFAULT_HOST
;
55 * The port on which to connect on.
59 protected $_port = self
::DEFAULT_PORT
;
62 * The default timeout to use.
66 protected $_timeout = self
::DEFAULT_TIMEOUT
;
69 * The prefix prepended to cache keys.
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.
77 protected $_prefix = self
::DEFAULT_PREFIX
;
80 * The actual memcache object.
89 * This is the effective prefix. It may be bumped up whenever the dataset is flushed.
91 * @see https://github.com/memcached/memcached/wiki/ProgrammingTricks#deleting-by-namespace
93 protected $_truePrefix = NULL;
98 * @param array $config
99 * An array of configuration params.
101 * @return \CRM_Utils_Cache_Memcache
103 public function __construct($config) {
104 if (isset($config['host'])) {
105 $this->_host
= $config['host'];
107 if (isset($config['port'])) {
108 $this->_port
= $config['port'];
110 if (isset($config['timeout'])) {
111 $this->_timeout
= $config['timeout'];
113 if (isset($config['prefix'])) {
114 $this->_prefix
= $config['prefix'];
117 $this->_cache
= new Memcache();
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();
129 * @param null|int|\DateInterval $ttl
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);
138 $expires = CRM_Utils_Date
::convertCacheTtlToExpires($ttl, $this->_timeout
);
139 return $this->_cache
->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires);
144 * @param mixed $default
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);
158 * @throws \Psr\SimpleCache\CacheException
160 public function has($key) {
161 CRM_Utils_Cache
::assertValidKey($key);
162 $result = $this->_cache
->get($this->getTruePrefix() . $key);
163 return ($result !== FALSE);
172 public function delete($key) {
173 CRM_Utils_Cache
::assertValidKey($key);
174 $this->_cache
->delete($this->getTruePrefix() . $key);
181 public function flush() {
182 $this->_truePrefix
= NULL;
183 $this->_cache
->delete($this->_prefix
);
187 public function clear() {
188 return $this->flush();
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) {
197 $this->_cache
->set($key, $value, FALSE, 0); // Indefinite.
199 $this->_truePrefix
= [
201 'expires' => time() + self
::NS_LOCAL_TTL
,
204 return $this->_prefix
. $this->_truePrefix
['value'] . '/';