3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Utils_Cache_Memcached
implements CRM_Utils_Cache_Interface
{
36 const DEFAULT_HOST
= 'localhost';
37 const DEFAULT_PORT
= 11211;
38 const DEFAULT_TIMEOUT
= 3600;
39 const DEFAULT_PREFIX
= '';
40 const MAX_KEY_LEN
= 62;
43 * The host name of the memcached server
47 protected $_host = self
::DEFAULT_HOST
;
50 * The port on which to connect on
54 protected $_port = self
::DEFAULT_PORT
;
57 * The default timeout to use
61 protected $_timeout = self
::DEFAULT_TIMEOUT
;
64 * The prefix prepended to cache keys.
66 * If we are using the same memcache instance for multiple CiviCRM
67 * installs, we must have a unique prefix for each install to prevent
68 * the keys from clobbering each other.
72 protected $_prefix = self
::DEFAULT_PREFIX
;
75 * The actual memcache object.
84 * @param array $config
85 * An array of configuration params.
87 * @return \CRM_Utils_Cache_Memcached
89 public function __construct($config) {
90 if (isset($config['host'])) {
91 $this->_host
= $config['host'];
93 if (isset($config['port'])) {
94 $this->_port
= $config['port'];
96 if (isset($config['timeout'])) {
97 $this->_timeout
= $config['timeout'];
99 if (isset($config['prefix'])) {
100 $this->_prefix
= $config['prefix'];
103 $this->_cache
= new Memcached();
105 if (!$this->_cache
->addServer($this->_host
, $this->_port
)) {
106 // dont use fatal here since we can go in an infinite loop
107 echo 'Could not connect to Memcached server';
108 CRM_Utils_System
::civiExit();
119 public function set($key, &$value) {
120 $key = $this->cleanKey($key);
121 if (!$this->_cache
->set($key, $value, $this->_timeout
)) {
122 CRM_Core_Error
::debug('Result Code: ', $this->_cache
->getResultMessage());
123 CRM_Core_Error
::fatal("memcached set failed, wondering why?, $key", $value);
134 public function &get($key) {
135 $key = $this->cleanKey($key);
136 $result = $this->_cache
->get($key);
145 public function delete($key) {
146 $key = $this->cleanKey($key);
147 return $this->_cache
->delete($key);
153 * @return mixed|string
155 public function cleanKey($key) {
156 $key = preg_replace('/\s+|\W+/', '_', $this->_prefix
. $key);
157 if (strlen($key) > self
::MAX_KEY_LEN
) {
158 $md5Key = md5($key); // this should be 32 characters in length
159 $subKeyLen = self
::MAX_KEY_LEN
- 1 - strlen($md5Key);
160 $key = substr($key, 0, $subKeyLen) . "_" . $md5Key;
168 public function flush() {
169 return $this->_cache
->flush();