3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
33 class CRM_Utils_Cache_Memcache
implements CRM_Utils_Cache_Interface
{
34 const DEFAULT_HOST
= 'localhost';
35 const DEFAULT_PORT
= 11211;
36 const DEFAULT_TIMEOUT
= 3600;
37 const DEFAULT_PREFIX
= '';
40 * The host name of the memcached server.
44 protected $_host = self
::DEFAULT_HOST
;
47 * The port on which to connect on.
51 protected $_port = self
::DEFAULT_PORT
;
54 * The default timeout to use.
58 protected $_timeout = self
::DEFAULT_TIMEOUT
;
61 * The prefix prepended to cache keys.
63 * If we are using the same memcache instance for multiple CiviCRM
64 * installs, we must have a unique prefix for each install to prevent
65 * the keys from clobbering each other.
69 protected $_prefix = self
::DEFAULT_PREFIX
;
72 * The actual memcache object.
81 * @param array $config
82 * An array of configuration params.
84 * @return \CRM_Utils_Cache_Memcache
86 public function __construct($config) {
87 if (isset($config['host'])) {
88 $this->_host
= $config['host'];
90 if (isset($config['port'])) {
91 $this->_port
= $config['port'];
93 if (isset($config['timeout'])) {
94 $this->_timeout
= $config['timeout'];
96 if (isset($config['prefix'])) {
97 $this->_prefix
= $config['prefix'];
100 $this->_cache
= new Memcache();
102 if (!$this->_cache
->connect($this->_host
, $this->_port
)) {
103 // dont use fatal here since we can go in an infinite loop
104 echo 'Could not connect to Memcached server';
105 CRM_Utils_System
::civiExit();
115 public function set($key, &$value) {
116 if (!$this->_cache
->set($this->_prefix
. $key, $value, FALSE, $this->_timeout
)) {
127 public function &get($key) {
128 $result = $this->_cache
->get($this->_prefix
. $key);
137 public function delete($key) {
138 return $this->_cache
->delete($this->_prefix
. $key);
144 public function flush() {
145 return $this->_cache
->flush();