Línea 12... |
Línea 12... |
12 |
// GNU General Public License for more details.
|
12 |
// GNU General Public License for more details.
|
13 |
//
|
13 |
//
|
14 |
// You should have received a copy of the GNU General Public License
|
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/>.
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
Línea 16... |
Línea -... |
16 |
|
- |
|
17 |
/**
|
- |
|
18 |
* Cache helper class
|
- |
|
19 |
*
|
- |
|
20 |
* This file is part of Moodle's cache API, affectionately called MUC.
|
- |
|
21 |
* It contains the components that are requried in order to use caching.
|
- |
|
22 |
*
|
- |
|
23 |
* @package core
|
16 |
|
24 |
* @category cache
|
- |
|
25 |
* @copyright 2012 Sam Hemelryk
|
- |
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
Línea -... |
Línea 17... |
- |
|
17 |
namespace core_cache;
|
27 |
*/
|
18 |
|
- |
|
19 |
use core\lang_string;
|
Línea 28... |
Línea 20... |
28 |
|
20 |
use core\exception\coding_exception;
|
29 |
defined('MOODLE_INTERNAL') || die();
|
21 |
use DirectoryIterator;
|
30 |
|
22 |
|
31 |
/**
|
23 |
/**
|
32 |
* The cache helper class.
|
24 |
* The cache helper class.
|
33 |
*
|
25 |
*
|
34 |
* The cache helper class provides common functionality to the cache API and is useful to developers within to interact with
|
26 |
* The cache helper class provides common functionality to the cache API and is useful to developers within to interact with
|
35 |
* the cache API in a general way.
|
27 |
* the cache API in a general way.
|
36 |
*
|
28 |
*
|
37 |
* @package core
|
29 |
* @package core_cache
|
38 |
* @category cache
|
30 |
* @category cache
|
39 |
* @copyright 2012 Sam Hemelryk
|
31 |
* @copyright 2012 Sam Hemelryk
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
41 |
*/
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
42 |
class cache_helper {
|
33 |
*/
|
43 |
|
34 |
class helper {
|
44 |
/**
|
35 |
/**
|
45 |
* Statistics gathered by the cache API during its operation will be used here.
|
36 |
* Statistics gathered by the cache API during its operation will be used here.
|
46 |
* @static
|
37 |
* @static
|
Línea 47... |
Línea 38... |
47 |
* @var array
|
38 |
* @var array
|
48 |
*/
|
39 |
*/
|
49 |
protected static $stats = array();
|
40 |
protected static $stats = [];
|
50 |
|
41 |
|
51 |
/**
|
42 |
/**
|
Línea 52... |
Línea 43... |
52 |
* The instance of the cache helper.
|
43 |
* The instance of the cache helper.
|
53 |
* @var cache_helper
|
44 |
* @var self
|
Línea 68... |
Línea 59... |
68 |
* configuration file has been created which allows use to set up caching when ever is required.
|
59 |
* configuration file has been created which allows use to set up caching when ever is required.
|
69 |
*
|
60 |
*
|
70 |
* @return bool
|
61 |
* @return bool
|
71 |
*/
|
62 |
*/
|
72 |
public static function ready_for_early_init() {
|
63 |
public static function ready_for_early_init() {
|
73 |
return cache_config::config_file_exists();
|
64 |
return config::config_file_exists();
|
74 |
}
|
65 |
}
|
Línea 75... |
Línea 66... |
75 |
|
66 |
|
76 |
/**
|
67 |
/**
|
77 |
* Returns an instance of the cache_helper.
|
68 |
* Returns an instance of the helper.
|
78 |
*
|
69 |
*
|
79 |
* This is designed for internal use only and acts as a static store.
|
70 |
* This is designed for internal use only and acts as a static store.
|
80 |
* @staticvar null $instance
|
71 |
* @staticvar null $instance
|
81 |
* @return cache_helper
|
72 |
* @return self
|
82 |
*/
|
73 |
*/
|
83 |
protected static function instance() {
|
74 |
protected static function instance() {
|
84 |
if (is_null(self::$instance)) {
|
75 |
if (is_null(self::$instance)) {
|
85 |
self::$instance = new cache_helper();
|
76 |
self::$instance = new self();
|
86 |
}
|
77 |
}
|
87 |
return self::$instance;
|
78 |
return self::$instance;
|
Línea 88... |
Línea 79... |
88 |
}
|
79 |
}
|
89 |
|
80 |
|
90 |
/**
|
81 |
/**
|
91 |
* Constructs an instance of the cache_helper class. Again for internal use only.
|
82 |
* Constructs an instance of the helper class. Again for internal use only.
|
92 |
*/
|
83 |
*/
|
93 |
protected function __construct() {
|
84 |
protected function __construct() {
|
Línea 94... |
Línea 85... |
94 |
// Nothing to do here, just making sure you can't get an instance of this.
|
85 |
// Nothing to do here, just making sure you can't get an instance of this.
|
95 |
}
|
86 |
}
|
96 |
|
87 |
|
97 |
/**
|
88 |
/**
|
98 |
* Used as a data store for initialised definitions.
|
89 |
* Used as a data store for initialised definitions.
|
Línea 99... |
Línea 90... |
99 |
* @var array
|
90 |
* @var array
|
100 |
*/
|
91 |
*/
|
101 |
protected $definitions = array();
|
92 |
protected $definitions = [];
|
102 |
|
93 |
|
103 |
/**
|
94 |
/**
|
104 |
* Used as a data store for initialised cache stores
|
95 |
* Used as a data store for initialised cache stores
|
Línea 105... |
Línea 96... |
105 |
* We use this because we want to avoid establishing multiple instances of a single store.
|
96 |
* We use this because we want to avoid establishing multiple instances of a single store.
|
106 |
* @var array
|
97 |
* @var array
|
107 |
*/
|
98 |
*/
|
108 |
protected $stores = array();
|
99 |
protected $stores = [];
|
109 |
|
100 |
|
110 |
/**
|
101 |
/**
|
111 |
* Returns the class for use as a cache loader for the given mode.
|
102 |
* Returns the class for use as a cache loader for the given mode.
|
112 |
*
|
103 |
*
|
113 |
* @param int $mode One of cache_store::MODE_
|
104 |
* @param int $mode One of store::MODE_
|
114 |
* @return string
|
105 |
* @return string
|
115 |
* @throws coding_exception
|
106 |
* @throws coding_exception
|
116 |
*/
|
107 |
*/
|
117 |
public static function get_class_for_mode($mode) {
|
108 |
public static function get_class_for_mode($mode) {
|
118 |
switch ($mode) {
|
109 |
switch ($mode) {
|
119 |
case cache_store::MODE_APPLICATION :
|
110 |
case store::MODE_APPLICATION:
|
120 |
return 'cache_application';
|
111 |
return application_cache::class;
|
121 |
case cache_store::MODE_REQUEST :
|
112 |
case store::MODE_REQUEST:
|
122 |
return 'cache_request';
|
113 |
return request_cache::class;
|
Línea 123... |
Línea 114... |
123 |
case cache_store::MODE_SESSION :
|
114 |
case store::MODE_SESSION:
|
124 |
return 'cache_session';
|
115 |
return session_cache::class;
|
125 |
}
|
116 |
}
|
126 |
throw new coding_exception('Unknown cache mode passed. Must be one of cache_store::MODE_*');
|
117 |
throw new coding_exception('Unknown cache mode passed. Must be one of store::MODE_*');
|
127 |
}
|
118 |
}
|
128 |
|
119 |
|
129 |
/**
|
120 |
/**
|
130 |
* Returns the cache stores to be used with the given definition.
|
121 |
* Returns the cache stores to be used with the given definition.
|
131 |
* @param cache_definition $definition
|
122 |
* @param definition $definition
|
132 |
* @return array
|
123 |
* @return array
|
133 |
*/
|
124 |
*/
|
Línea 134... |
Línea 125... |
134 |
public static function get_cache_stores(cache_definition $definition) {
|
125 |
public static function get_cache_stores(definition $definition) {
|
135 |
$instance = cache_config::instance();
|
126 |
$instance = config::instance();
|
136 |
$stores = $instance->get_stores_for_definition($definition);
|
127 |
$stores = $instance->get_stores_for_definition($definition);
|
137 |
$stores = self::initialise_cachestore_instances($stores, $definition);
|
128 |
$stores = self::initialise_cachestore_instances($stores, $definition);
|
138 |
return $stores;
|
129 |
return $stores;
|
139 |
}
|
130 |
}
|
140 |
|
131 |
|
141 |
/**
|
132 |
/**
|
142 |
* Internal function for initialising an array of stores against a given cache definition.
|
133 |
* Internal function for initialising an array of stores against a given cache definition.
|
143 |
*
|
134 |
*
|
144 |
* @param array $stores
|
135 |
* @param array $stores
|
145 |
* @param cache_definition $definition
|
136 |
* @param definition $definition
|
146 |
* @return cache_store[]
|
137 |
* @return store[]
|
147 |
*/
|
138 |
*/
|
148 |
protected static function initialise_cachestore_instances(array $stores, cache_definition $definition) {
|
139 |
protected static function initialise_cachestore_instances(array $stores, definition $definition) {
|
149 |
$return = array();
|
140 |
$return = [];
|
150 |
$factory = cache_factory::instance();
|
141 |
$factory = factory::instance();
|
151 |
foreach ($stores as $name => $details) {
|
142 |
foreach ($stores as $name => $details) {
|
Línea 152... |
Línea 143... |
152 |
$store = $factory->create_store_from_config($name, $details, $definition);
|
143 |
$store = $factory->create_store_from_config($name, $details, $definition);
|
153 |
if ($store !== false) {
|
144 |
if ($store !== false) {
|
154 |
$return[] = $store;
|
145 |
$return[] = $store;
|
155 |
}
|
146 |
}
|
156 |
}
|
147 |
}
|
157 |
return $return;
|
148 |
return $return;
|
158 |
}
|
149 |
}
|
159 |
|
150 |
|
160 |
/**
|
151 |
/**
|
161 |
* Returns a cache_lock instance suitable for use with the store.
|
152 |
* Returns a locakable_cache_interface instance suitable for use with the store.
|
162 |
*
|
153 |
*
|
163 |
* @param cache_store $store
|
154 |
* @param store $store
|
Línea 164... |
Línea 155... |
164 |
* @return cache_lock_interface
|
155 |
* @return lockable_cache_interface
|
165 |
*/
|
156 |
*/
|
Línea 178... |
Línea 169... |
178 |
*
|
169 |
*
|
179 |
* @return array
|
170 |
* @return array
|
180 |
*/
|
171 |
*/
|
181 |
public static function early_get_cache_plugins() {
|
172 |
public static function early_get_cache_plugins() {
|
182 |
global $CFG;
|
173 |
global $CFG;
|
183 |
$result = array();
|
174 |
$result = [];
|
184 |
$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db', 'yui', 'tests');
|
175 |
$ignored = ['CVS', '_vti_cnf', 'simpletest', 'db', 'yui', 'tests'];
|
185 |
$fulldir = $CFG->dirroot.'/cache/stores';
|
176 |
$fulldir = $CFG->dirroot . '/cache/stores';
|
186 |
$items = new DirectoryIterator($fulldir);
|
177 |
$items = new DirectoryIterator($fulldir);
|
187 |
foreach ($items as $item) {
|
178 |
foreach ($items as $item) {
|
188 |
if ($item->isDot() or !$item->isDir()) {
|
179 |
if ($item->isDot() or !$item->isDir()) {
|
189 |
continue;
|
180 |
continue;
|
190 |
}
|
181 |
}
|
Línea 194... |
Línea 185... |
194 |
}
|
185 |
}
|
195 |
if (!is_valid_plugin_name($pluginname)) {
|
186 |
if (!is_valid_plugin_name($pluginname)) {
|
196 |
// Better ignore plugins with problematic names here.
|
187 |
// Better ignore plugins with problematic names here.
|
197 |
continue;
|
188 |
continue;
|
198 |
}
|
189 |
}
|
199 |
$result[$pluginname] = $fulldir.'/'.$pluginname;
|
190 |
$result[$pluginname] = $fulldir . '/' . $pluginname;
|
200 |
unset($item);
|
191 |
unset($item);
|
201 |
}
|
192 |
}
|
202 |
unset($items);
|
193 |
unset($items);
|
203 |
return $result;
|
194 |
return $result;
|
204 |
}
|
195 |
}
|
Línea 213... |
Línea 204... |
213 |
* @param array $identifiers
|
204 |
* @param array $identifiers
|
214 |
* @param array|string|int $keys
|
205 |
* @param array|string|int $keys
|
215 |
* @return boolean
|
206 |
* @return boolean
|
216 |
* @throws coding_exception
|
207 |
* @throws coding_exception
|
217 |
*/
|
208 |
*/
|
218 |
public static function invalidate_by_definition($component, $area, array $identifiers = array(), $keys = array()) {
|
209 |
public static function invalidate_by_definition($component, $area, array $identifiers = [], $keys = []) {
|
219 |
$cache = cache::make($component, $area, $identifiers);
|
210 |
$cache = cache::make($component, $area, $identifiers);
|
220 |
if (is_array($keys)) {
|
211 |
if (is_array($keys)) {
|
221 |
$cache->delete_many($keys);
|
212 |
$cache->delete_many($keys);
|
222 |
} else if (is_scalar($keys)) {
|
213 |
} else if (is_scalar($keys)) {
|
223 |
$cache->delete($keys);
|
214 |
$cache->delete($keys);
|
224 |
} else {
|
215 |
} else {
|
225 |
throw new coding_exception('cache_helper::invalidate_by_definition only accepts $keys as array, or scalar.');
|
216 |
throw new coding_exception('helper::invalidate_by_definition only accepts $keys as array, or scalar.');
|
226 |
}
|
217 |
}
|
227 |
return true;
|
218 |
return true;
|
228 |
}
|
219 |
}
|
Línea 229... |
Línea 220... |
229 |
|
220 |
|
Línea 235... |
Línea 226... |
235 |
*
|
226 |
*
|
236 |
* @param string $event
|
227 |
* @param string $event
|
237 |
* @param array $keys
|
228 |
* @param array $keys
|
238 |
*/
|
229 |
*/
|
239 |
public static function invalidate_by_event($event, array $keys) {
|
230 |
public static function invalidate_by_event($event, array $keys) {
|
240 |
$instance = cache_config::instance();
|
231 |
$instance = config::instance();
|
241 |
$invalidationeventset = false;
|
232 |
$invalidationeventset = false;
|
242 |
$factory = cache_factory::instance();
|
233 |
$factory = factory::instance();
|
243 |
$inuse = $factory->get_caches_in_use();
|
234 |
$inuse = $factory->get_caches_in_use();
|
244 |
$purgetoken = null;
|
235 |
$purgetoken = null;
|
245 |
foreach ($instance->get_definitions() as $name => $definitionarr) {
|
236 |
foreach ($instance->get_definitions() as $name => $definitionarr) {
|
246 |
$definition = cache_definition::load($name, $definitionarr);
|
237 |
$definition = definition::load($name, $definitionarr);
|
247 |
if ($definition->invalidates_on_event($event)) {
|
238 |
if ($definition->invalidates_on_event($event)) {
|
248 |
// First up check if there is a cache loader for this definition already.
|
239 |
// First up check if there is a cache loader for this definition already.
|
249 |
// If there is we need to invalidate the keys from there.
|
240 |
// If there is we need to invalidate the keys from there.
|
250 |
$definitionkey = $definition->get_component().'/'.$definition->get_area();
|
241 |
$definitionkey = $definition->get_component() . '/' . $definition->get_area();
|
251 |
if (isset($inuse[$definitionkey])) {
|
242 |
if (isset($inuse[$definitionkey])) {
|
252 |
$inuse[$definitionkey]->delete_many($keys);
|
243 |
$inuse[$definitionkey]->delete_many($keys);
|
253 |
}
|
244 |
}
|
Línea 254... |
Línea 245... |
254 |
|
245 |
|
255 |
// We should only log events for application and session caches.
|
246 |
// We should only log events for application and session caches.
|
256 |
// Request caches shouldn't have events as all data is lost at the end of the request.
|
247 |
// Request caches shouldn't have events as all data is lost at the end of the request.
|
257 |
// Events should only be logged once of course and likely several definitions are watching so we
|
248 |
// Events should only be logged once of course and likely several definitions are watching so we
|
258 |
// track its logging with $invalidationeventset.
|
249 |
// track its logging with $invalidationeventset.
|
Línea 259... |
Línea 250... |
259 |
$logevent = ($invalidationeventset === false && $definition->get_mode() !== cache_store::MODE_REQUEST);
|
250 |
$logevent = ($invalidationeventset === false && $definition->get_mode() !== store::MODE_REQUEST);
|
260 |
|
251 |
|
261 |
if ($logevent) {
|
252 |
if ($logevent) {
|
262 |
// Get the event invalidation cache.
|
253 |
// Get the event invalidation cache.
|
263 |
$cache = cache::make('core', 'eventinvalidation');
|
254 |
$cache = cache::make('core', 'eventinvalidation');
|
264 |
// Get any existing invalidated keys for this cache.
|
255 |
// Get any existing invalidated keys for this cache.
|
265 |
$data = $cache->get($event);
|
256 |
$data = $cache->get($event);
|
266 |
if ($data === false) {
|
257 |
if ($data === false) {
|
267 |
// There are none.
|
258 |
// There are none.
|
268 |
$data = array();
|
259 |
$data = [];
|
269 |
}
|
260 |
}
|
270 |
// Add our keys to them with the current cache timestamp.
|
261 |
// Add our keys to them with the current cache timestamp.
|
271 |
if (null === $purgetoken) {
|
262 |
if (null === $purgetoken) {
|
Línea 289... |
Línea 280... |
289 |
* @param string $component
|
280 |
* @param string $component
|
290 |
* @param string $area
|
281 |
* @param string $area
|
291 |
* @param array $identifiers
|
282 |
* @param array $identifiers
|
292 |
* @return bool
|
283 |
* @return bool
|
293 |
*/
|
284 |
*/
|
294 |
public static function purge_by_definition($component, $area, array $identifiers = array()) {
|
285 |
public static function purge_by_definition($component, $area, array $identifiers = []) {
|
295 |
// Create the cache.
|
286 |
// Create the cache.
|
296 |
$cache = cache::make($component, $area, $identifiers);
|
287 |
$cache = cache::make($component, $area, $identifiers);
|
297 |
// Initialise, in case of a store.
|
288 |
// Initialise, in case of a store.
|
298 |
if ($cache instanceof cache_store) {
|
289 |
if ($cache instanceof store) {
|
299 |
$factory = cache_factory::instance();
|
290 |
$factory = factory::instance();
|
300 |
$definition = $factory->create_definition($component, $area, null);
|
291 |
$definition = $factory->create_definition($component, $area, null);
|
301 |
$cacheddefinition = clone $definition;
|
292 |
$cacheddefinition = clone $definition;
|
302 |
$cacheddefinition->set_identifiers($identifiers);
|
293 |
$cacheddefinition->set_identifiers($identifiers);
|
303 |
$cache->initialise($cacheddefinition);
|
294 |
$cache->initialise($cacheddefinition);
|
304 |
}
|
295 |
}
|
Línea 314... |
Línea 305... |
314 |
* are only supported on caches without identifiers.
|
305 |
* are only supported on caches without identifiers.
|
315 |
*
|
306 |
*
|
316 |
* @param string $event
|
307 |
* @param string $event
|
317 |
*/
|
308 |
*/
|
318 |
public static function purge_by_event($event) {
|
309 |
public static function purge_by_event($event) {
|
319 |
$instance = cache_config::instance();
|
310 |
$instance = config::instance();
|
320 |
$invalidationeventset = false;
|
311 |
$invalidationeventset = false;
|
321 |
$factory = cache_factory::instance();
|
312 |
$factory = factory::instance();
|
322 |
$inuse = $factory->get_caches_in_use();
|
313 |
$inuse = $factory->get_caches_in_use();
|
323 |
$purgetoken = null;
|
314 |
$purgetoken = null;
|
324 |
foreach ($instance->get_definitions() as $name => $definitionarr) {
|
315 |
foreach ($instance->get_definitions() as $name => $definitionarr) {
|
325 |
$definition = cache_definition::load($name, $definitionarr);
|
316 |
$definition = definition::load($name, $definitionarr);
|
326 |
if ($definition->invalidates_on_event($event)) {
|
317 |
if ($definition->invalidates_on_event($event)) {
|
327 |
// First up check if there is a cache loader for this definition already.
|
318 |
// First up check if there is a cache loader for this definition already.
|
328 |
// If there is we need to invalidate the keys from there.
|
319 |
// If there is we need to invalidate the keys from there.
|
329 |
$definitionkey = $definition->get_component().'/'.$definition->get_area();
|
320 |
$definitionkey = $definition->get_component() . '/' . $definition->get_area();
|
330 |
if (isset($inuse[$definitionkey])) {
|
321 |
if (isset($inuse[$definitionkey])) {
|
331 |
$inuse[$definitionkey]->purge();
|
322 |
$inuse[$definitionkey]->purge();
|
332 |
} else {
|
323 |
} else {
|
333 |
cache::make($definition->get_component(), $definition->get_area())->purge();
|
324 |
cache::make($definition->get_component(), $definition->get_area())->purge();
|
334 |
}
|
325 |
}
|
Línea 335... |
Línea 326... |
335 |
|
326 |
|
336 |
// We should only log events for application and session caches.
|
327 |
// We should only log events for application and session caches.
|
337 |
// Request caches shouldn't have events as all data is lost at the end of the request.
|
328 |
// Request caches shouldn't have events as all data is lost at the end of the request.
|
338 |
// Events should only be logged once of course and likely several definitions are watching so we
|
329 |
// Events should only be logged once of course and likely several definitions are watching so we
|
339 |
// track its logging with $invalidationeventset.
|
330 |
// track its logging with $invalidationeventset.
|
Línea 340... |
Línea 331... |
340 |
$logevent = ($invalidationeventset === false && $definition->get_mode() !== cache_store::MODE_REQUEST);
|
331 |
$logevent = ($invalidationeventset === false && $definition->get_mode() !== store::MODE_REQUEST);
|
341 |
|
332 |
|
342 |
// We need to flag the event in the "Event invalidation" cache if it hasn't already happened.
|
333 |
// We need to flag the event in the "Event invalidation" cache if it hasn't already happened.
|
343 |
if ($logevent && $invalidationeventset === false) {
|
334 |
if ($logevent && $invalidationeventset === false) {
|
344 |
// Get the event invalidation cache.
|
335 |
// Get the event invalidation cache.
|
345 |
$cache = cache::make('core', 'eventinvalidation');
|
336 |
$cache = cache::make('core', 'eventinvalidation');
|
346 |
// Create a key to invalidate all.
|
337 |
// Create a key to invalidate all.
|
347 |
if (null === $purgetoken) {
|
338 |
if (null === $purgetoken) {
|
348 |
$purgetoken = cache::get_purge_token(true);
|
339 |
$purgetoken = cache::get_purge_token(true);
|
349 |
}
|
340 |
}
|
350 |
$data = array(
|
341 |
$data = [
|
351 |
'purged' => $purgetoken,
|
342 |
'purged' => $purgetoken,
|
352 |
);
|
343 |
];
|
353 |
// Set that data back to the cache.
|
344 |
// Set that data back to the cache.
|
354 |
$cache->set($event, $data);
|
345 |
$cache->set($event, $data);
|
355 |
// This only needs to occur once.
|
346 |
// This only needs to occur once.
|
Línea 362... |
Línea 353... |
362 |
/**
|
353 |
/**
|
363 |
* Ensure that the stats array is ready to collect information for the given store and definition.
|
354 |
* Ensure that the stats array is ready to collect information for the given store and definition.
|
364 |
* @param string $store
|
355 |
* @param string $store
|
365 |
* @param string $storeclass
|
356 |
* @param string $storeclass
|
366 |
* @param string $definition A string that identifies the definition.
|
357 |
* @param string $definition A string that identifies the definition.
|
367 |
* @param int $mode One of cache_store::MODE_*. Since 2.9.
|
358 |
* @param int $mode One of store::MODE_*. Since 2.9.
|
368 |
*/
|
359 |
*/
|
369 |
protected static function ensure_ready_for_stats($store, $storeclass, $definition, $mode = cache_store::MODE_APPLICATION) {
|
360 |
protected static function ensure_ready_for_stats($store, $storeclass, $definition, $mode = store::MODE_APPLICATION) {
|
370 |
// This function is performance-sensitive, so exit as quickly as possible
|
361 |
// This function is performance-sensitive, so exit as quickly as possible
|
371 |
// if we do not need to do anything.
|
362 |
// if we do not need to do anything.
|
372 |
if (isset(self::$stats[$definition]['stores'][$store])) {
|
363 |
if (isset(self::$stats[$definition]['stores'][$store])) {
|
373 |
return;
|
364 |
return;
|
374 |
}
|
365 |
}
|
Línea 375... |
Línea 366... |
375 |
|
366 |
|
376 |
if (!array_key_exists($definition, self::$stats)) {
|
367 |
if (!array_key_exists($definition, self::$stats)) {
|
377 |
self::$stats[$definition] = array(
|
368 |
self::$stats[$definition] = [
|
378 |
'mode' => $mode,
|
369 |
'mode' => $mode,
|
379 |
'stores' => array(
|
370 |
'stores' => [
|
380 |
$store => array(
|
371 |
$store => [
|
381 |
'class' => $storeclass,
|
372 |
'class' => $storeclass,
|
382 |
'hits' => 0,
|
373 |
'hits' => 0,
|
383 |
'misses' => 0,
|
374 |
'misses' => 0,
|
384 |
'sets' => 0,
|
375 |
'sets' => 0,
|
385 |
'iobytes' => cache_store::IO_BYTES_NOT_SUPPORTED,
|
376 |
'iobytes' => store::IO_BYTES_NOT_SUPPORTED,
|
386 |
'locks' => 0,
|
377 |
'locks' => 0,
|
387 |
)
|
378 |
],
|
388 |
)
|
379 |
],
|
389 |
);
|
380 |
];
|
390 |
} else if (!array_key_exists($store, self::$stats[$definition]['stores'])) {
|
381 |
} else if (!array_key_exists($store, self::$stats[$definition]['stores'])) {
|
391 |
self::$stats[$definition]['stores'][$store] = array(
|
382 |
self::$stats[$definition]['stores'][$store] = [
|
392 |
'class' => $storeclass,
|
383 |
'class' => $storeclass,
|
393 |
'hits' => 0,
|
384 |
'hits' => 0,
|
394 |
'misses' => 0,
|
385 |
'misses' => 0,
|
395 |
'sets' => 0,
|
386 |
'sets' => 0,
|
396 |
'iobytes' => cache_store::IO_BYTES_NOT_SUPPORTED,
|
387 |
'iobytes' => store::IO_BYTES_NOT_SUPPORTED,
|
397 |
'locks' => 0,
|
388 |
'locks' => 0,
|
398 |
);
|
389 |
];
|
399 |
}
|
390 |
}
|
Línea 400... |
Línea 391... |
400 |
}
|
391 |
}
|
401 |
|
392 |
|
402 |
/**
|
393 |
/**
|
403 |
* Returns a string to describe the definition.
|
394 |
* Returns a string to describe the definition.
|
404 |
*
|
395 |
*
|
405 |
* This method supports the definition as a string due to legacy requirements.
|
396 |
* This method supports the definition as a string due to legacy requirements.
|
406 |
* It is backwards compatible when a string is passed but is not accurate.
|
397 |
* It is backwards compatible when a string is passed but is not accurate.
|
407 |
*
|
398 |
*
|
408 |
* @since 2.9
|
399 |
* @since 2.9
|
409 |
* @param cache_definition|string $definition
|
400 |
* @param definition|string $definition
|
410 |
* @return string
|
401 |
* @return string
|
411 |
*/
|
402 |
*/
|
412 |
protected static function get_definition_stat_id_and_mode($definition) {
|
403 |
protected static function get_definition_stat_id_and_mode($definition) {
|
413 |
if (!($definition instanceof cache_definition)) {
|
404 |
if (!($definition instanceof definition)) {
|
414 |
// All core calls to this method have been updated, this is the legacy state.
|
405 |
// All core calls to this method have been updated, this is the legacy state.
|
415 |
// We'll use application as the default as that is the most common, really this is not accurate of course but
|
406 |
// We'll use application as the default as that is the most common, really this is not accurate of course but
|
416 |
// at this point we can only guess and as it only affects calls to cache stat outside of core (of which there should
|
407 |
// at this point we can only guess and as it only affects calls to cache stat outside of core (of which there should
|
417 |
// be none) I think that is fine.
|
408 |
// be none) I think that is fine.
|
418 |
debugging('Please update you cache stat calls to pass the definition rather than just its ID.', DEBUG_DEVELOPER);
|
409 |
debugging('Please update you cache stat calls to pass the definition rather than just its ID.', DEBUG_DEVELOPER);
|
419 |
return array((string)$definition, cache_store::MODE_APPLICATION);
|
410 |
return [(string)$definition, store::MODE_APPLICATION];
|
420 |
}
|
411 |
}
|
Línea 421... |
Línea 412... |
421 |
return array($definition->get_id(), $definition->get_mode());
|
412 |
return [$definition->get_id(), $definition->get_mode()];
|
422 |
}
|
413 |
}
|
423 |
|
414 |
|
424 |
/**
|
415 |
/**
|
425 |
* Record a cache hit in the stats for the given store and definition.
|
416 |
* Record a cache hit in the stats for the given store and definition.
|
426 |
*
|
417 |
*
|
427 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
418 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
428 |
* cache_definition instance. It is preferable to pass a cache definition instance.
|
419 |
* definition instance. It is preferable to pass a cache definition instance.
|
429 |
*
|
420 |
*
|
430 |
* In Moodle 3.9 the first argument changed to also accept a cache_store.
|
421 |
* In Moodle 3.9 the first argument changed to also accept a store.
|
431 |
*
|
422 |
*
|
432 |
* @internal
|
423 |
* @internal
|
433 |
* @param string|cache_store $store
|
424 |
* @param string|store $store
|
434 |
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
425 |
* @param definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
435 |
* actual cache_definition object now.
|
426 |
* actual definition object now.
|
- |
|
427 |
* @param int $hits The number of hits to record (by default 1)
|
- |
|
428 |
* @param int $readbytes Number of bytes read from the cache or store::IO_BYTES_NOT_SUPPORTED
|
- |
|
429 |
*/
|
- |
|
430 |
public static function record_cache_hit(
|
436 |
* @param int $hits The number of hits to record (by default 1)
|
431 |
$store,
|
- |
|
432 |
$definition,
|
437 |
* @param int $readbytes Number of bytes read from the cache or cache_store::IO_BYTES_NOT_SUPPORTED
|
433 |
int $hits = 1,
|
438 |
*/
|
434 |
int $readbytes = store::IO_BYTES_NOT_SUPPORTED,
|
439 |
public static function record_cache_hit($store, $definition, int $hits = 1, int $readbytes = cache_store::IO_BYTES_NOT_SUPPORTED): void {
|
435 |
): void {
|
440 |
$storeclass = '';
|
436 |
$storeclass = '';
|
441 |
if ($store instanceof cache_store) {
|
437 |
if ($store instanceof store) {
|
442 |
$storeclass = get_class($store);
|
438 |
$storeclass = get_class($store);
|
443 |
$store = $store->my_name();
|
439 |
$store = $store->my_name();
|
444 |
}
|
440 |
}
|
445 |
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
|
441 |
[$definitionstr, $mode] = self::get_definition_stat_id_and_mode($definition);
|
446 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
442 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
447 |
self::$stats[$definitionstr]['stores'][$store]['hits'] += $hits;
|
443 |
self::$stats[$definitionstr]['stores'][$store]['hits'] += $hits;
|
448 |
if ($readbytes !== cache_store::IO_BYTES_NOT_SUPPORTED) {
|
444 |
if ($readbytes !== store::IO_BYTES_NOT_SUPPORTED) {
|
449 |
if (self::$stats[$definitionstr]['stores'][$store]['iobytes'] === cache_store::IO_BYTES_NOT_SUPPORTED) {
|
445 |
if (self::$stats[$definitionstr]['stores'][$store]['iobytes'] === store::IO_BYTES_NOT_SUPPORTED) {
|
450 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] = $readbytes;
|
446 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] = $readbytes;
|
451 |
} else {
|
447 |
} else {
|
Línea 456... |
Línea 452... |
456 |
|
452 |
|
457 |
/**
|
453 |
/**
|
458 |
* Record a cache miss in the stats for the given store and definition.
|
454 |
* Record a cache miss in the stats for the given store and definition.
|
459 |
*
|
455 |
*
|
460 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
456 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
461 |
* cache_definition instance. It is preferable to pass a cache definition instance.
|
457 |
* definition instance. It is preferable to pass a cache definition instance.
|
462 |
*
|
458 |
*
|
463 |
* In Moodle 3.9 the first argument changed to also accept a cache_store.
|
459 |
* In Moodle 3.9 the first argument changed to also accept a store.
|
464 |
*
|
460 |
*
|
465 |
* @internal
|
461 |
* @internal
|
466 |
* @param string|cache_store $store
|
462 |
* @param string|store $store
|
467 |
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
463 |
* @param definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
468 |
* actual cache_definition object now.
|
464 |
* actual definition object now.
|
469 |
* @param int $misses The number of misses to record (by default 1)
|
465 |
* @param int $misses The number of misses to record (by default 1)
|
470 |
*/
|
466 |
*/
|
471 |
public static function record_cache_miss($store, $definition, $misses = 1) {
|
467 |
public static function record_cache_miss($store, $definition, $misses = 1) {
|
472 |
$storeclass = '';
|
468 |
$storeclass = '';
|
473 |
if ($store instanceof cache_store) {
|
469 |
if ($store instanceof store) {
|
474 |
$storeclass = get_class($store);
|
470 |
$storeclass = get_class($store);
|
475 |
$store = $store->my_name();
|
471 |
$store = $store->my_name();
|
476 |
}
|
472 |
}
|
477 |
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
|
473 |
[$definitionstr, $mode] = self::get_definition_stat_id_and_mode($definition);
|
478 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
474 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
479 |
self::$stats[$definitionstr]['stores'][$store]['misses'] += $misses;
|
475 |
self::$stats[$definitionstr]['stores'][$store]['misses'] += $misses;
|
Línea 480... |
Línea 476... |
480 |
}
|
476 |
}
|
481 |
|
477 |
|
482 |
/**
|
478 |
/**
|
483 |
* Record a cache set in the stats for the given store and definition.
|
479 |
* Record a cache set in the stats for the given store and definition.
|
484 |
*
|
480 |
*
|
485 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
481 |
* In Moodle 2.9 the $definition argument changed from accepting only a string to accepting a string or a
|
486 |
* cache_definition instance. It is preferable to pass a cache definition instance.
|
482 |
* definition instance. It is preferable to pass a cache definition instance.
|
487 |
*
|
483 |
*
|
488 |
* In Moodle 3.9 the first argument changed to also accept a cache_store.
|
484 |
* In Moodle 3.9 the first argument changed to also accept a store.
|
489 |
*
|
485 |
*
|
490 |
* @internal
|
486 |
* @internal
|
491 |
* @param string|cache_store $store
|
487 |
* @param string|store $store
|
492 |
* @param cache_definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
488 |
* @param definition $definition You used to be able to pass a string here, however that is deprecated please pass the
|
493 |
* actual cache_definition object now.
|
489 |
* actual definition object now.
|
494 |
* @param int $sets The number of sets to record (by default 1)
|
490 |
* @param int $sets The number of sets to record (by default 1)
|
495 |
* @param int $writebytes Number of bytes written to the cache or cache_store::IO_BYTES_NOT_SUPPORTED
|
491 |
* @param int $writebytes Number of bytes written to the cache or store::IO_BYTES_NOT_SUPPORTED
|
- |
|
492 |
*/
|
- |
|
493 |
public static function record_cache_set(
|
- |
|
494 |
$store,
|
496 |
*/
|
495 |
$definition,
|
- |
|
496 |
int $sets = 1,
|
497 |
public static function record_cache_set($store, $definition, int $sets = 1,
|
497 |
int $writebytes = store::IO_BYTES_NOT_SUPPORTED
|
498 |
int $writebytes = cache_store::IO_BYTES_NOT_SUPPORTED) {
|
498 |
) {
|
499 |
$storeclass = '';
|
499 |
$storeclass = '';
|
500 |
if ($store instanceof cache_store) {
|
500 |
if ($store instanceof store) {
|
501 |
$storeclass = get_class($store);
|
501 |
$storeclass = get_class($store);
|
502 |
$store = $store->my_name();
|
502 |
$store = $store->my_name();
|
503 |
}
|
503 |
}
|
504 |
list($definitionstr, $mode) = self::get_definition_stat_id_and_mode($definition);
|
504 |
[$definitionstr, $mode] = self::get_definition_stat_id_and_mode($definition);
|
505 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
505 |
self::ensure_ready_for_stats($store, $storeclass, $definitionstr, $mode);
|
506 |
self::$stats[$definitionstr]['stores'][$store]['sets'] += $sets;
|
506 |
self::$stats[$definitionstr]['stores'][$store]['sets'] += $sets;
|
507 |
if ($writebytes !== cache_store::IO_BYTES_NOT_SUPPORTED) {
|
507 |
if ($writebytes !== store::IO_BYTES_NOT_SUPPORTED) {
|
508 |
if (self::$stats[$definitionstr]['stores'][$store]['iobytes'] === cache_store::IO_BYTES_NOT_SUPPORTED) {
|
508 |
if (self::$stats[$definitionstr]['stores'][$store]['iobytes'] === store::IO_BYTES_NOT_SUPPORTED) {
|
509 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] = $writebytes;
|
509 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] = $writebytes;
|
510 |
} else {
|
510 |
} else {
|
511 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] += $writebytes;
|
511 |
self::$stats[$definitionstr]['stores'][$store]['iobytes'] += $writebytes;
|
Línea 526... |
Línea 526... |
526 |
*
|
526 |
*
|
527 |
* Think twice before calling this method. It will purge **ALL** caches regardless of whether they have been used recently or
|
527 |
* Think twice before calling this method. It will purge **ALL** caches regardless of whether they have been used recently or
|
528 |
* anything. This will involve full setup of the cache + the purge operation. On a site using caching heavily this WILL be
|
528 |
* anything. This will involve full setup of the cache + the purge operation. On a site using caching heavily this WILL be
|
529 |
* painful.
|
529 |
* painful.
|
530 |
*
|
530 |
*
|
531 |
* @param bool $usewriter If set to true the cache_config_writer class is used. This class is special as it avoids
|
531 |
* @param bool $usewriter If set to true the config_writer class is used. This class is special as it avoids
|
532 |
* it is still usable when caches have been disabled.
|
532 |
* it is still usable when caches have been disabled.
|
533 |
* Please use this option only if you really must. It's purpose is to allow the cache to be purged when it would be
|
533 |
* Please use this option only if you really must. It's purpose is to allow the cache to be purged when it would be
|
534 |
* otherwise impossible.
|
534 |
* otherwise impossible.
|
535 |
*/
|
535 |
*/
|
536 |
public static function purge_all($usewriter = false) {
|
536 |
public static function purge_all($usewriter = false) {
|
537 |
$factory = cache_factory::instance();
|
537 |
$factory = factory::instance();
|
538 |
$config = $factory->create_config_instance($usewriter);
|
538 |
$config = $factory->create_config_instance($usewriter);
|
539 |
foreach ($config->get_all_stores() as $store) {
|
539 |
foreach ($config->get_all_stores() as $store) {
|
540 |
self::purge_store($store['name'], $config);
|
540 |
self::purge_store($store['name'], $config);
|
541 |
}
|
541 |
}
|
542 |
foreach ($factory->get_adhoc_caches_in_use() as $cache) {
|
542 |
foreach ($factory->get_adhoc_caches_in_use() as $cache) {
|
Línea 546... |
Línea 546... |
546 |
|
546 |
|
547 |
/**
|
547 |
/**
|
548 |
* Purges a store given its name.
|
548 |
* Purges a store given its name.
|
549 |
*
|
549 |
*
|
550 |
* @param string $storename
|
550 |
* @param string $storename
|
551 |
* @param cache_config $config
|
551 |
* @param config|null $config
|
552 |
* @return bool
|
552 |
* @return bool
|
553 |
*/
|
553 |
*/
|
554 |
public static function purge_store($storename, cache_config $config = null) {
|
554 |
public static function purge_store($storename, ?config $config = null) {
|
555 |
if ($config === null) {
|
555 |
if ($config === null) {
|
556 |
$config = cache_config::instance();
|
556 |
$config = config::instance();
|
Línea 557... |
Línea 557... |
557 |
}
|
557 |
}
|
558 |
|
558 |
|
559 |
$stores = $config->get_all_stores();
|
559 |
$stores = $config->get_all_stores();
|
Línea 569... |
Línea 569... |
569 |
// We check are_requirements_met although we expect is_ready is going to check as well.
|
569 |
// We check are_requirements_met although we expect is_ready is going to check as well.
|
570 |
if (!$class::are_requirements_met()) {
|
570 |
if (!$class::are_requirements_met()) {
|
571 |
return false;
|
571 |
return false;
|
572 |
}
|
572 |
}
|
573 |
// Found the store: is it ready?
|
573 |
// Found the store: is it ready?
|
574 |
/* @var cache_store $instance */
|
574 |
/* @var store $instance */
|
575 |
$instance = new $class($store['name'], $store['configuration']);
|
575 |
$instance = new $class($store['name'], $store['configuration']);
|
576 |
if (!$instance->is_ready()) {
|
576 |
if (!$instance->is_ready()) {
|
577 |
unset($instance);
|
577 |
unset($instance);
|
578 |
return false;
|
578 |
return false;
|
579 |
}
|
579 |
}
|
580 |
foreach ($config->get_definitions_by_store($storename) as $id => $definition) {
|
580 |
foreach ($config->get_definitions_by_store($storename) as $id => $definition) {
|
581 |
$definition = cache_definition::load($id, $definition);
|
581 |
$definition = definition::load($id, $definition);
|
582 |
$definitioninstance = clone($instance);
|
582 |
$definitioninstance = clone($instance);
|
583 |
$definitioninstance->initialise($definition);
|
583 |
$definitioninstance->initialise($definition);
|
584 |
$definitioninstance->purge();
|
584 |
$definitioninstance->purge();
|
585 |
unset($definitioninstance);
|
585 |
unset($definitioninstance);
|
586 |
}
|
586 |
}
|
Línea 589... |
Línea 589... |
589 |
}
|
589 |
}
|
Línea 590... |
Línea 590... |
590 |
|
590 |
|
591 |
/**
|
591 |
/**
|
592 |
* Purges all of the stores used by a definition.
|
592 |
* Purges all of the stores used by a definition.
|
593 |
*
|
593 |
*
|
594 |
* Unlike cache_helper::purge_by_definition this purges all of the data from the stores not
|
594 |
* Unlike helper::purge_by_definition this purges all of the data from the stores not
|
595 |
* just the data relating to the definition.
|
595 |
* just the data relating to the definition.
|
596 |
* This function is useful when you must purge a definition that requires setup but you don't
|
596 |
* This function is useful when you must purge a definition that requires setup but you don't
|
597 |
* want to set it up.
|
597 |
* want to set it up.
|
598 |
*
|
598 |
*
|
599 |
* @param string $component
|
599 |
* @param string $component
|
600 |
* @param string $area
|
600 |
* @param string $area
|
601 |
*/
|
601 |
*/
|
602 |
public static function purge_stores_used_by_definition($component, $area) {
|
602 |
public static function purge_stores_used_by_definition($component, $area) {
|
603 |
$factory = cache_factory::instance();
|
603 |
$factory = factory::instance();
|
604 |
$config = $factory->create_config_instance();
|
604 |
$config = $factory->create_config_instance();
|
605 |
$definition = $factory->create_definition($component, $area);
|
605 |
$definition = $factory->create_definition($component, $area);
|
606 |
$stores = $config->get_stores_for_definition($definition);
|
606 |
$stores = $config->get_stores_for_definition($definition);
|
607 |
foreach ($stores as $store) {
|
607 |
foreach ($stores as $store) {
|
Línea 610... |
Línea 610... |
610 |
}
|
610 |
}
|
Línea 611... |
Línea 611... |
611 |
|
611 |
|
612 |
/**
|
612 |
/**
|
613 |
* Returns the translated name of the definition.
|
613 |
* Returns the translated name of the definition.
|
614 |
*
|
614 |
*
|
615 |
* @param cache_definition $definition
|
615 |
* @param definition $definition
|
616 |
* @return lang_string
|
616 |
* @return lang_string
|
617 |
*/
|
617 |
*/
|
618 |
public static function get_definition_name($definition) {
|
618 |
public static function get_definition_name($definition) {
|
619 |
if ($definition instanceof cache_definition) {
|
619 |
if ($definition instanceof definition) {
|
620 |
return $definition->get_name();
|
620 |
return $definition->get_name();
|
621 |
}
|
621 |
}
|
622 |
$identifier = 'cachedef_'.clean_param($definition['area'], PARAM_STRINGID);
|
622 |
$identifier = 'cachedef_' . clean_param($definition['area'], PARAM_STRINGID);
|
623 |
$component = $definition['component'];
|
623 |
$component = $definition['component'];
|
624 |
if ($component === 'core') {
|
624 |
if ($component === 'core') {
|
625 |
$component = 'cache';
|
625 |
$component = 'cache';
|
626 |
}
|
626 |
}
|
627 |
return new lang_string($identifier, $component);
|
627 |
return new lang_string($identifier, $component);
|
Línea 628... |
Línea 628... |
628 |
}
|
628 |
}
|
629 |
|
629 |
|
630 |
/**
|
630 |
/**
|
631 |
* Hashes a descriptive key to make it shorter and still unique.
|
631 |
* Hashes a descriptive key to make it shorter and still unique.
|
632 |
* @param string|int $key
|
632 |
* @param string|int $key
|
633 |
* @param cache_definition $definition
|
633 |
* @param definition $definition
|
634 |
* @return string
|
634 |
* @return string
|
635 |
*/
|
635 |
*/
|
636 |
public static function hash_key($key, cache_definition $definition) {
|
636 |
public static function hash_key($key, definition $definition) {
|
- |
|
637 |
if ($definition->uses_simple_keys()) {
|
637 |
if ($definition->uses_simple_keys()) {
|
638 |
if (debugging() && preg_match('#[^a-zA-Z0-9_]#', $key ?? '')) {
|
- |
|
639 |
throw new coding_exception(
|
- |
|
640 |
'Cache definition ' . $definition->get_id() . ' requires simple keys. Invalid key provided.',
|
638 |
if (debugging() && preg_match('#[^a-zA-Z0-9_]#', $key ?? '')) {
|
641 |
$key,
|
639 |
throw new coding_exception('Cache definition '.$definition->get_id().' requires simple keys. Invalid key provided.', $key);
|
642 |
);
|
640 |
}
|
643 |
}
|
641 |
// We put the key first so that we can be sure the start of the key changes.
|
644 |
// We put the key first so that we can be sure the start of the key changes.
|
642 |
return (string)$key . '-' . $definition->generate_single_key_prefix();
|
645 |
return (string)$key . '-' . $definition->generate_single_key_prefix();
|
Línea 649... |
Línea 652... |
649 |
* Finds all definitions and updates them within the cache config file.
|
652 |
* Finds all definitions and updates them within the cache config file.
|
650 |
*
|
653 |
*
|
651 |
* @param bool $coreonly If set to true only core definitions will be updated.
|
654 |
* @param bool $coreonly If set to true only core definitions will be updated.
|
652 |
*/
|
655 |
*/
|
653 |
public static function update_definitions($coreonly = false) {
|
656 |
public static function update_definitions($coreonly = false) {
|
654 |
global $CFG;
|
- |
|
655 |
// Include locallib.
|
- |
|
656 |
require_once($CFG->dirroot.'/cache/locallib.php');
|
- |
|
657 |
// First update definitions
|
657 |
// First update definitions
|
658 |
cache_config_writer::update_definitions($coreonly);
|
658 |
config_writer::update_definitions($coreonly);
|
659 |
// Second reset anything we have already initialised to ensure we're all up to date.
|
659 |
// Second reset anything we have already initialised to ensure we're all up to date.
|
660 |
cache_factory::reset();
|
660 |
factory::reset();
|
661 |
}
|
661 |
}
|
Línea 662... |
Línea 662... |
662 |
|
662 |
|
663 |
/**
|
663 |
/**
|
664 |
* Update the site identifier stored by the cache API.
|
664 |
* Update the site identifier stored by the cache API.
|
665 |
*
|
665 |
*
|
666 |
* @param string $siteidentifier
|
666 |
* @param string $siteidentifier
|
667 |
* @return string The new site identifier.
|
667 |
* @return string The new site identifier.
|
668 |
*/
|
668 |
*/
|
669 |
public static function update_site_identifier($siteidentifier) {
|
- |
|
670 |
global $CFG;
|
- |
|
671 |
// Include locallib.
|
- |
|
672 |
require_once($CFG->dirroot.'/cache/locallib.php');
|
669 |
public static function update_site_identifier($siteidentifier) {
|
673 |
$factory = cache_factory::instance();
|
670 |
$factory = factory::instance();
|
674 |
$factory->updating_started();
|
671 |
$factory->updating_started();
|
675 |
$config = $factory->create_config_instance(true);
|
672 |
$config = $factory->create_config_instance(true);
|
676 |
$siteidentifier = $config->update_site_identifier($siteidentifier);
|
673 |
$siteidentifier = $config->update_site_identifier($siteidentifier);
|
677 |
$factory->updating_finished();
|
674 |
$factory->updating_finished();
|
678 |
cache_factory::reset();
|
675 |
factory::reset();
|
679 |
return $siteidentifier;
|
676 |
return $siteidentifier;
|
Línea 680... |
Línea 677... |
680 |
}
|
677 |
}
|
681 |
|
678 |
|
Línea 688... |
Línea 685... |
688 |
global $CFG;
|
685 |
global $CFG;
|
689 |
if (!is_null(self::$siteidentifier)) {
|
686 |
if (!is_null(self::$siteidentifier)) {
|
690 |
return self::$siteidentifier;
|
687 |
return self::$siteidentifier;
|
691 |
}
|
688 |
}
|
692 |
// If site identifier hasn't been collected yet attempt to get it from the cache config.
|
689 |
// If site identifier hasn't been collected yet attempt to get it from the cache config.
|
693 |
$factory = cache_factory::instance();
|
690 |
$factory = factory::instance();
|
694 |
// If the factory is initialising then we don't want to try to get it from the config or we risk
|
691 |
// If the factory is initialising then we don't want to try to get it from the config or we risk
|
695 |
// causing the cache to enter an infinite initialisation loop.
|
692 |
// causing the cache to enter an infinite initialisation loop.
|
696 |
if (!$factory->is_initialising()) {
|
693 |
if (!$factory->is_initialising()) {
|
697 |
$config = $factory->create_config_instance();
|
694 |
$config = $factory->create_config_instance();
|
698 |
self::$siteidentifier = $config->get_site_identifier();
|
695 |
self::$siteidentifier = $config->get_site_identifier();
|
Línea 737... |
Línea 734... |
737 |
public static function clean_old_session_data($output = false) {
|
734 |
public static function clean_old_session_data($output = false) {
|
738 |
global $CFG;
|
735 |
global $CFG;
|
739 |
if ($output) {
|
736 |
if ($output) {
|
740 |
mtrace('Cleaning up stale session data from cache stores.');
|
737 |
mtrace('Cleaning up stale session data from cache stores.');
|
741 |
}
|
738 |
}
|
742 |
$factory = cache_factory::instance();
|
739 |
$factory = factory::instance();
|
743 |
$config = $factory->create_config_instance();
|
740 |
$config = $factory->create_config_instance();
|
744 |
$definitions = $config->get_definitions();
|
741 |
$definitions = $config->get_definitions();
|
745 |
$purgetime = time() - $CFG->sessiontimeout;
|
742 |
$purgetime = time() - $CFG->sessiontimeout;
|
746 |
foreach ($definitions as $definitionarray) {
|
743 |
foreach ($definitions as $definitionarray) {
|
747 |
// We are only interested in session caches.
|
744 |
// We are only interested in session caches.
|
748 |
if (!($definitionarray['mode'] & cache_store::MODE_SESSION)) {
|
745 |
if (!($definitionarray['mode'] & store::MODE_SESSION)) {
|
749 |
continue;
|
746 |
continue;
|
750 |
}
|
747 |
}
|
751 |
$definition = $factory->create_definition($definitionarray['component'], $definitionarray['area']);
|
748 |
$definition = $factory->create_definition($definitionarray['component'], $definitionarray['area']);
|
752 |
$stores = $config->get_stores_for_definition($definition);
|
749 |
$stores = $config->get_stores_for_definition($definition);
|
753 |
// Turn them into store instances.
|
750 |
// Turn them into store instances.
|
754 |
$stores = self::initialise_cachestore_instances($stores, $definition);
|
751 |
$stores = self::initialise_cachestore_instances($stores, $definition);
|
755 |
// Initialise all of the stores used for that definition.
|
752 |
// Initialise all of the stores used for that definition.
|
756 |
foreach ($stores as $store) {
|
753 |
foreach ($stores as $store) {
|
757 |
// If the store doesn't support searching we can skip it.
|
754 |
// If the store doesn't support searching we can skip it.
|
758 |
if (!($store instanceof cache_is_searchable)) {
|
755 |
if (!($store instanceof searchable_cache_interface)) {
|
759 |
debugging('Cache stores used for session definitions should ideally be searchable.', DEBUG_DEVELOPER);
|
756 |
debugging('Cache stores used for session definitions should ideally be searchable.', DEBUG_DEVELOPER);
|
760 |
continue;
|
757 |
continue;
|
761 |
}
|
758 |
}
|
762 |
// Get all of the last access keys.
|
759 |
// Get all of the last access keys.
|
763 |
$keys = $store->find_by_prefix(cache_session::LASTACCESS);
|
760 |
$keys = $store->find_by_prefix(session_cache::LASTACCESS);
|
764 |
$todelete = [];
|
761 |
$todelete = [];
|
765 |
foreach ($store->get_many($keys) as $key => $value) {
|
762 |
foreach ($store->get_many($keys) as $key => $value) {
|
766 |
$expiresvalue = 0;
|
763 |
$expiresvalue = 0;
|
767 |
if ($value instanceof cache_ttl_wrapper) {
|
764 |
if ($value instanceof ttl_wrapper) {
|
768 |
$expiresvalue = $value->data;
|
765 |
$expiresvalue = $value->data;
|
769 |
} else if ($value instanceof cache_cached_object) {
|
766 |
} else if ($value instanceof cached_object) {
|
770 |
$expiresvalue = $value->restore_object();
|
767 |
$expiresvalue = $value->restore_object();
|
771 |
} else {
|
768 |
} else {
|
772 |
$expiresvalue = $value;
|
769 |
$expiresvalue = $value;
|
773 |
}
|
770 |
}
|
774 |
$expires = (int) $expiresvalue;
|
771 |
$expires = (int) $expiresvalue;
|
Línea 775... |
Línea 772... |
775 |
|
772 |
|
776 |
if ($expires > 0 && $expires < $purgetime) {
|
773 |
if ($expires > 0 && $expires < $purgetime) {
|
777 |
$prefix = substr($key, strlen(cache_session::LASTACCESS));
|
774 |
$prefix = substr($key, strlen(session_cache::LASTACCESS));
|
778 |
$foundbyprefix = $store->find_by_prefix($prefix);
|
775 |
$foundbyprefix = $store->find_by_prefix($prefix);
|
779 |
$todelete = array_merge($todelete, [$key], $foundbyprefix);
|
776 |
$todelete = array_merge($todelete, [$key], $foundbyprefix);
|
780 |
}
|
777 |
}
|
781 |
}
|
778 |
}
|
782 |
if ($todelete) {
|
779 |
if ($todelete) {
|
783 |
$outcome = (int)$store->delete_many($todelete);
|
780 |
$outcome = (int) $store->delete_many($todelete);
|
784 |
if ($output) {
|
781 |
if ($output) {
|
785 |
$strdef = s($definition->get_id());
|
782 |
$strdef = s($definition->get_id());
|
786 |
$strstore = s($store->my_name());
|
783 |
$strstore = s($store->my_name());
|
787 |
mtrace("- Removed {$outcome} old {$strdef} sessions from the '{$strstore}' cache store.");
|
784 |
mtrace("- Removed {$outcome} old {$strdef} sessions from the '{$strstore}' cache store.");
|
Línea 797... |
Línea 794... |
797 |
* These stores would be 100% suitable to map as defaults for cache modes.
|
794 |
* These stores would be 100% suitable to map as defaults for cache modes.
|
798 |
*
|
795 |
*
|
799 |
* @return array[] An array of stores, keys are the store names.
|
796 |
* @return array[] An array of stores, keys are the store names.
|
800 |
*/
|
797 |
*/
|
801 |
public static function get_stores_suitable_for_mode_default() {
|
798 |
public static function get_stores_suitable_for_mode_default() {
|
802 |
$factory = cache_factory::instance();
|
799 |
$factory = factory::instance();
|
803 |
$config = $factory->create_config_instance();
|
800 |
$config = $factory->create_config_instance();
|
804 |
$requirements = 0;
|
801 |
$requirements = 0;
|
805 |
foreach ($config->get_definitions() as $definition) {
|
802 |
foreach ($config->get_definitions() as $definition) {
|
806 |
$definition = cache_definition::load($definition['component'].'/'.$definition['area'], $definition);
|
803 |
$definition = definition::load($definition['component'] . '/' . $definition['area'], $definition);
|
807 |
$requirements = $requirements | $definition->get_requirements_bin();
|
804 |
$requirements = $requirements | $definition->get_requirements_bin();
|
808 |
}
|
805 |
}
|
809 |
$stores = array();
|
806 |
$stores = [];
|
810 |
foreach ($config->get_all_stores() as $name => $store) {
|
807 |
foreach ($config->get_all_stores() as $name => $store) {
|
811 |
if (!empty($store['features']) && ($store['features'] & $requirements)) {
|
808 |
if (!empty($store['features']) && ($store['features'] & $requirements)) {
|
812 |
$stores[$name] = $store;
|
809 |
$stores[$name] = $store;
|
813 |
}
|
810 |
}
|
814 |
}
|
811 |
}
|
Línea 816... |
Línea 813... |
816 |
}
|
813 |
}
|
Línea 817... |
Línea 814... |
817 |
|
814 |
|
818 |
/**
|
815 |
/**
|
819 |
* Returns stores suitable for use with a given definition.
|
816 |
* Returns stores suitable for use with a given definition.
|
820 |
*
|
817 |
*
|
821 |
* @param cache_definition $definition
|
818 |
* @param definition $definition
|
822 |
* @return cache_store[]
|
819 |
* @return store[]
|
823 |
*/
|
820 |
*/
|
824 |
public static function get_stores_suitable_for_definition(cache_definition $definition) {
|
821 |
public static function get_stores_suitable_for_definition(definition $definition) {
|
825 |
$factory = cache_factory::instance();
|
822 |
$factory = factory::instance();
|
826 |
$stores = array();
|
823 |
$stores = [];
|
827 |
if ($factory->is_initialising() || $factory->stores_disabled()) {
|
824 |
if ($factory->is_initialising() || $factory->stores_disabled()) {
|
828 |
// No suitable stores here.
|
825 |
// No suitable stores here.
|
829 |
return $stores;
|
826 |
return $stores;
|
830 |
} else {
|
827 |
} else {
|
Línea 854... |
Línea 851... |
854 |
* These get shown on the admin notifications page for example.
|
851 |
* These get shown on the admin notifications page for example.
|
855 |
*
|
852 |
*
|
856 |
* @param array|null $stores An array of stores to get warnings for, or null for all.
|
853 |
* @param array|null $stores An array of stores to get warnings for, or null for all.
|
857 |
* @return string[]
|
854 |
* @return string[]
|
858 |
*/
|
855 |
*/
|
859 |
public static function warnings(array $stores = null) {
|
856 |
public static function warnings(?array $stores = null) {
|
860 |
global $CFG;
|
- |
|
861 |
if ($stores === null) {
|
857 |
if ($stores === null) {
|
862 |
require_once($CFG->dirroot.'/cache/locallib.php');
|
- |
|
863 |
$stores = core_cache\administration_helper::get_store_instance_summaries();
|
858 |
$stores = administration_helper::get_store_instance_summaries();
|
864 |
}
|
859 |
}
|
865 |
$warnings = array();
|
860 |
$warnings = [];
|
866 |
foreach ($stores as $store) {
|
861 |
foreach ($stores as $store) {
|
867 |
if (!empty($store['warnings'])) {
|
862 |
if (!empty($store['warnings'])) {
|
868 |
$warnings = array_merge($warnings, $store['warnings']);
|
863 |
$warnings = array_merge($warnings, $store['warnings']);
|
869 |
}
|
864 |
}
|
870 |
}
|
865 |
}
|
Línea 890... |
Línea 885... |
890 |
*/
|
885 |
*/
|
891 |
public static function is_cluster_available(): bool {
|
886 |
public static function is_cluster_available(): bool {
|
892 |
return class_exists('RedisCluster');
|
887 |
return class_exists('RedisCluster');
|
893 |
}
|
888 |
}
|
894 |
}
|
889 |
}
|
- |
|
890 |
|
- |
|
891 |
// Alias this class to the old name.
|
- |
|
892 |
// This file will be autoloaded by the legacyclasses autoload system.
|
- |
|
893 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
- |
|
894 |
class_alias(helper::class, \cache_helper::class);
|