(NFC) CRM-17789 - Replace `@return $this` with types
[civicrm-core.git] / CRM / Utils / Cache / Memcached.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035 32 */
9f49773e 33class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface {
353ffa53
TO
34 const DEFAULT_HOST = 'localhost';
35 const DEFAULT_PORT = 11211;
6a488035 36 const DEFAULT_TIMEOUT = 3600;
353ffa53
TO
37 const DEFAULT_PREFIX = '';
38 const MAX_KEY_LEN = 62;
6a488035
TO
39
40 /**
41 * The host name of the memcached server
42 *
43 * @var string
44 */
45 protected $_host = self::DEFAULT_HOST;
46
47 /**
48 * The port on which to connect on
49 *
50 * @var int
51 */
52 protected $_port = self::DEFAULT_PORT;
53
54 /**
55 * The default timeout to use
56 *
57 * @var int
58 */
59 protected $_timeout = self::DEFAULT_TIMEOUT;
60
61 /**
62 * The prefix prepended to cache keys.
63 *
64 * If we are using the same memcache instance for multiple CiviCRM
65 * installs, we must have a unique prefix for each install to prevent
66 * the keys from clobbering each other.
67 *
68 * @var string
69 */
70 protected $_prefix = self::DEFAULT_PREFIX;
71
72 /**
fe482240 73 * The actual memcache object.
6a488035
TO
74 *
75 * @var resource
76 */
77 protected $_cache;
78
79 /**
fe482240 80 * Constructor.
6a488035 81 *
77855840
TO
82 * @param array $config
83 * An array of configuration params.
dd244018
EM
84 *
85 * @return \CRM_Utils_Cache_Memcached
6a488035 86 */
00be9182 87 public function __construct($config) {
6a488035
TO
88 if (isset($config['host'])) {
89 $this->_host = $config['host'];
90 }
91 if (isset($config['port'])) {
92 $this->_port = $config['port'];
93 }
94 if (isset($config['timeout'])) {
95 $this->_timeout = $config['timeout'];
96 }
97 if (isset($config['prefix'])) {
98 $this->_prefix = $config['prefix'];
99 }
100
101 $this->_cache = new Memcached();
102
103 if (!$this->_cache->addServer($this->_host, $this->_port)) {
104 // dont use fatal here since we can go in an infinite loop
105 echo 'Could not connect to Memcached server';
106 CRM_Utils_System::civiExit();
107 }
108 }
109
5bc392e6
EM
110 /**
111 * @param $key
112 * @param $value
113 *
114 * @return bool
115 * @throws Exception
116 */
00be9182 117 public function set($key, &$value) {
6a488035
TO
118 $key = $this->cleanKey($key);
119 if (!$this->_cache->set($key, $value, $this->_timeout)) {
481a74f4
TO
120 CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage());
121 CRM_Core_Error::fatal("memcached set failed, wondering why?, $key", $value);
6a488035
TO
122 return FALSE;
123 }
124 return TRUE;
125 }
126
5bc392e6
EM
127 /**
128 * @param $key
129 *
130 * @return mixed
131 */
00be9182 132 public function &get($key) {
6a488035
TO
133 $key = $this->cleanKey($key);
134 $result = $this->_cache->get($key);
135 return $result;
136 }
137
5bc392e6
EM
138 /**
139 * @param $key
140 *
141 * @return mixed
142 */
00be9182 143 public function delete($key) {
6a488035
TO
144 $key = $this->cleanKey($key);
145 return $this->_cache->delete($key);
146 }
147
5bc392e6
EM
148 /**
149 * @param $key
150 *
151 * @return mixed|string
152 */
00be9182 153 public function cleanKey($key) {
6a488035 154 $key = preg_replace('/\s+|\W+/', '_', $this->_prefix . $key);
481a74f4 155 if (strlen($key) > self::MAX_KEY_LEN) {
6a488035
TO
156 $md5Key = md5($key); // this should be 32 characters in length
157 $subKeyLen = self::MAX_KEY_LEN - 1 - strlen($md5Key);
158 $key = substr($key, 0, $subKeyLen) . "_" . $md5Key;
159 }
160 return $key;
161 }
162
e0ef6999
EM
163 /**
164 * @return mixed
165 */
00be9182 166 public function flush() {
6a488035
TO
167 return $this->_cache->flush();
168 }
96025800 169
6a488035 170}