forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdummystore.php
283 lines (257 loc) · 7.68 KB
/
dummystore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cache dummy store.
*
* This dummy store is used when a load has no other stores that it can make use of.
* This shouldn't happen in normal operation... I think.
*
* This file is part of Moodle's cache API, affectionately called MUC.
* It contains the components that are requried in order to use caching.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* The cache dummy store.
*
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_dummy extends cache_store {
/**
* The name of this store.
* @var string
*/
protected $name;
/**
* Gets set to true if this store is going to store data.
* This happens when the definition doesn't require static acceleration as the loader will not be storing information and
* something has to.
* @var bool
*/
protected $persist = false;
/**
* The stored data array
* @var array
*/
protected $store = array();
/**
* Constructs a dummy store instance.
* @param string $name
* @param array $configuration
*/
public function __construct($name = 'Dummy store', array $configuration = array()) {
$this->name = $name;
}
/**
* Returns true if this store plugin is usable.
* @return bool
*/
public static function are_requirements_met() {
return true;
}
/**
* Returns true if the user can add an instance.
* @return bool
*/
public static function can_add_instance() {
return false;
}
/**
* Returns the supported features.
* @param array $configuration
* @return int
*/
public static function get_supported_features(array $configuration = array()) {
return self::SUPPORTS_NATIVE_TTL;
}
/**
* Returns the supported mode.
* @param array $configuration
* @return int
*/
public static function get_supported_modes(array $configuration = array()) {
return self::MODE_APPLICATION + self::MODE_REQUEST + self::MODE_SESSION;
}
/**
* Initialises the store instance for a definition.
* @param cache_definition $definition
*/
public function initialise(cache_definition $definition) {
// If the definition isn't using static acceleration then we need to be store data here.
// The reasoning behind this is that:
// - If the definition is using static acceleration then the cache loader is going to
// store things in its static array.
// - If the definition is not using static acceleration then the cache loader won't try to store anything
// and we will need to store it here in order to make sure it is accessible.
if ($definition->get_mode() !== self::MODE_APPLICATION) {
// Neither the request cache nor the session cache provide static acceleration.
$this->persist = true;
} else {
$this->persist = !$definition->use_static_acceleration();
}
}
/**
* Returns true if this has been initialised.
* @return bool
*/
public function is_initialised() {
return (!empty($this->definition));
}
/**
* Returns true the given mode is supported.
* @param int $mode
* @return bool
*/
public static function is_supported_mode($mode) {
return true;
}
/**
* Returns the data for the given key
* @param string $key
* @return string|false
*/
public function get($key) {
if ($this->persist && array_key_exists($key, $this->store)) {
return $this->store[$key];
}
return false;
}
/**
* Gets' the values for many keys
* @param array $keys
* @return bool
*/
public function get_many($keys) {
$return = array();
foreach ($keys as $key) {
if ($this->persist && array_key_exists($key, $this->store)) {
$return[$key] = $this->store[$key];
} else {
$return[$key] = false;
}
}
return $return;
}
/**
* Sets an item in the cache
* @param string $key
* @param mixed $data
* @return bool
*/
public function set($key, $data) {
if ($this->persist) {
$this->store[$key] = $data;
}
return true;
}
/**
* Sets many items in the cache
* @param array $keyvaluearray
* @return int
*/
public function set_many(array $keyvaluearray) {
if ($this->persist) {
foreach ($keyvaluearray as $pair) {
$this->store[$pair['key']] = $pair['value'];
}
}
return count($keyvaluearray);
}
/**
* Deletes an item from the cache
* @param string $key
* @return bool
*/
public function delete($key) {
unset($this->store[$key]);
return true;
}
/**
* Deletes many items from the cache
* @param array $keys
* @return bool
*/
public function delete_many(array $keys) {
if ($this->persist) {
foreach ($keys as $key) {
unset($this->store[$key]);
}
}
return count($keys);
}
/**
* Deletes all of the items from the cache.
* @return bool
*/
public function purge() {
$this->store = array();
return true;
}
/**
* Performs any necessary clean up when the store instance is being deleted.
*
* @deprecated since 3.2
* @see cachestore_dummy::instance_deleted()
*/
public function cleanup() {
debugging('cachestore_dummy::cleanup() is deprecated. Please use cachestore_dummy::instance_deleted() instead.',
DEBUG_DEVELOPER);
$this->instance_deleted();
}
/**
* Performs any necessary operation when the store instance is being deleted.
*
* This method may be called before the store has been initialised.
*
* @since Moodle 3.2
*/
public function instance_deleted() {
$this->purge();
}
/**
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @return false
*/
public static function initialise_test_instance(cache_definition $definition) {
$cache = new cachestore_dummy('Dummy store test');
if ($cache->is_ready()) {
$cache->initialise($definition);
}
return $cache;
}
/**
* Generates the appropriate configuration required for unit testing.
*
* @return array Array of unit test configuration data to be used by initialise().
*/
public static function unit_test_configuration() {
return [];
}
/**
* Returns the name of this instance.
* @return string
*/
public function my_name() {
return $this->name;
}
}