MDL-35558 mod_data: Show only own entries while there are required pending.
[moodle.git] / cache / stores / memcache / lib.php
CommitLineData
e3b77f9f
SH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * The library file for the memcache cache store.
19 *
20 * This file is part of the memcache cache store, it contains the API for interacting with an instance of the store.
21 *
6fec1820 22 * @package cachestore_memcache
e3b77f9f
SH
23 * @copyright 2012 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * The memcache store class.
31 *
32 * (Not to be confused with memcached store)
33 *
34 * Configuration options:
35 * servers: string: host:port:weight , ...
36 *
37 * @copyright 2012 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
6fec1820 40class cachestore_memcache implements cache_store {
e3b77f9f
SH
41
42 /**
43 * The name of the store
44 * @var store
45 */
46 protected $name;
47
48 /**
49 * The memcache connection once established.
50 * @var Memcache
51 */
52 protected $connection;
53
54 /**
55 * An array of servers to use in the connection args.
56 * @var array
57 */
58 protected $servers = array();
59
60 /**
61 * An array of options used when establishing the connection.
62 * @var array
63 */
64 protected $options = array();
65
66 /**
67 * Set to true when things are ready to be initialised.
68 * @var bool
69 */
70 protected $isready = false;
71
72 /**
73 * The cache definition this store was initialised for.
74 * @var cache_definition
75 */
76 protected $definition;
77
78 /**
79 * Constructs the store instance.
80 *
81 * Noting that this function is not an initialisation. It is used to prepare the store for use.
82 * The store will be initialised when required and will be provided with a cache_definition at that time.
83 *
84 * @param string $name
85 * @param array $configuration
86 */
87 public function __construct($name, array $configuration = array()) {
88 $this->name = $name;
89 if (!array_key_exists('servers', $configuration) || empty($configuration['servers'])) {
90 // Nothing configured.
91 return;
92 }
42f5d164
SH
93 if (!is_array($configuration['servers'])) {
94 $configuration['servers'] = array($configuration['servers']);
95 }
e3b77f9f
SH
96 foreach ($configuration['servers'] as $server) {
97 if (!is_array($server)) {
98 $server = explode(':', $server, 3);
99 }
100 if (!array_key_exists(1, $server)) {
101 $server[1] = 11211;
102 $server[2] = 100;
103 } else if (!array_key_exists(2, $server)) {
104 $server[2] = 100;
105 }
106 $this->servers[] = $server;
107 }
108
109 $this->isready = true;
110 }
111
112 /**
113 * Initialises the cache.
114 *
115 * Once this has been done the cache is all set to be used.
116 *
117 * @param cache_definition $definition
118 */
119 public function initialise(cache_definition $definition) {
120 if ($this->is_initialised()) {
121 throw new coding_exception('This memcache instance has already been initialised.');
122 }
123 $this->definition = $definition;
124 $this->connection = new Memcache;
125 foreach ($this->servers as $server) {
126 $this->connection->addServer($server[0], $server[1], true, $server[2]);
127 }
128 }
129
130 /**
131 * Returns true once this instance has been initialised.
132 *
133 * @return bool
134 */
135 public function is_initialised() {
136 return ($this->connection !== null);
137 }
138
139 /**
140 * Returns true if this store instance is ready to be used.
141 * @return bool
142 */
143 public function is_ready() {
144 return $this->isready;
145 }
146
147 /**
148 * Returns true if the store requirements are met.
149 *
150 * @return bool
151 */
152 public static function are_requirements_met() {
153 return class_exists('Memcache');
154 }
155
156 /**
157 * Returns true if the given mode is supported by this store.
158 *
159 * @param int $mode One of cache_store::MODE_*
160 * @return bool
161 */
162 public static function is_supported_mode($mode) {
163 return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION);
164 }
165
166 /**
167 * Returns the supported features as a combined int.
168 *
169 * @param array $configuration
170 * @return int
171 */
172 public static function get_supported_features(array $configuration = array()) {
173 return self::SUPPORTS_NATIVE_TTL;
174 }
175
176 /**
177 * Returns true if the store instance supports multiple identifiers.
178 *
179 * @return bool
180 */
181 public function supports_multiple_indentifiers() {
182 return false;
183 }
184
185 /**
186 * Returns true if the store instance guarantees data.
187 *
188 * @return bool
189 */
190 public function supports_data_guarantee() {
191 return false;
192 }
193
194 /**
195 * Returns true if the store instance supports native ttl.
196 *
197 * @return bool
198 */
199 public function supports_native_ttl() {
200 return true;
201 }
202
203 /**
204 * Returns the supported modes as a combined int.
205 *
206 * @param array $configuration
207 * @return int
208 */
209 public static function get_supported_modes(array $configuration = array()) {
210 return self::MODE_APPLICATION + self::MODE_SESSION;
211 }
212
213 /**
214 * Retrieves an item from the cache store given its key.
215 *
216 * @param string $key The key to retrieve
217 * @return mixed The data that was associated with the key, or false if the key did not exist.
218 */
219 public function get($key) {
220 return $this->connection->get($key);
221 }
222
223 /**
224 * Retrieves several items from the cache store in a single transaction.
225 *
226 * If not all of the items are available in the cache then the data value for those that are missing will be set to false.
227 *
228 * @param array $keys The array of keys to retrieve
229 * @return array An array of items from the cache. There will be an item for each key, those that were not in the store will
230 * be set to false.
231 */
232 public function get_many($keys) {
233 $result = $this->connection->get($keys);
234 if (!is_array($result)) {
235 $result = array();
236 }
237 foreach ($keys as $key) {
238 if (!array_key_exists($key, $result)) {
239 $result[$key] = false;
240 }
241 }
242 return $result;
243 }
244
245 /**
246 * Sets an item in the cache given its key and data value.
247 *
248 * @param string $key The key to use.
249 * @param mixed $data The data to set.
250 * @return bool True if the operation was a success false otherwise.
251 */
252 public function set($key, $data) {
253 return $this->connection->set($key, $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
254 }
255
256 /**
257 * Sets many items in the cache in a single transaction.
258 *
259 * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
260 * keys, 'key' and 'value'.
261 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
262 * sent ... if they care that is.
263 */
264 public function set_many(array $keyvaluearray) {
265 $count = 0;
266 foreach ($keyvaluearray as $pair) {
267 if ($this->connection->set($pair['key'], $pair['value'], MEMCACHE_COMPRESSED, $this->definition->get_ttl())) {
268 $count++;
269 }
270 }
271 return $count;
272 }
273
274 /**
275 * Deletes an item from the cache store.
276 *
277 * @param string $key The key to delete.
278 * @return bool Returns true if the operation was a success, false otherwise.
279 */
280 public function delete($key) {
281 return $this->connection->delete($key);
282 }
283
284 /**
285 * Deletes several keys from the cache in a single action.
286 *
287 * @param array $keys The keys to delete
288 * @return int The number of items successfully deleted.
289 */
290 public function delete_many(array $keys) {
291 $count = 0;
292 foreach ($keys as $key) {
293 if ($this->delete($key)) {
294 $count++;
295 }
296 }
297 return $count;
298 }
299
300 /**
301 * Purges the cache deleting all items within it.
302 *
303 * @return boolean True on success. False otherwise.
304 */
305 public function purge() {
306 $this->connection->flush();
307 return true;
308 }
309
310 /**
311 * Given the data from the add instance form this function creates a configuration array.
312 *
313 * @param stdClass $data
314 * @return array
315 */
316 public static function config_get_configuration_array($data) {
317 $lines = explode("\n", $data->servers);
318 $servers = array();
319 foreach ($lines as $line) {
320 $line = trim($line, ':');
321 $servers[] = explode(':', $line, 3);
322 }
323 return array(
324 'servers' => $servers,
325 );
326 }
327
328 /**
329 * Returns true if the user can add an instance of the store plugin.
330 *
331 * @return bool
332 */
333 public static function can_add_instance() {
334 return true;
335 }
336
337 /**
338 * Performs any necessary clean up when the store instance is being deleted.
339 */
340 public function cleanup() {
341 $this->purge();
342 }
343
344 /**
345 * Generates an instance of the cache store that can be used for testing.
346 *
347 * @param cache_definition $definition
348 * @return false
349 */
350 public static function initialise_test_instance(cache_definition $definition) {
351 if (!self::are_requirements_met()) {
352 return false;
353 }
354
6fec1820 355 $config = get_config('cachestore_memcache');
e3b77f9f
SH
356 if (empty($config->testservers)) {
357 return false;
358 }
359
360 $configuration = array();
361 $configuration['servers'] = explode("\n", $config->testservers);
362
6fec1820 363 $store = new cachestore_memcache('Test memcache', $configuration);
e3b77f9f
SH
364 $store->initialise($definition);
365
366 return $store;
367 }
34c84c72
SH
368
369 /**
370 * Returns the name of this instance.
371 * @return string
372 */
373 public function my_name() {
374 return $this->name;
375 }
e3b77f9f 376}