Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_cache;
18
 
19
/**
20
 * The cache dummy store.
21
 *
22
 * @copyright  2012 Sam Hemelryk
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @package core_cache
25
 */
26
class dummy_cachestore extends store {
27
    /**
28
     * The name of this store.
29
     * @var string
30
     */
31
    protected $name;
32
 
33
    /**
34
     * Gets set to true if this store is going to store data.
35
     * This happens when the definition doesn't require static acceleration as the loader will not be storing information and
36
     * something has to.
37
     * @var bool
38
     */
39
    protected $persist = false;
40
 
41
    /**
42
     * The stored data array
43
     * @var array
44
     */
45
    protected $store = [];
46
 
47
    /**
48
     * Cache definition
49
     * @var definition
50
     */
51
    protected $definition;
52
 
53
    /**
54
     * Constructs a dummy store instance.
55
     * @param string $name
56
     * @param array $configuration
57
     */
58
    public function __construct($name = 'Dummy store', array $configuration = []) {
59
        $this->name = $name;
60
    }
61
 
62
    /**
63
     * Returns true if this store plugin is usable.
64
     * @return bool
65
     */
66
    public static function are_requirements_met() {
67
        return true;
68
    }
69
 
70
    /**
71
     * Returns true if the user can add an instance.
72
     * @return bool
73
     */
74
    public static function can_add_instance() {
75
        return false;
76
    }
77
 
78
    /**
79
     * Returns the supported features.
80
     * @param array $configuration
81
     * @return int
82
     */
83
    public static function get_supported_features(array $configuration = []) {
84
        return store::SUPPORTS_NATIVE_TTL;
85
    }
86
 
87
    /**
88
     * Returns the supported mode.
89
     * @param array $configuration
90
     * @return int
91
     */
92
    public static function get_supported_modes(array $configuration = []) {
93
        return store::MODE_APPLICATION + store::MODE_REQUEST + store::MODE_SESSION;
94
    }
95
 
96
    /**
97
     * Initialises the store instance for a definition.
98
     * @param definition $definition
99
     */
100
    public function initialise(definition $definition) {
101
        // If the definition isn't using static acceleration then we need to be store data here.
102
        // The reasoning behind this is that:
103
        // - If the definition is using static acceleration then the cache loader is going to
104
        // store things in its static array.
105
        // - If the definition is not using static acceleration then the cache loader won't try to store anything
106
        // and we will need to store it here in order to make sure it is accessible.
107
        if ($definition->get_mode() !== store::MODE_APPLICATION) {
108
            // Neither the request cache nor the session cache provide static acceleration.
109
            $this->persist = true;
110
        } else {
111
            $this->persist = !$definition->use_static_acceleration();
112
        }
113
 
114
        $this->definition = $definition;
115
    }
116
 
117
    /**
118
     * Returns true if this has been initialised.
119
     * @return bool
120
     */
121
    public function is_initialised() {
122
        return (!empty($this->definition));
123
    }
124
 
125
    /**
126
     * Returns true the given mode is supported.
127
     * @param int $mode
128
     * @return bool
129
     */
130
    public static function is_supported_mode($mode) {
131
        return true;
132
    }
133
 
134
    /**
135
     * Returns the data for the given key
136
     * @param string $key
137
     * @return string|false
138
     */
139
    public function get($key) {
140
        if ($this->persist && array_key_exists($key, $this->store)) {
141
            return $this->store[$key];
142
        }
143
        return false;
144
    }
145
 
146
    /**
147
     * Gets' the values for many keys
148
     * @param array $keys
149
     * @return bool
150
     */
151
    public function get_many($keys) {
152
        $return = [];
153
        foreach ($keys as $key) {
154
            if ($this->persist && array_key_exists($key, $this->store)) {
155
                $return[$key] = $this->store[$key];
156
            } else {
157
                $return[$key] = false;
158
            }
159
        }
160
        return $return;
161
    }
162
 
163
    /**
164
     * Sets an item in the cache
165
     * @param string $key
166
     * @param mixed $data
167
     * @return bool
168
     */
169
    public function set($key, $data) {
170
        if ($this->persist) {
171
            $this->store[$key] = $data;
172
        }
173
        return true;
174
    }
175
 
176
    /**
177
     * Sets many items in the cache
178
     * @param array $keyvaluearray
179
     * @return int
180
     */
181
    public function set_many(array $keyvaluearray) {
182
        if ($this->persist) {
183
            foreach ($keyvaluearray as $pair) {
184
                $this->store[$pair['key']] = $pair['value'];
185
            }
186
        }
187
        return count($keyvaluearray);
188
    }
189
 
190
    /**
191
     * Deletes an item from the cache
192
     * @param string $key
193
     * @return bool
194
     */
195
    public function delete($key) {
196
        unset($this->store[$key]);
197
        return true;
198
    }
199
    /**
200
     * Deletes many items from the cache
201
     * @param array $keys
202
     * @return bool
203
     */
204
    public function delete_many(array $keys) {
205
        if ($this->persist) {
206
            foreach ($keys as $key) {
207
                unset($this->store[$key]);
208
            }
209
        }
210
        return count($keys);
211
    }
212
 
213
    /**
214
     * Deletes all of the items from the cache.
215
     * @return bool
216
     */
217
    public function purge() {
218
        $this->store = [];
219
        return true;
220
    }
221
 
222
    /**
223
     * Performs any necessary clean up when the store instance is being deleted.
224
     *
225
     * @deprecated since 3.2
226
     * @see dummy_cachestore::instance_deleted()
227
     */
228
    public function cleanup() {
229
        debugging(
230
            'dummy_cachestore::cleanup() is deprecated. Please use dummy_cachestore::instance_deleted() instead.',
231
            DEBUG_DEVELOPER
232
        );
233
        $this->instance_deleted();
234
    }
235
 
236
    /**
237
     * Performs any necessary operation when the store instance is being deleted.
238
     *
239
     * This method may be called before the store has been initialised.
240
     *
241
     * @since Moodle 3.2
242
     */
243
    public function instance_deleted() {
244
        $this->purge();
245
    }
246
 
247
    /**
248
     * Generates an instance of the cache store that can be used for testing.
249
     *
250
     * @param definition $definition
251
     * @return self
252
     */
253
    public static function initialise_test_instance(definition $definition) {
254
        $cache = new dummy_cachestore('Dummy store test');
255
        if ($cache->is_ready()) {
256
            $cache->initialise($definition);
257
        }
258
        return $cache;
259
    }
260
 
261
    /**
262
     * Generates the appropriate configuration required for unit testing.
263
     *
264
     * @return array Array of unit test configuration data to be used by initialise().
265
     */
266
    public static function unit_test_configuration() {
267
        return [];
268
    }
269
 
270
    /**
271
     * Returns the name of this instance.
272
     * @return string
273
     */
274
    public function my_name() {
275
        return $this->name;
276
    }
277
}
278
 
279
// Alias this class to the old name.
280
// This file will be autoloaded by the legacyclasses autoload system.
281
// In future all uses of this class will be corrected and the legacy references will be removed.
282
class_alias(dummy_cachestore::class, \cachestore_dummy::class);