2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * The library file for the session cache store.
20 * This file is part of the session cache store, it contains the API for interacting with an instance of the store.
21 * This is used as a default cache store within the Cache API. It should never be deleted.
23 * @package cachestore_session
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
32 * The Session store class.
34 * @copyright 2012 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class cachestore_session extends session_data_store implements cache_store, cache_is_key_aware {
40 * The name of the store
46 * The store id (should be unique)
52 * The store we use for data.
58 * The ttl if there is one. Hopefully not.
64 * Constructs the store instance.
66 * Noting that this function is not an initialisation. It is used to prepare the store for use.
67 * The store will be initialised when required and will be provided with a cache_definition at that time.
70 * @param array $configuration
72 public function __construct($name, array $configuration = array()) {
77 * Returns the supported features as a combined int.
79 * @param array $configuration
82 public static function get_supported_features(array $configuration = array()) {
83 return self::SUPPORTS_DATA_GUARANTEE +
84 self::SUPPORTS_NATIVE_TTL;
88 * Returns the supported modes as a combined int.
90 * @param array $configuration
93 public static function get_supported_modes(array $configuration = array()) {
94 return self::MODE_SESSION;
98 * Returns true if the store requirements are met.
102 public static function are_requirements_met() {
107 * Returns true if the given mode is supported by this store.
109 * @param int $mode One of cache_store::MODE_*
112 public static function is_supported_mode($mode) {
113 return ($mode === self::MODE_SESSION);
117 * Returns true if the store instance guarantees data.
121 public function supports_data_guarantee() {
126 * Returns true if the store instance supports multiple identifiers.
130 public function supports_multiple_identifiers() {
135 * Returns true if the store instance supports native ttl.
139 public function supports_native_ttl() {
144 * Initialises the cache.
146 * Once this has been done the cache is all set to be used.
148 * @param cache_definition $definition
150 public function initialise(cache_definition $definition) {
151 $this->storeid = $definition->generate_definition_hash();
152 $this->store = &self::register_store_id($definition->get_id());
153 $this->ttl = $definition->get_ttl();
157 * Returns true once this instance has been initialised.
161 public function is_initialised() {
162 return (is_array($this->store));
166 * Returns true if this store instance is ready to be used.
169 public function is_ready() {
174 * Retrieves an item from the cache store given its key.
176 * @param string $key The key to retrieve
177 * @return mixed The data that was associated with the key, or false if the key did not exist.
179 public function get($key) {
180 $maxtime = cache::now() - $this->ttl;
181 if (array_key_exists($key, $this->store)) {
182 if ($this->ttl == 0) {
183 return $this->store[$key];
184 } else if ($this->store[$key][1] >= $maxtime) {
185 return $this->store[$key][0];
192 * Retrieves several items from the cache store in a single transaction.
194 * 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.
196 * @param array $keys The array of keys to retrieve
197 * @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
200 public function get_many($keys) {
202 $maxtime = cache::now() - $this->ttl;
203 foreach ($keys as $key) {
204 $return[$key] = false;
205 if (array_key_exists($key, $this->store)) {
206 if ($this->ttl == 0) {
207 $return[$key] = $this->store[$key];
208 } else if ($this->store[$key][1] >= $maxtime) {
209 $return[$key] = $this->store[$key][0];
217 * Sets an item in the cache given its key and data value.
219 * @param string $key The key to use.
220 * @param mixed $data The data to set.
221 * @return bool True if the operation was a success false otherwise.
223 public function set($key, $data) {
224 if ($this->ttl == 0) {
225 $this->store[$key] = $data;
227 $this->store[$key] = array($data, cache::now());
233 * Sets many items in the cache in a single transaction.
235 * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
236 * keys, 'key' and 'value'.
237 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
238 * sent ... if they care that is.
240 public function set_many(array $keyvaluearray) {
242 foreach ($keyvaluearray as $pair) {
243 $this->set($pair['key'], $pair['value']);
250 * Checks if the store has a record for the given key and returns true if so.
255 public function has($key) {
256 $maxtime = cache::now() - $this->ttl;
257 if (array_key_exists($key, $this->store)) {
258 if ($this->ttl == 0) {
260 } else if ($this->store[$key][1] >= $maxtime) {
268 * Returns true if the store contains records for all of the given keys.
273 public function has_all(array $keys) {
274 $maxtime = cache::now() - $this->ttl;
275 foreach ($keys as $key) {
276 if (!array_key_exists($key, $this->store)) {
279 if ($this->ttl != 0 && $this->store[$key][1] < $maxtime) {
287 * Returns true if the store contains records for any of the given keys.
292 public function has_any(array $keys) {
293 $maxtime = cache::now() - $this->ttl;
294 foreach ($keys as $key) {
295 if (array_key_exists($key, $this->store) && ($this->ttl == 0 || $this->store[$key][1] >= $maxtime)) {
303 * Deletes an item from the cache store.
305 * @param string $key The key to delete.
306 * @return bool Returns true if the operation was a success, false otherwise.
308 public function delete($key) {
309 unset($this->store[$key]);
314 * Deletes several keys from the cache in a single action.
316 * @param array $keys The keys to delete
317 * @return int The number of items successfully deleted.
319 public function delete_many(array $keys) {
321 foreach ($keys as $key) {
322 unset($this->store[$key]);
329 * Purges the cache deleting all items within it.
331 * @return boolean True on success. False otherwise.
333 public function purge() {
334 $this->store = array();
338 * Returns true if the user can add an instance of the store plugin.
342 public static function can_add_instance() {
347 * Performs any necessary clean up when the store instance is being deleted.
349 public function cleanup() {
354 * Generates an instance of the cache store that can be used for testing.
356 * @param cache_definition $definition
359 public static function initialise_test_instance(cache_definition $definition) {
360 // Do something here perhaps.
361 $cache = new cachestore_session('Session test');
362 $cache->initialise($definition);
367 * Returns the name of this instance.
370 public function my_name() {
376 * The session data store class.
378 * @copyright 2012 Sam Hemelryk
379 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
381 abstract class session_data_store {
384 * Used for the actual storage.
387 private static $sessionstore = null;
390 * Returns a static store by reference... REFERENCE SUPER IMPORTANT.
395 protected static function ®ister_store_id($id) {
396 if (is_null(self::$sessionstore)) {
398 if (!isset($SESSION->cachestore_session)) {
399 $SESSION->cachestore_session = array();
401 self::$sessionstore =& $SESSION->cachestore_session;
403 if (!array_key_exists($id, self::$sessionstore)) {
404 self::$sessionstore[$id] = array();
406 return self::$sessionstore[$id];
410 * Flushes the data belong to the given store id.
413 protected static function flush_store_by_id($id) {
414 unset(self::$sessionstore[$id]);
415 self::$sessionstore[$id] = array();
419 * Flushes the store of all data.
421 protected static function flush_store() {
422 $ids = array_keys(self::$sessionstore);
423 unset(self::$sessionstore);
424 self::$sessionstore = array();
425 foreach ($ids as $id) {
426 self::$sessionstore[$id] = array();