3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
33 class CRM_Utils_Cache_Memcached
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
= '';
41 const MAX_KEY_LEN
= 62;
44 * The host name of the memcached server
48 protected $_host = self
::DEFAULT_HOST
;
51 * The port on which to connect on
55 protected $_port = self
::DEFAULT_PORT
;
58 * The default timeout to use
62 protected $_timeout = self
::DEFAULT_TIMEOUT
;
65 * The prefix prepended to cache keys.
67 * If we are using the same memcache instance for multiple CiviCRM
68 * installs, we must have a unique prefix for each install to prevent
69 * the keys from clobbering each other.
73 protected $_prefix = self
::DEFAULT_PREFIX
;
76 * The actual memcache object.
85 * @param array $config
86 * An array of configuration params.
88 * @return \CRM_Utils_Cache_Memcached
90 public function __construct($config) {
91 if (isset($config['host'])) {
92 $this->_host
= $config['host'];
94 if (isset($config['port'])) {
95 $this->_port
= $config['port'];
97 if (isset($config['timeout'])) {
98 $this->_timeout
= $config['timeout'];
100 if (isset($config['prefix'])) {
101 $this->_prefix
= $config['prefix'];
104 $this->_cache
= new Memcached();
106 if (!$this->_cache
->addServer($this->_host
, $this->_port
)) {
107 // dont use fatal here since we can go in an infinite loop
108 echo 'Could not connect to Memcached server';
109 CRM_Utils_System
::civiExit();
116 * @param null|int|\DateInterval $ttl
121 public function set($key, $value, $ttl = NULL) {
123 throw new \
RuntimeException("FIXME: " . __CLASS__
. "::set() should support non-NULL TTL");
125 $key = $this->cleanKey($key);
126 if (!$this->_cache
->set($key, $value, $this->_timeout
)) {
127 CRM_Core_Error
::debug('Result Code: ', $this->_cache
->getResultMessage());
128 CRM_Core_Error
::fatal("memcached set failed, wondering why?, $key", $value);
136 * @param mixed $default
140 public function get($key, $default = NULL) {
141 if ($default !== NULL) {
142 throw new \
RuntimeException("FIXME: " . __CLASS__
. "::get() only supports NULL default");
144 $key = $this->cleanKey($key);
145 $result = $this->_cache
->get($key);
154 public function delete($key) {
155 $key = $this->cleanKey($key);
156 return $this->_cache
->delete($key);
162 * @return mixed|string
164 public function cleanKey($key) {
165 $key = preg_replace('/\s+|\W+/', '_', $this->_prefix
. $key);
166 if (strlen($key) > self
::MAX_KEY_LEN
) {
167 $md5Key = md5($key); // this should be 32 characters in length
168 $subKeyLen = self
::MAX_KEY_LEN
- 1 - strlen($md5Key);
169 $key = substr($key, 0, $subKeyLen) . "_" . $md5Key;
177 public function flush() {
178 // FIXME: Only delete items matching `$this->_prefix`.
179 return $this->_cache
->flush();