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 |
* Cache Loader.
|
|
|
21 |
*
|
|
|
22 |
* This cache loader interface provides the required structure for classes that wish to be interacted with as a
|
|
|
23 |
* means of accessing and interacting with a cache.
|
|
|
24 |
*
|
|
|
25 |
* Can be implemented by any class wishing to be a cache loader.
|
|
|
26 |
* @package core_cache
|
|
|
27 |
* @copyright Sam Hemelryk
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
interface loader_interface {
|
|
|
31 |
/**
|
|
|
32 |
* Retrieves the value for the given key from the cache.
|
|
|
33 |
*
|
|
|
34 |
* @param string|int $key The key for the data being requested.
|
|
|
35 |
* @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
|
|
|
36 |
* @return mixed The data retrieved from the cache, or false if the key did not exist within the cache.
|
|
|
37 |
* If MUST_EXIST was used then an exception will be thrown if the key does not exist within the cache.
|
|
|
38 |
*/
|
|
|
39 |
public function get($key, $strictness = IGNORE_MISSING);
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Retrieves the value and actual version for the given key, with at least the required version.
|
|
|
43 |
*
|
|
|
44 |
* If there is no value for the key, or there is a value but it doesn't have the required
|
|
|
45 |
* version, then this function will return false (or throw an exception if you set strictness
|
|
|
46 |
* to MUST_EXIST).
|
|
|
47 |
*
|
|
|
48 |
* This function can be used to make it easier to support localisable caches (where the cache
|
|
|
49 |
* could be stored on a local server as well as a shared cache). Specifying the version means
|
|
|
50 |
* that it will automatically retrieve the correct version if available, either from the local
|
|
|
51 |
* server or [if that has an older version] from the shared server.
|
|
|
52 |
*
|
|
|
53 |
* If the cached version is newer than specified version, it will be returned regardless. For
|
|
|
54 |
* example, if you request version 4, but the locally cached version is 5, it will be returned.
|
|
|
55 |
* If you request version 6, and the locally cached version is 5, then the system will look in
|
|
|
56 |
* higher-level caches (if any); if there still isn't a version 6 or greater, it will return
|
|
|
57 |
* null.
|
|
|
58 |
*
|
|
|
59 |
* You must use this function if you use set_versioned.
|
|
|
60 |
*
|
|
|
61 |
* @param string|int $key The key for the data being requested.
|
|
|
62 |
* @param int $requiredversion Minimum required version of the data
|
|
|
63 |
* @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
|
|
|
64 |
* @param mixed $actualversion If specified, will be set to the actual version number retrieved
|
|
|
65 |
* @return mixed Data from the cache, or false if the key did not exist or was too old
|
|
|
66 |
*/
|
|
|
67 |
public function get_versioned($key, int $requiredversion, int $strictness = IGNORE_MISSING, &$actualversion = null);
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Retrieves an array of values for an array of keys.
|
|
|
71 |
*
|
|
|
72 |
* Using this function comes with potential performance implications.
|
|
|
73 |
* Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
|
|
|
74 |
* the equivalent singular method for each item provided.
|
|
|
75 |
* This should not deter you from using this function as there is a performance benefit in situations where the cache
|
|
|
76 |
* store does support it, but you should be aware of this fact.
|
|
|
77 |
*
|
|
|
78 |
* @param array $keys The keys of the data being requested.
|
|
|
79 |
* @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
|
|
|
80 |
* @return array An array of key value pairs for the items that could be retrieved from the cache.
|
|
|
81 |
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
|
|
|
82 |
* Otherwise any key that did not exist will have a data value of false within the results.
|
|
|
83 |
*/
|
|
|
84 |
public function get_many(array $keys, $strictness = IGNORE_MISSING);
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Sends a key => value pair to the cache.
|
|
|
88 |
*
|
|
|
89 |
* <code>
|
|
|
90 |
* // This code will add four entries to the cache, one for each url.
|
|
|
91 |
* $cache->set('main', 'http://moodle.org');
|
|
|
92 |
* $cache->set('docs', 'http://docs.moodle.org');
|
|
|
93 |
* $cache->set('tracker', 'http://tracker.moodle.org');
|
|
|
94 |
* $cache->set('qa', 'http://qa.moodle.net');
|
|
|
95 |
* </code>
|
|
|
96 |
*
|
|
|
97 |
* @param string|int $key The key for the data being requested.
|
|
|
98 |
* @param mixed $data The data to set against the key.
|
|
|
99 |
* @return bool True on success, false otherwise.
|
|
|
100 |
*/
|
|
|
101 |
public function set($key, $data);
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Sets the value for the given key with the given version.
|
|
|
105 |
*
|
|
|
106 |
* The cache does not store multiple versions - any existing version will be overwritten with
|
|
|
107 |
* this one. This function should only be used if there is a known 'current version' (e.g.
|
|
|
108 |
* stored in a database table). It only ensures that the cache does not return outdated data.
|
|
|
109 |
*
|
|
|
110 |
* This function can be used to help implement localisable caches (where the cache could be
|
|
|
111 |
* stored on a local server as well as a shared cache). The version will be recorded alongside
|
|
|
112 |
* the item and get_versioned will always return the correct version.
|
|
|
113 |
*
|
|
|
114 |
* The version number must be an integer that always increases. This could be based on the
|
|
|
115 |
* current time, or a stored value that increases by 1 each time it changes, etc.
|
|
|
116 |
*
|
|
|
117 |
* If you use this function you must use get_versioned to retrieve the data.
|
|
|
118 |
*
|
|
|
119 |
* @param string|int $key The key for the data being set.
|
|
|
120 |
* @param int $version Integer for the version of the data
|
|
|
121 |
* @param mixed $data The data to set against the key.
|
|
|
122 |
* @return bool True on success, false otherwise.
|
|
|
123 |
*/
|
|
|
124 |
public function set_versioned($key, int $version, $data): bool;
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Sends several key => value pairs to the cache.
|
|
|
128 |
*
|
|
|
129 |
* Using this function comes with potential performance implications.
|
|
|
130 |
* Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
|
|
|
131 |
* the equivalent singular method for each item provided.
|
|
|
132 |
* This should not deter you from using this function as there is a performance benefit in situations where the cache store
|
|
|
133 |
* does support it, but you should be aware of this fact.
|
|
|
134 |
*
|
|
|
135 |
* <code>
|
|
|
136 |
* // This code will add four entries to the cache, one for each url.
|
|
|
137 |
* $cache->set_many(array(
|
|
|
138 |
* 'main' => 'http://moodle.org',
|
|
|
139 |
* 'docs' => 'http://docs.moodle.org',
|
|
|
140 |
* 'tracker' => 'http://tracker.moodle.org',
|
|
|
141 |
* 'qa' => ''http://qa.moodle.net'
|
|
|
142 |
* ));
|
|
|
143 |
* </code>
|
|
|
144 |
*
|
|
|
145 |
* @param array $keyvaluearray An array of key => value pairs to send to the cache.
|
|
|
146 |
* @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
|
|
|
147 |
* ... if they care that is.
|
|
|
148 |
*/
|
|
|
149 |
public function set_many(array $keyvaluearray);
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Test is a cache has a key.
|
|
|
153 |
*
|
|
|
154 |
* The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
|
|
|
155 |
* test and any subsequent action (get, set, delete etc).
|
|
|
156 |
* Instead it is recommended to write your code in such a way they it performs the following steps:
|
|
|
157 |
* <ol>
|
|
|
158 |
* <li>Attempt to retrieve the information.</li>
|
|
|
159 |
* <li>Generate the information.</li>
|
|
|
160 |
* <li>Attempt to set the information</li>
|
|
|
161 |
* </ol>
|
|
|
162 |
*
|
|
|
163 |
* Its also worth mentioning that not all stores support key tests.
|
|
|
164 |
* For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
|
|
|
165 |
* Just one more reason you should not use these methods unless you have a very good reason to do so.
|
|
|
166 |
*
|
|
|
167 |
* @param string|int $key
|
|
|
168 |
* @return bool True if the cache has the requested key, false otherwise.
|
|
|
169 |
*/
|
|
|
170 |
public function has($key);
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Test if a cache has at least one of the given keys.
|
|
|
174 |
*
|
|
|
175 |
* It is strongly recommended to avoid the use of this function if not absolutely required.
|
|
|
176 |
* In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
|
|
|
177 |
*
|
|
|
178 |
* Its also worth mentioning that not all stores support key tests.
|
|
|
179 |
* For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
|
|
|
180 |
* Just one more reason you should not use these methods unless you have a very good reason to do so.
|
|
|
181 |
*
|
|
|
182 |
* @param array $keys
|
|
|
183 |
* @return bool True if the cache has at least one of the given keys
|
|
|
184 |
*/
|
|
|
185 |
public function has_any(array $keys);
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Test is a cache has all of the given keys.
|
|
|
189 |
*
|
|
|
190 |
* It is strongly recommended to avoid the use of this function if not absolutely required.
|
|
|
191 |
* In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
|
|
|
192 |
*
|
|
|
193 |
* Its also worth mentioning that not all stores support key tests.
|
|
|
194 |
* For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
|
|
|
195 |
* Just one more reason you should not use these methods unless you have a very good reason to do so.
|
|
|
196 |
*
|
|
|
197 |
* @param array $keys
|
|
|
198 |
* @return bool True if the cache has all of the given keys, false otherwise.
|
|
|
199 |
*/
|
|
|
200 |
public function has_all(array $keys);
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Delete the given key from the cache.
|
|
|
204 |
*
|
|
|
205 |
* @param string|int $key The key to delete.
|
|
|
206 |
* @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
|
|
|
207 |
* This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
|
|
|
208 |
* @return bool True of success, false otherwise.
|
|
|
209 |
*/
|
|
|
210 |
public function delete($key, $recurse = true);
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Delete all of the given keys from the cache.
|
|
|
214 |
*
|
|
|
215 |
* @param array $keys The key to delete.
|
|
|
216 |
* @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
|
|
|
217 |
* This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
|
|
|
218 |
* @return int The number of items successfully deleted.
|
|
|
219 |
*/
|
|
|
220 |
public function delete_many(array $keys, $recurse = true);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// Alias this class to the old name.
|
|
|
224 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
225 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
226 |
class_alias(loader_interface::class, \cache_loader::class);
|