(dev/core#174) CRM_Utils_Cache_Interface - Add getMultiple/setMultiple (PSR-16)
[civicrm-core.git] / CRM / Utils / Cache / Memcached.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33 class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface {
34
35 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
36
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;
42
43 /**
44 * The host name of the memcached server
45 *
46 * @var string
47 */
48 protected $_host = self::DEFAULT_HOST;
49
50 /**
51 * The port on which to connect on
52 *
53 * @var int
54 */
55 protected $_port = self::DEFAULT_PORT;
56
57 /**
58 * The default timeout to use
59 *
60 * @var int
61 */
62 protected $_timeout = self::DEFAULT_TIMEOUT;
63
64 /**
65 * The prefix prepended to cache keys.
66 *
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.
70 *
71 * @var string
72 */
73 protected $_prefix = self::DEFAULT_PREFIX;
74
75 /**
76 * The actual memcache object.
77 *
78 * @var Memcached
79 */
80 protected $_cache;
81
82 /**
83 * Constructor.
84 *
85 * @param array $config
86 * An array of configuration params.
87 *
88 * @return \CRM_Utils_Cache_Memcached
89 */
90 public function __construct($config) {
91 if (isset($config['host'])) {
92 $this->_host = $config['host'];
93 }
94 if (isset($config['port'])) {
95 $this->_port = $config['port'];
96 }
97 if (isset($config['timeout'])) {
98 $this->_timeout = $config['timeout'];
99 }
100 if (isset($config['prefix'])) {
101 $this->_prefix = $config['prefix'];
102 }
103
104 $this->_cache = new Memcached();
105
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();
110 }
111 }
112
113 /**
114 * @param $key
115 * @param $value
116 * @param null|int|\DateInterval $ttl
117 *
118 * @return bool
119 * @throws Exception
120 */
121 public function set($key, $value, $ttl = NULL) {
122 if ($ttl !== NULL) {
123 throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL");
124 }
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);
129 return FALSE;
130 }
131 return TRUE;
132 }
133
134 /**
135 * @param $key
136 * @param mixed $default
137 *
138 * @return mixed
139 */
140 public function get($key, $default = NULL) {
141 if ($default !== NULL) {
142 throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default");
143 }
144 $key = $this->cleanKey($key);
145 $result = $this->_cache->get($key);
146 return $result;
147 }
148
149 /**
150 * @param $key
151 *
152 * @return mixed
153 */
154 public function delete($key) {
155 $key = $this->cleanKey($key);
156 return $this->_cache->delete($key);
157 }
158
159 /**
160 * @param $key
161 *
162 * @return mixed|string
163 */
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;
170 }
171 return $key;
172 }
173
174 /**
175 * @return mixed
176 */
177 public function flush() {
178 // FIXME: Only delete items matching `$this->_prefix`.
179 return $this->_cache->flush();
180 }
181
182 }