MDL-40903 cache: fixed up event invalidation
[moodle.git] / cache / classes / dummystore.php
CommitLineData
8139ad13
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 * Cache dummy store.
19 *
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.
22 *
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.
25 *
26 * @package core
27 * @category cache
28 * @copyright 2012 Sam Hemelryk
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31
32defined('MOODLE_INTERNAL') || die();
33
34/**
35 * The cache dummy store.
36 *
37 * @copyright 2012 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
75cde6b9 40class cachestore_dummy extends cache_store {
170f821b 41
8139ad13
SH
42 /**
43 * The name of this store.
44 * @var string
45 */
46 protected $name;
47
48 /**
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.
51 * @var bool
52 */
53 protected $persist = false;
170f821b 54
8139ad13
SH
55 /**
56 * The persistent store array
57 * @var array
58 */
59 protected $store = array();
60
61 /**
62 * Constructs a dummy store instance.
63 * @param string $name
64 * @param array $configuration
65 */
66 public function __construct($name = 'Dummy store', array $configuration = array()) {
67 $this->name = $name;
68 }
69
70 /**
71 * Returns true if this store plugin is usable.
72 * @return bool
73 */
74 public static function are_requirements_met() {
75 return true;
76 }
77
78 /**
79 * Returns true if the user can add an instance.
80 * @return bool
81 */
82 public static function can_add_instance() {
83 return false;
84 }
85
86 /**
87 * Returns the supported features.
88 * @param array $configuration
89 * @return int
90 */
91 public static function get_supported_features(array $configuration = array()) {
92 return self::SUPPORTS_NATIVE_TTL;
93 }
94
95 /**
96 * Returns the supported mode.
97 * @param array $configuration
98 * @return int
99 */
100 public static function get_supported_modes(array $configuration = array()) {
101 return self::MODE_APPLICATION + self::MODE_REQUEST + self::MODE_SESSION;
102 }
103
104 /**
105 * Initialises the store instance for a definition.
106 * @param cache_definition $definition
107 */
108 public function initialise(cache_definition $definition) {
109 // If the definition isn't persistent then we need to be persistent here.
82afd05c
SH
110 // The reasoning behind this is that:
111 // - If the definition is persistent then the cache loader is going to
112 // store things in its persistent cache.
113 // - If the definition is not persistent then the cache loader won't try to store anything
114 // and we will need to store it here in order to make sure it is accessible.
083fa877 115 $this->persist = !$definition->data_should_be_persistent();
8139ad13
SH
116 }
117
118 /**
119 * Returns true if this has been initialised.
120 * @return bool
121 */
122 public function is_initialised() {
123 return (!empty($this->definition));
124 }
125
126 /**
127 * Returns true if this is ready.
128 * @return bool
129 */
130 public function is_ready() {
131 return true;
132 }
133
134 /**
135 * Returns true the given mode is supported.
136 * @param int $mode
137 * @return bool
138 */
139 public static function is_supported_mode($mode) {
140 return true;
141 }
142
8139ad13
SH
143 /**
144 * Returns the data for the given key
145 * @param string $key
146 * @return string|false
147 */
148 public function get($key) {
149 if ($this->persist && array_key_exists($key, $this->store)) {
150 return $this->store[$key];
151 }
152 return false;
153 }
154
155 /**
156 * Gets' the values for many keys
157 * @param array $keys
158 * @return bool
159 */
160 public function get_many($keys) {
161 $return = array();
162 foreach ($keys as $key) {
163 if ($this->persist && array_key_exists($key, $this->store)) {
164 $return[$key] = $this->store[$key];
165 } else {
166 $return[$key] = false;
167 }
168 }
169 return $return;
170 }
171
172 /**
173 * Sets an item in the cache
174 * @param string $key
175 * @param mixed $data
176 * @return bool
177 */
178 public function set($key, $data) {
179 if ($this->persist) {
180 $this->store[$key] = $data;
181 }
182 return true;
183 }
184
185 /**
186 * Sets many items in the cache
187 * @param array $keyvaluearray
188 * @return int
189 */
190 public function set_many(array $keyvaluearray) {
191 if ($this->persist) {
192 foreach ($keyvaluearray as $pair) {
193 $this->store[$pair['key']] = $pair['value'];
194 }
195 return count($keyvaluearray);
196 }
197 return 0;
198 }
199
200 /**
201 * Deletes an item from the cache
202 * @param string $key
203 * @return bool
204 */
205 public function delete($key) {
206 unset($this->store[$key]);
207 return true;
208 }
209 /**
210 * Deletes many items from the cache
211 * @param array $keys
212 * @return bool
213 */
214 public function delete_many(array $keys) {
215 if ($this->persist) {
216 foreach ($keys as $key) {
217 unset($this->store[$key]);
218 }
219 }
220 return count($keys);
221 }
222
223 /**
224 * Deletes all of the items from the cache.
225 * @return bool
226 */
227 public function purge() {
228 $this->store = array();
229 return true;
230 }
231
232 /**
233 * Performs any necessary clean up when the store instance is being deleted.
234 */
235 public function cleanup() {
236 $this->purge();
237 }
238
239 /**
240 * Generates an instance of the cache store that can be used for testing.
241 *
242 * @param cache_definition $definition
243 * @return false
244 */
245 public static function initialise_test_instance(cache_definition $definition) {
6fec1820 246 $cache = new cachestore_dummy('Dummy store test');
8139ad13 247 $cache->initialise($definition);
a3f3ea26 248 return $cache;
8139ad13 249 }
34c84c72
SH
250
251 /**
252 * Returns the name of this instance.
253 * @return string
254 */
255 public function my_name() {
256 return $this->name;
257 }
a3f3ea26 258}