MDL-36768 cache: replaced cache_store interface with abstract class
[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.
110 $this->persist = !$definition->should_be_persistent();
111 }
112
113 /**
114 * Returns true if this has been initialised.
115 * @return bool
116 */
117 public function is_initialised() {
118 return (!empty($this->definition));
119 }
120
121 /**
122 * Returns true if this is ready.
123 * @return bool
124 */
125 public function is_ready() {
126 return true;
127 }
128
129 /**
130 * Returns true the given mode is supported.
131 * @param int $mode
132 * @return bool
133 */
134 public static function is_supported_mode($mode) {
135 return true;
136 }
137
8139ad13
SH
138 /**
139 * Returns the data for the given key
140 * @param string $key
141 * @return string|false
142 */
143 public function get($key) {
144 if ($this->persist && array_key_exists($key, $this->store)) {
145 return $this->store[$key];
146 }
147 return false;
148 }
149
150 /**
151 * Gets' the values for many keys
152 * @param array $keys
153 * @return bool
154 */
155 public function get_many($keys) {
156 $return = array();
157 foreach ($keys as $key) {
158 if ($this->persist && array_key_exists($key, $this->store)) {
159 $return[$key] = $this->store[$key];
160 } else {
161 $return[$key] = false;
162 }
163 }
164 return $return;
165 }
166
167 /**
168 * Sets an item in the cache
169 * @param string $key
170 * @param mixed $data
171 * @return bool
172 */
173 public function set($key, $data) {
174 if ($this->persist) {
175 $this->store[$key] = $data;
176 }
177 return true;
178 }
179
180 /**
181 * Sets many items in the cache
182 * @param array $keyvaluearray
183 * @return int
184 */
185 public function set_many(array $keyvaluearray) {
186 if ($this->persist) {
187 foreach ($keyvaluearray as $pair) {
188 $this->store[$pair['key']] = $pair['value'];
189 }
190 return count($keyvaluearray);
191 }
192 return 0;
193 }
194
195 /**
196 * Deletes an item from the cache
197 * @param string $key
198 * @return bool
199 */
200 public function delete($key) {
201 unset($this->store[$key]);
202 return true;
203 }
204 /**
205 * Deletes many items from the cache
206 * @param array $keys
207 * @return bool
208 */
209 public function delete_many(array $keys) {
210 if ($this->persist) {
211 foreach ($keys as $key) {
212 unset($this->store[$key]);
213 }
214 }
215 return count($keys);
216 }
217
218 /**
219 * Deletes all of the items from the cache.
220 * @return bool
221 */
222 public function purge() {
223 $this->store = array();
224 return true;
225 }
226
227 /**
228 * Performs any necessary clean up when the store instance is being deleted.
229 */
230 public function cleanup() {
231 $this->purge();
232 }
233
234 /**
235 * Generates an instance of the cache store that can be used for testing.
236 *
237 * @param cache_definition $definition
238 * @return false
239 */
240 public static function initialise_test_instance(cache_definition $definition) {
6fec1820 241 $cache = new cachestore_dummy('Dummy store test');
8139ad13 242 $cache->initialise($definition);
a3f3ea26 243 return $cache;
8139ad13 244 }
34c84c72
SH
245
246 /**
247 * Returns the name of this instance.
248 * @return string
249 */
250 public function my_name() {
251 return $this->name;
252 }
a3f3ea26 253}