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/>.
20 * This dummy store is used when a load has no other stores that it can make use of.
21 * This shouldn't happen in normal operation... I think.
23 * This file is part of Moodle's cache API, affectionately called MUC.
24 * It contains the components that are requried in order to use caching.
28 * @copyright 2012 Sam Hemelryk
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 defined('MOODLE_INTERNAL') || die();
35 * The cache dummy store.
37 * @copyright 2012 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class cachestore_dummy implements cache_store {
43 * The name of this store.
49 * Gets set to true if this store is going to persist data.
50 * This happens when the definition doesn't require it as the loader will not be persisting information and something has to.
53 protected $persist = false;
56 * The persistent store array
59 protected $store = array();
62 * Constructs a dummy store instance.
64 * @param array $configuration
66 public function __construct($name = 'Dummy store', array $configuration = array()) {
71 * Returns true if this store plugin is usable.
74 public static function are_requirements_met() {
79 * Returns true if the user can add an instance.
82 public static function can_add_instance() {
87 * Returns the supported features.
88 * @param array $configuration
91 public static function get_supported_features(array $configuration = array()) {
92 return self::SUPPORTS_NATIVE_TTL;
96 * Returns the supported mode.
97 * @param array $configuration
100 public static function get_supported_modes(array $configuration = array()) {
101 return self::MODE_APPLICATION + self::MODE_REQUEST + self::MODE_SESSION;
105 * Initialises the store instance for a definition.
106 * @param cache_definition $definition
108 public function initialise(cache_definition $definition) {
109 // If the definition isn't persistent then we need to be persistent here.
110 $this->persist = !$definition->should_be_persistent();
114 * Returns true if this has been initialised.
117 public function is_initialised() {
118 return (!empty($this->definition));
122 * Returns true if this is ready.
125 public function is_ready() {
130 * Returns true the given mode is supported.
134 public static function is_supported_mode($mode) {
139 * Returns true if this store supports data guarantee.
142 public function supports_data_guarantee() {
147 * Returns true if this store supports multiple identifiers.
150 public function supports_multiple_identifiers() {
155 * Returns true if this store supports a native ttl.
158 public function supports_native_ttl() {
163 * Returns the data for the given key
165 * @return string|false
167 public function get($key) {
168 if ($this->persist && array_key_exists($key, $this->store)) {
169 return $this->store[$key];
175 * Gets' the values for many keys
179 public function get_many($keys) {
181 foreach ($keys as $key) {
182 if ($this->persist && array_key_exists($key, $this->store)) {
183 $return[$key] = $this->store[$key];
185 $return[$key] = false;
192 * Sets an item in the cache
197 public function set($key, $data) {
198 if ($this->persist) {
199 $this->store[$key] = $data;
205 * Sets many items in the cache
206 * @param array $keyvaluearray
209 public function set_many(array $keyvaluearray) {
210 if ($this->persist) {
211 foreach ($keyvaluearray as $pair) {
212 $this->store[$pair['key']] = $pair['value'];
214 return count($keyvaluearray);
220 * Deletes an item from the cache
224 public function delete($key) {
225 unset($this->store[$key]);
229 * Deletes many items from the cache
233 public function delete_many(array $keys) {
234 if ($this->persist) {
235 foreach ($keys as $key) {
236 unset($this->store[$key]);
243 * Deletes all of the items from the cache.
246 public function purge() {
247 $this->store = array();
252 * Performs any necessary clean up when the store instance is being deleted.
254 public function cleanup() {
259 * Generates an instance of the cache store that can be used for testing.
261 * @param cache_definition $definition
264 public static function initialise_test_instance(cache_definition $definition) {
265 $cache = new cachestore_dummy('Dummy store test');
266 $cache->initialise($definition);
271 * Returns the name of this instance.
274 public function my_name() {