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
use core\exception\coding_exception;
20
 
21
/**
22
 * The cache loader class used when the Cache has been disabled.
23
 *
24
 * @package core_cache
25
 * @copyright  2012 Sam Hemelryk
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class disabled_cache extends cache implements loader_with_locking_interface {
29
    /**
30
     * Constructs the cache.
31
     *
32
     * @param definition $definition
33
     * @param store $store
34
     * @param null $loader Unused.
35
     */
36
    public function __construct(definition $definition, store $store, $loader = null) {
37
        if ($loader instanceof data_source_interface) {
38
            // Set the data source to allow data sources to work when caching is entirely disabled.
39
            $this->set_data_source($loader);
40
        }
41
 
42
        // No other features are handled.
43
    }
44
 
45
    /**
46
     * Gets a key from the cache.
47
     *
48
     * @param int|string $key
49
     * @param int $requiredversion Minimum required version of the data or cache::VERSION_NONE
50
     * @param int $strictness Unused.
51
     * @param mixed &$actualversion If specified, will be set to the actual version number retrieved
52
     * @return bool
53
     */
54
    protected function get_implementation($key, int $requiredversion, int $strictness, &$actualversion = null) {
55
        $datasource = $this->get_datasource();
56
        if ($datasource !== false) {
57
            if ($requiredversion === cache::VERSION_NONE) {
58
                return $datasource->load_for_cache($key);
59
            } else {
60
                if (!$datasource instanceof versionable_data_source_interface) {
61
                    throw new coding_exception('Data source is not versionable');
62
                }
63
                $result = $datasource->load_for_cache_versioned($key, $requiredversion, $actualversion);
64
                if ($result && $actualversion < $requiredversion) {
65
                    throw new coding_exception('Data source returned outdated version');
66
                }
67
                return $result;
68
            }
69
        }
70
        return false;
71
    }
72
 
73
    /**
74
     * Gets many keys at once from the cache.
75
     *
76
     * @param array $keys
77
     * @param int $strictness Unused.
78
     * @return array
79
     */
80
    public function get_many(array $keys, $strictness = IGNORE_MISSING) {
81
        if ($this->get_datasource() !== false) {
82
            return $this->get_datasource()->load_many_for_cache($keys);
83
        }
84
 
85
        return array_combine($keys, array_fill(0, count($keys), false));
86
    }
87
 
88
    /**
89
     * Sets a key value pair in the cache.
90
     *
91
     * @param int|string $key Unused.
92
     * @param int $version Unused.
93
     * @param mixed $data Unused.
94
     * @param bool $setparents Unused.
95
     * @return bool
96
     */
97
    protected function set_implementation($key, int $version, $data, bool $setparents = true): bool {
98
        return false;
99
    }
100
 
101
    /**
102
     * Sets many key value pairs in the cache at once.
103
     *
104
     * @param array $keyvaluearray Unused.
105
     * @return int
106
     */
107
    public function set_many(array $keyvaluearray) {
108
        return 0;
109
    }
110
 
111
    /**
112
     * Deletes an item from the cache.
113
     *
114
     * @param int|string $key Unused.
115
     * @param bool $recurse Unused.
116
     * @return bool
117
     */
118
    public function delete($key, $recurse = true) {
119
        return false;
120
    }
121
 
122
    /**
123
     * Deletes many items at once from the cache.
124
     *
125
     * @param array $keys Unused.
126
     * @param bool $recurse Unused.
127
     * @return int
128
     */
129
    public function delete_many(array $keys, $recurse = true) {
130
        return 0;
131
    }
132
 
133
    /**
134
     * Checks if the cache has the requested key.
135
     *
136
     * @param int|string $key Unused.
137
     * @param bool $tryloadifpossible Unused.
138
     * @return bool
139
     */
140
    public function has($key, $tryloadifpossible = false) {
141
        $result = $this->get($key);
142
 
143
        return $result !== false;
144
    }
145
 
146
    /**
147
     * Checks if the cache has all of the requested keys.
148
     * @param array $keys Unused.
149
     * @return bool
150
     */
151
    public function has_all(array $keys) {
152
        if (!$this->get_datasource()) {
153
            return false;
154
        }
155
 
156
        foreach ($keys as $key) {
157
            if (!$this->has($key)) {
158
                return false;
159
            }
160
        }
161
        return true;
162
    }
163
 
164
    /**
165
     * Checks if the cache has any of the requested keys.
166
     *
167
     * @param array $keys Unused.
168
     * @return bool
169
     */
170
    public function has_any(array $keys) {
171
        foreach ($keys as $key) {
172
            if ($this->has($key)) {
173
                return true;
174
            }
175
        }
176
 
177
        return false;
178
    }
179
 
180
    /**
181
     * Purges all items from the cache.
182
     *
183
     * @return bool
184
     */
185
    public function purge() {
186
        return true;
187
    }
188
 
189
    /**
190
     * Pretend that we got a lock to avoid errors.
191
     *
192
     * @param int|string $key
193
     * @return bool
194
     */
195
    public function acquire_lock($key): bool {
196
        return true;
197
    }
198
 
199
    /**
200
     * Pretend that we released a lock to avoid errors.
201
     *
202
     * @param int|string $key
203
     * @return bool
204
     */
205
    public function release_lock($key): bool {
206
        return true;
207
    }
208
 
209
    /**
210
     * Pretend that we have a lock to avoid errors.
211
     *
212
     * @param int|string $key
213
     * @return bool
214
     */
215
    public function check_lock_state($key): bool {
216
        return true;
217
    }
218
}
219
 
220
// Alias this class to the old name.
221
// This file will be autoloaded by the legacyclasses autoload system.
222
// In future all uses of this class will be corrected and the legacy references will be removed.
223
class_alias(disabled_cache::class, \cache_disabled::class);