Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 | 32 | */ |
9f49773e | 33 | class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface { |
0d64c8fa TO |
34 | |
35 | use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. | |
9f70b0e4 | 36 | use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation |
0d64c8fa | 37 | |
353ffa53 TO |
38 | const DEFAULT_HOST = 'localhost'; |
39 | const DEFAULT_PORT = 11211; | |
6a488035 | 40 | const DEFAULT_TIMEOUT = 3600; |
353ffa53 TO |
41 | const DEFAULT_PREFIX = ''; |
42 | const MAX_KEY_LEN = 62; | |
6a488035 TO |
43 | |
44 | /** | |
45 | * The host name of the memcached server | |
46 | * | |
47 | * @var string | |
48 | */ | |
49 | protected $_host = self::DEFAULT_HOST; | |
50 | ||
51 | /** | |
52 | * The port on which to connect on | |
53 | * | |
54 | * @var int | |
55 | */ | |
56 | protected $_port = self::DEFAULT_PORT; | |
57 | ||
58 | /** | |
59 | * The default timeout to use | |
60 | * | |
61 | * @var int | |
62 | */ | |
63 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
64 | ||
65 | /** | |
66 | * The prefix prepended to cache keys. | |
67 | * | |
68 | * If we are using the same memcache instance for multiple CiviCRM | |
69 | * installs, we must have a unique prefix for each install to prevent | |
70 | * the keys from clobbering each other. | |
71 | * | |
72 | * @var string | |
73 | */ | |
74 | protected $_prefix = self::DEFAULT_PREFIX; | |
75 | ||
76 | /** | |
fe482240 | 77 | * The actual memcache object. |
6a488035 | 78 | * |
41e0c250 | 79 | * @var Memcached |
6a488035 TO |
80 | */ |
81 | protected $_cache; | |
82 | ||
83 | /** | |
fe482240 | 84 | * Constructor. |
6a488035 | 85 | * |
77855840 TO |
86 | * @param array $config |
87 | * An array of configuration params. | |
dd244018 EM |
88 | * |
89 | * @return \CRM_Utils_Cache_Memcached | |
6a488035 | 90 | */ |
00be9182 | 91 | public function __construct($config) { |
6a488035 TO |
92 | if (isset($config['host'])) { |
93 | $this->_host = $config['host']; | |
94 | } | |
95 | if (isset($config['port'])) { | |
96 | $this->_port = $config['port']; | |
97 | } | |
98 | if (isset($config['timeout'])) { | |
99 | $this->_timeout = $config['timeout']; | |
100 | } | |
101 | if (isset($config['prefix'])) { | |
102 | $this->_prefix = $config['prefix']; | |
103 | } | |
104 | ||
105 | $this->_cache = new Memcached(); | |
106 | ||
107 | if (!$this->_cache->addServer($this->_host, $this->_port)) { | |
108 | // dont use fatal here since we can go in an infinite loop | |
109 | echo 'Could not connect to Memcached server'; | |
110 | CRM_Utils_System::civiExit(); | |
111 | } | |
112 | } | |
113 | ||
5bc392e6 EM |
114 | /** |
115 | * @param $key | |
116 | * @param $value | |
858451a9 | 117 | * @param null|int|\DateInterval $ttl |
5bc392e6 EM |
118 | * |
119 | * @return bool | |
120 | * @throws Exception | |
121 | */ | |
858451a9 TO |
122 | public function set($key, $value, $ttl = NULL) { |
123 | if ($ttl !== NULL) { | |
124 | throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL"); | |
125 | } | |
6a488035 TO |
126 | $key = $this->cleanKey($key); |
127 | if (!$this->_cache->set($key, $value, $this->_timeout)) { | |
481a74f4 TO |
128 | CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage()); |
129 | CRM_Core_Error::fatal("memcached set failed, wondering why?, $key", $value); | |
6a488035 TO |
130 | return FALSE; |
131 | } | |
132 | return TRUE; | |
133 | } | |
134 | ||
5bc392e6 EM |
135 | /** |
136 | * @param $key | |
2da67cc5 | 137 | * @param mixed $default |
5bc392e6 EM |
138 | * |
139 | * @return mixed | |
140 | */ | |
2da67cc5 TO |
141 | public function get($key, $default = NULL) { |
142 | if ($default !== NULL) { | |
143 | throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); | |
144 | } | |
6a488035 TO |
145 | $key = $this->cleanKey($key); |
146 | $result = $this->_cache->get($key); | |
147 | return $result; | |
148 | } | |
149 | ||
5bc392e6 EM |
150 | /** |
151 | * @param $key | |
152 | * | |
153 | * @return mixed | |
154 | */ | |
00be9182 | 155 | public function delete($key) { |
6a488035 TO |
156 | $key = $this->cleanKey($key); |
157 | return $this->_cache->delete($key); | |
158 | } | |
159 | ||
5bc392e6 EM |
160 | /** |
161 | * @param $key | |
162 | * | |
163 | * @return mixed|string | |
164 | */ | |
00be9182 | 165 | public function cleanKey($key) { |
6a488035 | 166 | $key = preg_replace('/\s+|\W+/', '_', $this->_prefix . $key); |
481a74f4 | 167 | if (strlen($key) > self::MAX_KEY_LEN) { |
6a488035 TO |
168 | $md5Key = md5($key); // this should be 32 characters in length |
169 | $subKeyLen = self::MAX_KEY_LEN - 1 - strlen($md5Key); | |
170 | $key = substr($key, 0, $subKeyLen) . "_" . $md5Key; | |
171 | } | |
172 | return $key; | |
173 | } | |
174 | ||
e0ef6999 | 175 | /** |
124e5288 | 176 | * @return bool |
e0ef6999 | 177 | */ |
00be9182 | 178 | public function flush() { |
fbebc7a0 | 179 | // FIXME: Only delete items matching `$this->_prefix`. |
6a488035 TO |
180 | return $this->_cache->flush(); |
181 | } | |
96025800 | 182 | |
c31de879 TO |
183 | public function clear() { |
184 | return $this->flush(); | |
185 | } | |
186 | ||
6a488035 | 187 | } |