MDL-82158 core: Update most uses of cache_ classes

This commit is contained in:
Andrew Nicols 2024-06-12 21:10:04 +08:00
parent b92008c92c
commit c21aeeb380
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
48 changed files with 275 additions and 294 deletions

28
cache/README.md vendored
View File

@ -8,7 +8,7 @@ A definition:
$definitions = array(
'string' => array( // Required, unique to the component
'mode' => cache_store::MODE_APPLICATION, // Required
'mode' => \core_cache\store::MODE_APPLICATION, // Required
'simplekeys' => false, // Optional
'simpledata' => false, // Optional
'requireidentifiers' => array( // Optional
@ -38,7 +38,7 @@ A definition:
Getting something from a cache using the definition:
$cache = cache::make('core', 'string');
$cache = \core_cache\cache::make('core', 'string');
if (!$component = $cache->get('component')) {
// get returns false if its not there and can't be loaded.
$component = generate_data();
@ -47,7 +47,7 @@ Getting something from a cache using the definition:
The same thing but using params:
$cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'core', 'string');
$cache = \core_cache\cache::make_from_params(\core_cache\store::MODE_APPLICATION, 'core', 'string');
if (!$component = $cache->get('component')) {
// get returns false if its not there and can't be loaded.
$component = generate_data();
@ -56,7 +56,7 @@ The same thing but using params:
If a data source had been specified in the definition, the following would be all that was needed.
$cache = cache::make('core', 'string');
$cache = \core_cache\cache::make('core', 'string');
$component = $cache->get('component');
Disabling the cache stores.
@ -67,9 +67,9 @@ While the cache API must still be functional in order for calls to it to work it
define('CACHE_DISABLE_STORES', true);
// Disable the cache within your script when you want with:
cache_factory::disable_stores();
\core_cache\factory::disable_stores();
// If you disabled it using the above means you can re-enable it with:
cache_factory::reset();
\core_cache\factory::reset();
Disabling the cache entirely.
Like above there are times when you want the cache to avoid initialising anything it doesn't absolutely need. Things such as installation and upgrade require this functionality.
@ -90,7 +90,7 @@ The loader is central to the whole thing.
It is used by the end developer to get an object that handles caching.
90% of end developers will not need to know or use anything else in the cache API.
In order to get a loader you must use one of two static methods, make or make_from_params.
The loader has been kept as simple as possible, interaction is summarised by the cache_loader interface.
The loader has been kept as simple as possible, interaction is summarised by the core_cache\loader_interface interface.
Internally there is lots of magic going on. The important parts to know about are:
* There are two ways to get a loader, the first with a definition (discussed below) the second with params. When params are used they are turned into an adhoc definition with default params.
* A loader is passed three things when being constructed, a definition, a store, and another loader or datasource if there is either.
@ -106,9 +106,9 @@ The store is the bridge between the cache API and a cache solution.
Cache store plugins exist within moodle/cache/store.
The administrator of a site can configure multiple instances of each plugin, the configuration gets initialised as a store for the loader when required in code (during construction of the loader).
The following points highlight things you should know about stores.
* A cache_store interface is used to define the requirements of a store plugin.
* The store plugin can inherit the cache_is_lockable interface to handle its own locking.
* The store plugin can inherit the cache_is_key_aware interface to handle is own has checks.
* A \core_cache\store interface is used to define the requirements of a store plugin.
* The store plugin can inherit the \core_cache\lockable_cache_interface interface to handle its own locking.
* The store plugin can inherit the \core_cache\key_aware_cache_interface interface to handle is own has checks.
* Store plugins inform the cache API about the things they support. Features can be required by a definition.
* Data guarantee - Data is guaranteed to exist in the cache once it is set there. It is never cleaned up to free space or because it has not been recently used.
* Multiple identifiers - Rather than a single string key, the parts that make up the key are passed as an array.
@ -141,7 +141,7 @@ The following optional settings can also be defined:
* maxsize - This gives a cache an indication about the maximum items it should store. Cache stores don't have to use this, it is up to them to decide if its required.
* overrideclass - If provided this class will be used for the loader. It must extend one of the core loader classes (based upon mode).
* overrideclassfile - Included if required when using the overrideclass param.
* datasource - If provided this class will be used as a data source for the definition. It must implement the cache_data_source interface.
* datasource - If provided this class will be used as a data source for the definition. It must implement the \core_cache\data_source_interface interface.
* datasourcefile - Included if required when using the datasource param.
* staticacceleration - Any data passing through the cache will be held onto to make subsequent requests for it faster.
* staticaccelerationsize - If set to an int this will be the maximum number of items stored in the static acceleration array.
@ -171,7 +171,7 @@ By default all sharing options are available to select. This particular option a
### Data source
Data sources allow cache _misses_ (requests for a key that doesn't exist) to be handled and loaded internally.
The loader gets used as the last resort if provided and means that code using the cache doesn't need to handle the situation that information isn't cached.
They can be specified in a cache definition and must implement the cache_data_source interface.
They can be specified in a cache definition and must implement the \core_cache\data_source_interface interface.
### How it all chains together.
Consider the following:
@ -212,12 +212,12 @@ Other internal magic you should be aware of
The following should fill you in on a bit more of the behind-the-scenes stuff for the cache API.
### Helper class
There is a helper class called cache_helper which is abstract with static methods.
There is a helper class called \core_cache\helper which is abstract with static methods.
This class handles much of the internal generation and initialisation requirements.
In normal use this class will not be needed outside of the API (mostly internal use only)
### Configuration
There are two configuration classes cache_config and cache_config_writer.
There are two configuration classes \core_cache\config and \core_cache\config_writer.
The reader class is used for every request, the writer is only used when modifying the configuration.
Because the cache API is designed to cache database configuration and meta data it must be able to operate prior to database configuration being loaded.
To get around this we store the configuration information in a file in the dataroot.

11
cache/admin.php vendored
View File

@ -19,14 +19,17 @@
*
* This file is part of Moodle's cache API, affectionately called MUC.
*
* @package core_cache
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_cache\factory as cache_factory;
use core_cache\helper as cache_helper;
require_once('../config.php');
require_once($CFG->dirroot . '/lib/adminlib.php');
require_once($CFG->dirroot.'/lib/adminlib.php');
// The first time the user visits this page we are going to reparse the definitions.
// Just ensures that everything is up to date.
@ -42,7 +45,7 @@ $action = optional_param('action', null, PARAM_ALPHA);
admin_externalpage_setup('cacheconfig');
$adminhelper = cache_factory::instance()->get_administration_display_helper();
$notifications = [];
$notifications = array();
// Empty array to hold any form information returned from actions.
$forminfo = [];
@ -57,7 +60,7 @@ if (!empty($action)) {
// Add cache store warnings to the list of notifications.
// Obviously as these are warnings they are show as failures.
foreach (cache_helper::warnings(core_cache\administration_helper::get_store_instance_summaries()) as $warning) {
$notifications[] = [$warning, false];
$notifications[] = array($warning, false);
}
// Decide on display mode based on returned forminfo.

View File

@ -70,9 +70,9 @@ abstract class administration_helper extends helper {
'multipleidentifiers' => $store ? $store->supports_multiple_identifiers() : false,
'dataguarantee' => $store ? $store->supports_data_guarantee() : false,
'nativettl' => $store ? $store->supports_native_ttl() : false,
'nativelocking' => ($store instanceof \cache_is_lockable),
'keyawareness' => ($store instanceof \cache_is_key_aware),
'searchable' => ($store instanceof \cache_is_searchable),
'nativelocking' => ($store instanceof lockable_cache_interface),
'keyawareness' => ($store instanceof key_aware_cache_interface),
'searchable' => ($store instanceof searchable_cache_interface),
],
'warnings' => $store ? $store->get_warnings() : [],
];

View File

@ -19,7 +19,7 @@ namespace core_cache;
/**
* Create and keep an instance of this class to allow temporary caches when caches are disabled.
*
* This class works together with code in {@see cache_factory_disabled}.
* This class works together with code in {@see disabled_factory}.
*
* The intention is that temporary cache should be short-lived (not for the entire install process),
* which avoids two problems: first, that we might run out of memory for the caches, and second,
@ -62,7 +62,7 @@ class allow_temporary_caches {
public function __destruct() {
self::$references--;
if (self::$references === 0) {
\cache_factory_disabled::clear_temporary_caches();
disabled_factory::clear_temporary_caches();
}
}

View File

@ -67,7 +67,7 @@ class application_cache extends cache implements loader_with_locking_interface {
/**
* Gets set to a store to use for locking if the caches primary store doesn't support locking natively.
* @var cache_lock_interface
* @var lockable_cache_interface
*/
protected $cachelockinstance;
@ -110,7 +110,7 @@ class application_cache extends cache implements loader_with_locking_interface {
$this->get_definition()->generate_definition_hash() .
sesskey() .
$instances++ .
'cache_application'
application_cache::class,
);
}
return $this->lockidentifier;

View File

@ -17,9 +17,6 @@
namespace core_cache;
use core_cache\exception\cache_exception;
use cache_factory as factory;
use cache_helper as helper;
use cache_store as store;
/**
* Cache configuration reader.
@ -70,7 +67,7 @@ class config {
protected $siteidentifier = null;
/**
* Please use cache_config::instance to get an instance of the cache config that is ready to be used.
* Please use config::instance to get an instance of the cache config that is ready to be used.
*/
public function __construct() {
// Nothing to do here but look pretty.
@ -218,26 +215,26 @@ class config {
continue;
}
$conf['mode'] = (int)$conf['mode'];
if ($conf['mode'] < cache_store::MODE_APPLICATION || $conf['mode'] > cache_store::MODE_REQUEST) {
if ($conf['mode'] < store::MODE_APPLICATION || $conf['mode'] > store::MODE_REQUEST) {
// Invalid cache mode used for the definition.
continue;
}
if ($conf['mode'] === cache_store::MODE_SESSION || $conf['mode'] === cache_store::MODE_REQUEST) {
if ($conf['mode'] === store::MODE_SESSION || $conf['mode'] === store::MODE_REQUEST) {
// We force this for session and request caches.
// They are only allowed to use the default as we don't want people changing them.
$conf['sharingoptions'] = cache_definition::SHARING_DEFAULT;
$conf['selectedsharingoption'] = cache_definition::SHARING_DEFAULT;
$conf['sharingoptions'] = definition::SHARING_DEFAULT;
$conf['selectedsharingoption'] = definition::SHARING_DEFAULT;
$conf['userinputsharingkey'] = '';
} else {
// Default the sharing option as it was added for 2.5.
// This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
if (!isset($conf['sharingoptions'])) {
$conf['sharingoptions'] = cache_definition::SHARING_DEFAULTOPTIONS;
$conf['sharingoptions'] = definition::SHARING_DEFAULTOPTIONS;
}
// Default the selected sharing option as it was added for 2.5.
// This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
if (!isset($conf['selectedsharingoption'])) {
$conf['selectedsharingoption'] = cache_definition::SHARING_DEFAULT;
$conf['selectedsharingoption'] = definition::SHARING_DEFAULT;
}
// Default the user input sharing key as it was added for 2.5.
// This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
@ -399,7 +396,7 @@ class config {
// It was converted to an instance method but to be backwards compatible
// we must step around this in code.
if (!isset($this)) {
$config = cache_config::instance();
$config = self::instance();
} else {
$config = $this;
}
@ -452,7 +449,7 @@ class config {
/**
* Returns all of the stores that are suitable for the given mode and requirements.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
* @param int $requirements The requirements of the cache as a binary flag
* @return array An array of suitable stores.
*/

View File

@ -27,7 +27,7 @@ use ReflectionClass;
/**
* Cache configuration writer.
*
* This class should only be used when you need to write to the config, all read operations exist within the cache_config.
* This class should only be used when you need to write to the config, all read operations exist within the cache config.
*
* @package core_cache
* @category cache

View File

@ -17,6 +17,7 @@
namespace core_cache;
use moodleform;
use stdClass;
/**
* Cache store feature: configurable.
@ -26,7 +27,7 @@ use moodleform;
* store instance, and then the reverse converting configuration data into an array that can be used to set the
* data for the edit form.
*
* Can be implemented by classes already implementing cache_store.
* Can be implemented by classes already implementing store.
* @package core_cache
* @copyright Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -38,10 +38,10 @@ interface data_source_interface {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @param definition $definition
* @return object
*/
public static function get_instance_for_cache(cache_definition $definition);
public static function get_instance_for_cache(definition $definition);
/**
* Loads the data for the key provided ready formatted for caching.

View File

@ -17,8 +17,6 @@
namespace core_cache;
use core\exception\coding_exception;
use cache_helper as helper;
use cache_store as store;
use lang_string;
/**
@ -54,14 +52,14 @@ use lang_string;
* + overrideclass
* [string] A class to use as the loader for this cache. This is an advanced setting and will allow the developer of the
* definition to take 100% control of the caching solution.
* Any class used here must inherit the cache_loader interface and must extend default cache loader for the mode they are
* Any class used here must inherit the cache loader_interface and must extend default cache loader for the mode they are
* using.
* + overrideclassfile
* [string] Suplements the above setting indicated the file containing the class to be used. This file is included when
* required.
* + datasource
* [string] A class to use as the data loader for this definition.
* Any class used here must inherit the cache_data_loader interface.
* Any class used here must inherit the \core_cache\data_source_interface interface.
* + datasourcefile
* [string] Supplements the above setting indicating the file containing the class to be used. This file is included when
* required.
@ -497,8 +495,8 @@ class definition {
if (!class_exists($datasource)) {
throw new coding_exception('The data source class does not exist.');
}
if (!array_key_exists('cache_data_source', class_implements($datasource))) {
throw new coding_exception('Cache data source classes must implement the cache_data_source interface');
if (!is_a($datasource, data_source_interface::class, true)) {
throw new coding_exception('Cache data source classes must implement the data_source_interface interface');
}
}
@ -816,14 +814,14 @@ class definition {
}
/**
* Please call {@link cache_definition::use_static_acceleration()} instead.
* Please call {@link definition::use_static_acceleration()} instead.
*
* @see definition::use_static_acceleration()
* @deprecated since 2.6
*/
public function should_be_persistent() {
throw new coding_exception('definition::should_be_persistent() can not be used anymore.' .
' Please use cache_definition::use_static_acceleration() instead.');
' Please use definition::use_static_acceleration() instead.');
}
/**
@ -843,14 +841,14 @@ class definition {
}
/**
* Please call {@link cache_definition::get_static_acceleration_size()} instead.
* Please call {@link definition::get_static_acceleration_size()} instead.
*
* @see cache_definition::get_static_acceleration_size()
* @see definition::get_static_acceleration_size()
* @deprecated since 2.6
*/
public function get_persistent_max_size() {
throw new coding_exception('cache_definition::get_persistent_max_size() can not be used anymore.' .
' Please use cache_definition::get_static_acceleration_size() instead.');
throw new coding_exception('definition::get_persistent_max_size() can not be used anymore.' .
' Please use definition::get_static_acceleration_size() instead.');
}
/**

View File

@ -16,11 +16,6 @@
namespace core_cache;
use cache_factory as factory;
use cache_store as store;
use core_cache\definition;
use cache_application as application_cache;
use cache_session as session_cache;
use cachestore_static;
use core\exception\coding_exception;
@ -36,7 +31,7 @@ class disabled_factory extends factory {
protected static $tempcaches = [];
/**
* Returns an instance of the cache_factor method.
* Returns an instance of the factory method.
*
* @param bool $forcereload Unused.
* @return factory
@ -100,8 +95,8 @@ class disabled_factory extends factory {
$store = new cachestore_static('TEMP:' . $component . '/' . $area);
$store->initialise($definition);
// We need to use a cache loader wrapper rather than directly returning the store,
// or it wouldn't have support for versioning. The cache_application class is used
// (rather than cache_request which might make more sense logically) because it
// or it wouldn't have support for versioning. The application_cache class is used
// (rather than request_cache which might make more sense logically) because it
// includes support for locking, which might be necessary for some caches.
$cache = new application_cache($definition, $store);
self::$tempcaches[$key] = $cache;
@ -170,18 +165,18 @@ class disabled_factory extends factory {
* @return disabled_config|config_writer
*/
public function create_config_instance($writer = false) {
// We are always going to use the cache_config_disabled class for all regular request.
// We are always going to use the disabled_config class for all regular request.
// However if the code has requested the writer then likely something is changing and
// we're going to need to interact with the config.php file.
// In this case we will still use the cache_config_writer.
$class = 'cache_config_disabled';
// In this case we will still use the config_writer.
$class = disabled_config::class;
if ($writer) {
// If the writer was requested then something is changing.
$class = 'cache_config_writer';
$class = config_writer::class;
}
if (!array_key_exists($class, $this->configs)) {
self::set_state(factory::STATE_INITIALISING);
if ($class === 'cache_config_disabled') {
if ($class === disabled_config::class) {
$configuration = $class::create_default_configuration();
$this->configs[$class] = new $class();
} else {

View File

@ -17,7 +17,7 @@
namespace core_cache\form;
use core_cache\administration_helper;
use cache_store as store;
use core_cache\store;
use html_writer;
use moodleform;

View File

@ -16,7 +16,7 @@
namespace core_cache\form;
use cache_config;
use core_cache\config as cache_config;
use moodleform;
/**

View File

@ -16,7 +16,7 @@
namespace core_cache\form;
use cache_store as store;
use core_cache\store;
use moodleform;
/**

View File

@ -149,7 +149,7 @@ class helper {
}
/**
* Returns a cache_lock instance suitable for use with the store.
* Returns a locakable_cache_interface instance suitable for use with the store.
*
* @param store $store
* @return lockable_cache_interface

View File

@ -21,10 +21,10 @@ use core\exception\moodle_exception;
/**
* Cache Loader supporting locking.
*
* This interface should be given to classes already implementing cache_loader that also wish to support locking.
* This interface should be given to classes already implementing core_cache\loader_interface that also wish to support locking.
* It outlines the required structure for utilising locking functionality when using a cache.
*
* Can be implemented by any class already implementing the cache_loader interface.
* Can be implemented by any class already implementing the core_cache\loader_interface interface.
* @package core_cache
* @copyright Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -30,20 +30,20 @@
namespace core_cache\local;
use cache_store as store;
use cache_config_writer as config_writer;
use cache_factory as factory;
use cache_helper;
use core_cache\cache;
use core_cache\config;
use core_cache\config_writer;
use core_cache\configurable_cache_interface;
use core_cache\exception\cache_exception;
use core_cache\factory;
use core_cache\form\cache_lock_form;
use core_cache\form\cache_mode_mappings_form;
use core_cache\form\cache_definition_sharing_form;
use core_cache\form\cache_definition_mappings_form;
use core_cache\form\cachestore_addinstance_form;
use core_cache\helper as cache_helper;
use core_cache\lockable_cache_interface;
use core_cache\store;
use core_component;
use core\context;
use core\context\system as context_system;
@ -350,12 +350,14 @@ class administration_display_helper extends \core_cache\administration_helper {
throw new coding_exception('Invalid cache lock plugin requested when trying to create a form.');
}
$plugindir = $plugins[$plugin];
$class = 'cache_lock_form';
if (file_exists($plugindir . '/addinstanceform.php') && in_array(configurable_cache_interface::class, class_implements($class))) {
$class = cache_lock_form::class;
$hasaddinstanceform = file_exists($plugindir . '/addinstanceform.php');
$hasaddinstanceform = $hasaddinstanceform && in_array(configurable_cache_interface::class, class_implements($class));
if ($hasaddinstanceform) {
require_once($plugindir . '/addinstanceform.php');
if (class_exists('cachelock_' . $plugin . '_addinstance_form')) {
$class = 'cachelock_' . $plugin . '_addinstance_form';
if (!array_key_exists(cache_lock_form::class, class_parents($class))) {
if (!is_a($class, cache_lock_form::class, true)) {
throw new coding_exception('Cache lock plugin add instance forms must extend cache_lock_form');
}
}
@ -371,7 +373,7 @@ class administration_display_helper extends \core_cache\administration_helper {
* @return array
* @throws coding_exception
*/
public function get_lock_configuration_from_data(string $plugin, stClass $data): array {
public function get_lock_configuration_from_data(string $plugin, stdClass $data): array {
global $CFG;
$file = $CFG->dirroot . '/cache/locks/' . $plugin . '/lib.php';
if (!file_exists($file)) {

View File

@ -22,7 +22,7 @@ namespace core_cache;
* This is a feature that cache stores can implement if they wish to support locking themselves rather
* than having the cache loader handle it for them.
*
* Can be implemented by classes already implementing cache_store.
* Can be implemented by classes already implementing store.
* @package core_cache
* @copyright Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -16,9 +16,9 @@
namespace core_cache\output;
use cache_factory;
use cache_store;
use context;
use core_cache\factory as cache_factory;
use core_cache\store as cache_store;
use core_collator;
use html_table;
use html_table_cell;
@ -416,7 +416,7 @@ class renderer extends \plugin_renderer_base {
/**
* Creates the two tables which display on the usage page.
*
* @param array $usage Usage information (from cache_helper::usage)
* @param array $usage Usage information (from \core_cache\helper::usage)
* @return array Array of 2 tables (main and summary table)
* @throws \coding_exception
*/

View File

@ -274,7 +274,7 @@ abstract class store implements store_interface {
* @return bool
*/
public function is_searchable() {
return in_array(searchable_cache_interface::class, class_implements($this));
return ($this instanceof searchable_cache_interface);
}
/**

View File

@ -20,7 +20,7 @@ namespace core_cache;
* Cache store interface.
*
* This interface defines the static methods that must be implemented by every cache store plugin.
* To ensure plugins implement this class the abstract cache_store class implements this interface.
* To ensure plugins implement this class the abstract store class implements this interface.
*
* @package core_cache
* @category cache
@ -38,7 +38,7 @@ interface store_interface {
/**
* Static method to check if a store is usable with the given mode.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
*/
public static function is_supported_mode($mode);

View File

@ -26,7 +26,7 @@ namespace core_cache;
* @copyright Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface versionable_data_source_interface extends cache_data_source {
interface versionable_data_source_interface extends data_source_interface {
/**
* Loads the data for the key provided ready formatted for caching.
*

3
cache/forms.php vendored
View File

@ -13,3 +13,6 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// This file is intentionally empty.
// It will be changed to emit debugging in MDL-82836 for Moodle 5.0.

15
cache/locallib.php vendored
View File

@ -1,15 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

View File

@ -14,24 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use core_cache\configurable_cache_interface;
use core_cache\definition;
use core_cache\key_aware_cache_interface;
use core_cache\store;
/**
* APCu cache store main library.
* The APCu cache store class.
*
* @package cachestore_apcu
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* The APCu cache store class.
*
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_apcu extends cache_store implements cache_is_key_aware, cache_is_configurable {
class cachestore_apcu extends store implements configurable_cache_interface, key_aware_cache_interface {
/**
* The required version of APCu for this extension.
*/
@ -45,7 +40,7 @@ class cachestore_apcu extends cache_store implements cache_is_key_aware, cache_i
/**
* The definition used when this instance was initialised.
* @var cache_definition
* @var definition
*/
protected $definition = null;
@ -81,7 +76,7 @@ class cachestore_apcu extends cache_store implements cache_is_key_aware, cache_i
/**
* Static method to check if a store is usable with the given mode.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
* @return bool True if the mode is supported.
*/
public static function is_supported_mode($mode) {
@ -138,10 +133,10 @@ class cachestore_apcu extends cache_store implements cache_is_key_aware, cache_i
*
* This function should prepare any given connections etc.
*
* @param cache_definition $definition
* @param definition $definition
* @return bool
*/
public function initialise(cache_definition $definition) {
public function initialise(definition $definition) {
$this->definition = $definition;
$this->cacheprefix = $this->storeprefix.$definition->generate_definition_hash().'__';
return true;
@ -303,10 +298,10 @@ class cachestore_apcu extends cache_store implements cache_is_key_aware, cache_i
*
* Returns an instance of the cache store, or false if one cannot be created.
*
* @param cache_definition $definition
* @return cache_store
* @param definition $definition
* @return store
*/
public static function initialise_test_instance(cache_definition $definition) {
public static function initialise_test_instance(definition $definition) {
$testperformance = get_config('cachestore_apcu', 'testperformance');
if (empty($testperformance)) {
return false;

View File

@ -16,8 +16,8 @@
namespace cachestore_apcu;
use cache_store;
use cache_definition;
use core_cache\store;
use core_cache\definition;
use cachestore_apcu;
defined('MOODLE_INTERNAL') || die();
@ -54,7 +54,7 @@ class store_test extends \cachestore_tests {
* Test that the Moodle APCu store doesn't cross paths with other code using APCu as well.
*/
public function test_cross_application_interaction(): void {
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
$instance = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
$instance->initialise($definition);
@ -73,11 +73,11 @@ class store_test extends \cachestore_tests {
}
public function test_different_caches_have_different_prefixes(): void {
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
$instance = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
$instance->initialise($definition);
$definition2 = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test2');
$definition2 = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test2');
$instance2 = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
$instance2->initialise($definition2);

View File

@ -14,17 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The library file for the file cache store.
*
* This file is part of the file cache store, it contains the API for interacting with an instance of the store.
* This is used as a default cache store within the Cache API. It should never be deleted.
*
* @package cachestore_file
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_cache\configurable_cache_interface;
use core_cache\definition;
use core_cache\key_aware_cache_interface;
use core_cache\lockable_cache_interface;
use core_cache\searchable_cache_interface;
use core_cache\store;
/**
* The file store class.
@ -34,12 +29,16 @@
* autocreate: true, false
* prescan: true, false
*
* @package cachestore_file
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_file extends cache_store implements cache_is_key_aware, cache_is_configurable, cache_is_searchable,
cache_is_lockable {
class cachestore_file extends store implements
key_aware_cache_interface,
configurable_cache_interface,
searchable_cache_interface,
lockable_cache_interface
{
/**
* The name of the store.
* @var string
@ -105,7 +104,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
/**
* The cache definition this instance has been initialised with.
* @var cache_definition
* @var definition
*/
protected $definition;
@ -150,7 +149,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
* Constructs the store instance.
*
* Noting that this function is not an initialisation. It is used to prepare the store for use.
* The store will be initialised when required and will be provided with a cache_definition at that time.
* The store will be initialised when required and will be provided with a definition at that time.
*
* @param string $name
* @param array $configuration
@ -302,11 +301,11 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
/**
* Returns true if the given mode is supported by this store.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
* @return bool
*/
public static function is_supported_mode($mode) {
return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION);
return ($mode === static::MODE_APPLICATION || $mode === static::MODE_SESSION);
}
/**
@ -314,9 +313,9 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
*
* Once this has been done the cache is all set to be used.
*
* @param cache_definition $definition
* @param definition $definition
*/
public function initialise(cache_definition $definition) {
public function initialise(definition $definition) {
global $CFG;
$this->definition = $definition;
@ -802,10 +801,10 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
*
* Returns an instance of the cache store, or false if one cannot be created.
*
* @param cache_definition $definition
* @param definition $definition
* @return cachestore_file
*/
public static function initialise_test_instance(cache_definition $definition) {
public static function initialise_test_instance(definition $definition) {
$name = 'File test';
$path = make_cache_directory('cachestore_file_test');
$cache = new cachestore_file($name, array('path' => $path));
@ -1009,7 +1008,6 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
* @param string $key Lock identifier
* @param string $ownerid Cache identifier
* @return bool
* @throws cache_exception
*/
public function acquire_lock($key, $ownerid): bool {
$lock = $this->lockfactory->get_lock($key, $this->lockwait);

View File

@ -16,8 +16,8 @@
namespace cachestore_file;
use cache_definition;
use cache_store;
use core_cache\definition;
use core_cache\store;
use cachestore_file;
/**
@ -43,7 +43,7 @@ class asyncpurge_test extends \advanced_testcase {
$this->resetAfterTest(true);
// Cache definition.
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_file', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_file', 'phpunit_test');
// Extra config, set async purge = true.
$extraconfig = ['asyncpurge' => true, 'filecacherev' => time()];

View File

@ -16,8 +16,8 @@
namespace cachestore_file;
use cache_definition;
use cache_store;
use core_cache\definition;
use core_cache\store;
use cachestore_file;
defined('MOODLE_INTERNAL') || die();
@ -54,7 +54,7 @@ class store_test extends \cachestore_tests {
public function test_cache_get_with_prescan_and_purge(): void {
global $CFG;
$definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
$name = 'File test';
$path = make_cache_directory('cachestore_file_test');
@ -78,7 +78,7 @@ class store_test extends \cachestore_tests {
* Tests the get_last_read byte count.
*/
public function test_get_last_io_bytes(): void {
$definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
$store = new \cachestore_file('Test');
$store->initialise($definition);

View File

@ -14,15 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Redis Cache Store - Main library
*
* @package cachestore_redis
* @copyright 2013 Adam Durana
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_cache\configurable_cache_interface;
use core_cache\definition;
use core_cache\key_aware_cache_interface;
use core_cache\lockable_cache_interface;
use core_cache\searchable_cache_interface;
use core_cache\store;
/**
* Redis Cache Store
@ -33,11 +30,16 @@ defined('MOODLE_INTERNAL') || die();
* not to use TTL if at all possible and the benefits of having many stores in Redis using the
* hash configuration, the hash implementation has been used.
*
* @package cachestore_redis
* @copyright 2013 Adam Durana
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_redis extends cache_store implements cache_is_key_aware, cache_is_lockable,
cache_is_configurable, cache_is_searchable {
class cachestore_redis extends store implements
key_aware_cache_interface,
configurable_cache_interface,
searchable_cache_interface,
lockable_cache_interface
{
/**
* Compressor: none.
*/
@ -87,7 +89,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Cache definition for this store.
*
* @var cache_definition
* @var definition
*/
protected $definition = null;
@ -331,10 +333,10 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Initialize the store.
*
* @param cache_definition $definition
* @param definition $definition
* @return bool
*/
public function initialise(cache_definition $definition) {
public function initialise(definition $definition) {
$this->definition = $definition;
$this->hash = $definition->generate_definition_hash();
return true;
@ -553,7 +555,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Determines if the store has a given key.
*
* @see cache_is_key_aware
* @see key_aware_cache_interface
* @param string $key The key to check for.
* @return bool True if the key exists, false if it does not.
*/
@ -564,7 +566,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Determines if the store has any of the keys in a list.
*
* @see cache_is_key_aware
* @see key_aware_cache_interface
* @param array $keys The keys to check for.
* @return bool True if any of the keys are found, false none of the keys are found.
*/
@ -580,7 +582,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Determines if the store has all of the keys in a list.
*
* @see cache_is_key_aware
* @see key_aware_cache_interface
* @param array $keys The keys to check for.
* @return bool True if all of the keys are found, false otherwise.
*/
@ -596,7 +598,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Tries to acquire a lock with a given name.
*
* @see cache_is_lockable
* @see lockable_cache_interface
* @param string $key Name of the lock to acquire.
* @param string $ownerid Information to identify owner of lock if acquired.
* @return bool True if the lock was acquired, false if it was not.
@ -639,7 +641,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Checks a lock with a given name and owner information.
*
* @see cache_is_lockable
* @see lockable_cache_interface
* @param string $key Name of the lock to check.
* @param string $ownerid Owner information to check existing lock against.
* @return mixed True if the lock exists and the owner information matches, null if the lock does not
@ -685,7 +687,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Releases a given lock if the owner information matches.
*
* @see cache_is_lockable
* @see lockable_cache_interface
* @param string $key Name of the lock to release.
* @param string $ownerid Owner information to use.
* @return bool True if the lock is released, false if it is not.
@ -804,7 +806,8 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Creates a configuration array from given 'add instance' form data.
*
* @see cache_is_configurable
* @see configurable_cache_interface
*
* @param stdClass $data
* @return array
*/
@ -824,7 +827,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Sets form data from a configuration array.
*
* @see cache_is_configurable
* @see configurable_cache_interface
* @param moodleform $editform
* @param array $config
*/
@ -855,10 +858,10 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
/**
* Creates an instance of the store for testing.
*
* @param cache_definition $definition
* @param definition $definition
* @return mixed An instance of the store, or false if an instance cannot be created.
*/
public static function initialise_test_instance(cache_definition $definition) {
public static function initialise_test_instance(definition $definition) {
if (!self::are_requirements_met()) {
return false;
}

View File

@ -16,8 +16,9 @@
namespace cachestore_redis;
use cache_definition;
use cache_store;
use core_cache\definition;
use core_cache\helper;
use core_cache\store;
use cachestore_redis;
defined('MOODLE_INTERNAL') || die();
@ -53,8 +54,8 @@ class cachestore_cluster_redis_test extends \advanced_testcase {
public function create_store(?string $seed = null): cachestore_redis {
global $DB;
$definition = cache_definition::load_adhoc(
mode: cache_store::MODE_APPLICATION,
$definition = definition::load_adhoc(
mode: store::MODE_APPLICATION,
component: 'cachestore_redis',
area: 'phpunit_test',
);
@ -91,7 +92,7 @@ class cachestore_cluster_redis_test extends \advanced_testcase {
parent::setUp();
if (!cachestore_redis::are_requirements_met()) {
$this->markTestSkipped('Could not test cachestore_redis with cluster, missing requirements.');
} else if (!\cache_helper::is_cluster_available()) {
} else if (!helper::is_cluster_available()) {
$this->markTestSkipped('Could not test cachestore_redis with cluster, class RedisCluster is not available.');
} else if (!defined('TEST_CACHESTORE_REDIS_SERVERSCLUSTER')) {
$this->markTestSkipped('Could not test cachestore_redis with cluster, missing configuration. ' .

View File

@ -16,9 +16,9 @@
namespace cachestore_redis;
use cache_definition;
use cache_store;
use cachestore_redis;
use core_cache\definition;
use core_cache\store;
defined('MOODLE_INTERNAL') || die();
@ -73,7 +73,7 @@ class cachestore_redis_test extends \cachestore_tests {
* @return cachestore_redis
*/
protected function create_cachestore_redis(): cachestore_redis {
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
$store = new cachestore_redis('Test', cachestore_redis::unit_test_configuration());
$store->initialise($definition);
$this->store = $store;

View File

@ -16,9 +16,9 @@
namespace cachestore_redis;
use cache_definition;
use cache_store;
use cachestore_redis;
use core_cache\definition;
use core_cache\store;
require_once(__DIR__.'/../../../tests/fixtures/stores.php');
require_once(__DIR__.'/../lib.php');
@ -57,8 +57,8 @@ class compressor_test extends \advanced_testcase {
* @return cachestore_redis
*/
public function create_store($compressor, $serializer) {
/** @var cache_definition $definition */
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
/** @var definition $definition */
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
$config = cachestore_redis::unit_test_configuration();
$config['compressor'] = $compressor;
$config['serializer'] = $serializer;

View File

@ -16,8 +16,8 @@
namespace cachestore_redis;
use cache_store;
use cache_definition;
use core_cache\definition;
use core_cache\store;
use cachestore_redis;
defined('MOODLE_INTERNAL') || die();
@ -76,8 +76,8 @@ class store_test extends \cachestore_tests {
*/
protected function create_cachestore_redis(array $extraconfig = [], bool $ttl = false): cachestore_redis {
if ($ttl) {
/** @var cache_definition $definition */
$definition = cache_definition::load('core/wibble', [
/** @var definition $definition */
$definition = definition::load('core/wibble', [
'mode' => 1,
'simplekeys' => true,
'simpledata' => true,
@ -89,8 +89,8 @@ class store_test extends \cachestore_tests {
'sharingoptions' => 15,
]);
} else {
/** @var cache_definition $definition */
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
/** @var definition $definition */
$definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
}
$configuration = array_merge(cachestore_redis::unit_test_configuration(), $extraconfig);
$store = new cachestore_redis('Test', $configuration);
@ -200,9 +200,9 @@ class store_test extends \cachestore_tests {
$store = $this->create_cachestore_redis();
$store->set('foo', [1, 2, 3, 4]);
$this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
$this->assertEquals(store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
$store->get('foo');
$this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
$this->assertEquals(store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
}
/**

View File

@ -16,6 +16,8 @@
namespace cachestore_redis;
use core_cache\definition;
/**
* TTL support test for Redis cache.
*
@ -41,7 +43,7 @@ final class ttl_test extends \advanced_testcase {
}
// Set up a Redis store with a fake definition that has TTL set to 10 seconds.
$definition = \cache_definition::load('core/wibble', [
$definition = definition::load('core/wibble', [
'mode' => 1,
'simplekeys' => true,
'simpledata' => true,

View File

@ -26,6 +26,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_cache\definition;
use core_cache\key_aware_cache_interface;
use core_cache\searchable_cache_interface;
use core_cache\store;
defined('MOODLE_INTERNAL') || die();
/**
@ -34,7 +39,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class session_data_store extends cache_store {
abstract class session_data_store extends store {
/**
* Used for the actual storage.
@ -90,8 +95,7 @@ abstract class session_data_store extends cache_store {
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_session extends session_data_store implements cache_is_key_aware, cache_is_searchable {
class cachestore_session extends session_data_store implements key_aware_cache_interface, searchable_cache_interface {
/**
* The name of the store
* @var store
@ -132,7 +136,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
* Constructs the store instance.
*
* Noting that this function is not an initialisation. It is used to prepare the store for use.
* The store will be initialised when required and will be provided with a cache_definition at that time.
* The store will be initialised when required and will be provided with a definition at that time.
*
* @param string $name
* @param array $configuration
@ -186,11 +190,11 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
/**
* Returns true if the given mode is supported by this store.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
* @return bool
*/
public static function is_supported_mode($mode) {
return ($mode === self::MODE_SESSION);
return ($mode === store::MODE_SESSION);
}
/**
@ -198,9 +202,9 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
*
* Once this has been done the cache is all set to be used.
*
* @param cache_definition $definition
* @param definition $definition
*/
public function initialise(cache_definition $definition) {
public function initialise(definition $definition) {
$this->storeid = $definition->generate_definition_hash();
$this->store = &self::register_store_id($this->name.'-'.$definition->get_id());
$this->ttl = $definition->get_ttl();
@ -506,10 +510,10 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
/**
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @param definition $definition
* @return cachestore_session
*/
public static function initialise_test_instance(cache_definition $definition) {
public static function initialise_test_instance(definition $definition) {
// Do something here perhaps.
$cache = new cachestore_session('Session test');
$cache->initialise($definition);

View File

@ -26,6 +26,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_cache\definition;
use core_cache\store;
defined('MOODLE_INTERNAL') || die();
/**
@ -34,8 +37,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class static_data_store extends cache_store {
abstract class static_data_store extends store {
/**
* An array for storage.
* @var array
@ -134,7 +136,7 @@ class cachestore_static extends static_data_store implements cache_is_key_aware,
* Constructs the store instance.
*
* Noting that this function is not an initialisation. It is used to prepare the store for use.
* The store will be initialised when required and will be provided with a cache_definition at that time.
* The store will be initialised when required and will be provided with a definition at that time.
*
* @param string $name
* @param array $configuration
@ -190,11 +192,11 @@ class cachestore_static extends static_data_store implements cache_is_key_aware,
/**
* Returns true if the given mode is supported by this store.
*
* @param int $mode One of cache_store::MODE_*
* @param int $mode One of store::MODE_*
* @return bool
*/
public static function is_supported_mode($mode) {
return ($mode === self::MODE_REQUEST);
return ($mode === store::MODE_REQUEST);
}
/**
@ -202,9 +204,9 @@ class cachestore_static extends static_data_store implements cache_is_key_aware,
*
* Once this has been done the cache is all set to be used.
*
* @param cache_definition $definition
* @param definition $definition
*/
public function initialise(cache_definition $definition) {
public function initialise(definition $definition) {
$keyarray = $definition->generate_multi_key_parts();
$this->storeid = $keyarray['mode'].'/'.$keyarray['component'].'/'.$keyarray['area'].'/'.$keyarray['siteidentifier'];
$this->store = &self::register_store_id($this->storeid);
@ -525,10 +527,10 @@ class cachestore_static extends static_data_store implements cache_is_key_aware,
/**
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @param definition $definition
* @return cachestore_static
*/
public static function initialise_test_instance(cache_definition $definition) {
public static function initialise_test_instance(definition $definition) {
// Do something here perhaps.
$cache = new cachestore_static('Static store');
$cache->initialise($definition);

View File

@ -16,8 +16,8 @@
namespace cachestore_static;
use cache_definition;
use cache_store;
use core_cache\definition;
use core_cache\store;
use cachestore_static;
defined('MOODLE_INTERNAL') || die();
@ -50,12 +50,12 @@ class store_test extends \cachestore_tests {
$defid = 'phpunit/testmaxsize';
$config = \cache_config_testing::instance();
$config->phpunit_add_definition($defid, array(
'mode' => cache_store::MODE_REQUEST,
'mode' => store::MODE_REQUEST,
'component' => 'phpunit',
'area' => 'testmaxsize',
'maxsize' => 3
));
$definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
$definition = definition::load($defid, $config->get_definition_by_id($defid));
$instance = cachestore_static::initialise_test_instance($definition);
$this->assertTrue($instance->set('key1', 'value1'));
@ -128,11 +128,11 @@ class store_test extends \cachestore_tests {
$defid = 'phpunit/igbinary';
$config = \cache_config_testing::instance();
$config->phpunit_add_definition($defid, array(
'mode' => cache_store::MODE_REQUEST,
'mode' => store::MODE_REQUEST,
'component' => 'phpunit',
'area' => 'testigbinary'
));
$definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
$definition = definition::load($defid, $config->get_definition_by_id($defid));
$instance = cachestore_static::initialise_test_instance($definition);
// Prepare an object.
$obj = new \stdClass();

View File

@ -93,7 +93,6 @@ final class cache_test extends \advanced_testcase {
*/
protected function get_expected_application_store(): string {
global $CFG;
$expected = 'cachestore_file';
$expected = cachestore_file::class;
// Verify if we are using any of the available ways to use a different application store within tests.
@ -1477,7 +1476,7 @@ final class cache_test extends \advanced_testcase {
$factory = factory::instance(true);
$config = $factory->create_config_instance();
$this->assertEquals('cache_config_testing', get_class($config));
$this->assertInstanceOf(cache_config_testing::class, $config);
}
/**
@ -1770,7 +1769,7 @@ final class cache_test extends \advanced_testcase {
// compare, so check it ignoring the time field.
$this->assertEquals(3, $localvalue->version);
$ttldata = $localvalue->data;
$this->assertInstanceOf('cache_ttl_wrapper', $ttldata);
$this->assertInstanceOf(ttl_wrapper::class, $ttldata);
$this->assertEquals('British Bulldog', $ttldata->data);
} else {
$this->assertEquals(new \core_cache\version_wrapper('British Bulldog', 3), $localvalue);

2
cache/usage.php vendored
View File

@ -27,7 +27,7 @@ require_once('../config.php');
require_once($CFG->dirroot . '/lib/adminlib.php');
admin_externalpage_setup('cacheusage');
$adminhelper = cache_factory::instance()->get_administration_display_helper();
$adminhelper = \core_cache\factory::instance()->get_administration_display_helper();
raise_memory_limit(MEMORY_EXTRA);
$samples = optional_param('samples', 50, PARAM_INT);

View File

@ -16,8 +16,8 @@
namespace core_course\cache;
use cache_data_source;
use cache_definition;
use core_cache\data_source_interface;
use core_cache\definition;
use moodle_url;
use core_course_list_element;
@ -30,7 +30,7 @@ use core_course_list_element;
* @copyright 2021 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_image implements cache_data_source {
class course_image implements data_source_interface {
/** @var course_image */
protected static $instance = null;
@ -39,10 +39,10 @@ class course_image implements cache_data_source {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @param definition $definition
* @return \core_course\cache\course_image
*/
public static function get_instance_for_cache(cache_definition $definition): course_image {
public static function get_instance_for_cache(definition $definition): course_image {
if (is_null(self::$instance)) {
self::$instance = new course_image();
}

View File

@ -19,7 +19,7 @@ namespace core_course;
use context_user;
use context_course;
use ReflectionMethod;
use cache_definition;
use core_cache\definition;
use core_course\cache\course_image;
/**
@ -97,7 +97,7 @@ class course_image_cache_test extends \advanced_testcase {
*/
public function test_get_image_url_from_overview_files_return_null_if_no_summary_files_in_the_course(): void {
$method = new ReflectionMethod(course_image::class, 'get_image_url_from_overview_files');
$cache = course_image::get_instance_for_cache(new cache_definition());
$cache = course_image::get_instance_for_cache(new definition());
// Create course without files.
$course = $this->getDataGenerator()->create_course();
@ -109,7 +109,7 @@ class course_image_cache_test extends \advanced_testcase {
*/
public function test_get_image_url_from_overview_files_returns_null_if_no_summary_images_in_the_course(): void {
$method = new ReflectionMethod(course_image::class, 'get_image_url_from_overview_files');
$cache = course_image::get_instance_for_cache(new cache_definition());
$cache = course_image::get_instance_for_cache(new definition());
// Create course without image files.
$draftid2 = $this->fill_draft_area(['filename2.zip' => 'Test file contents2']);
@ -122,7 +122,7 @@ class course_image_cache_test extends \advanced_testcase {
*/
public function test_get_image_url_from_overview_files_returns_url_if_there_is_a_summary_image(): void {
$method = new ReflectionMethod(course_image::class, 'get_image_url_from_overview_files');
$cache = course_image::get_instance_for_cache(new cache_definition());
$cache = course_image::get_instance_for_cache(new definition());
// Create course without one image.
$draftid1 = $this->fill_draft_area(['filename1.jpg' => file_get_contents(__DIR__ . '/fixtures/image.jpg')]);
@ -136,7 +136,7 @@ class course_image_cache_test extends \advanced_testcase {
*/
public function test_get_image_url_from_overview_files_returns_url_of_the_first_image_if_there_are_many_summary_images(): void {
$method = new ReflectionMethod(course_image::class, 'get_image_url_from_overview_files');
$cache = course_image::get_instance_for_cache(new cache_definition());
$cache = course_image::get_instance_for_cache(new definition());
// Create course with two image files.
$draftid1 = $this->fill_draft_area([
@ -154,7 +154,7 @@ class course_image_cache_test extends \advanced_testcase {
*/
public function test_get_image_url_from_overview_files_returns_url_of_the_first_image_if_there_are_many_summary_files(): void {
$method = new ReflectionMethod(course_image::class, 'get_image_url_from_overview_files');
$cache = course_image::get_instance_for_cache(new cache_definition());
$cache = course_image::get_instance_for_cache(new definition());
// Create course with two image files and one zip file.
$draftid1 = $this->fill_draft_area([

View File

@ -14,18 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cache data source for the time of the last message between users.
*
* @package core_message
* @category cache
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message;
defined('MOODLE_INTERNAL') || die();
use core_cache\data_source_interface;
use core_cache\definition;
/**
* Cache data source for the time of the last message in a conversation.
@ -35,19 +27,19 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class time_last_message_between_users implements \cache_data_source {
class time_last_message_between_users implements data_source_interface {
/** @var time_last_message_between_users the singleton instance of this class. */
protected static $instance = null;
/**
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by the cache_data_source interface.
* specified by the data_source_interface interface.
*
* @param \cache_definition $definition
* @param definition $definition
* @return object
*/
public static function get_instance_for_cache(\cache_definition $definition) {
public static function get_instance_for_cache(definition $definition) {
if (is_null(self::$instance)) {
self::$instance = new time_last_message_between_users();
}

View File

@ -26,7 +26,8 @@ declare(strict_types=1);
namespace mod_assign\cache;
use cache_definition;
use core_cache\data_source_interface;
use core_cache\definition;
/**
* Class assign_overrides
@ -35,7 +36,7 @@ use cache_definition;
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class overrides implements \cache_data_source {
class overrides implements data_source_interface {
/** @var overrides the singleton instance of this class. */
protected static $instance = null;
@ -44,10 +45,10 @@ class overrides implements \cache_data_source {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @param definition $definition
* @return object
*/
public static function get_instance_for_cache(cache_definition $definition): overrides {
public static function get_instance_for_cache(definition $definition): overrides {
if (is_null(self::$instance)) {
self::$instance = new overrides();
}

View File

@ -14,6 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace mod_lesson\cache;
use core_cache\data_source_interface;
use core_cache\definition;
/**
* Cache data source for the lesson overrides.
*
@ -21,21 +28,7 @@
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace mod_lesson\cache;
use cache_definition;
/**
* Class lesson_overrides
*
* @package mod_lesson
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class overrides implements \cache_data_source {
class overrides implements data_source_interface {
/** @var overrides the singleton instance of this class. */
protected static $instance = null;
@ -44,10 +37,10 @@ class overrides implements \cache_data_source {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @param definition $definition
* @return object
*/
public static function get_instance_for_cache(cache_definition $definition): overrides {
public static function get_instance_for_cache(definition $definition): overrides {
if (is_null(self::$instance)) {
self::$instance = new overrides();
}

View File

@ -26,7 +26,8 @@ declare(strict_types=1);
namespace mod_quiz\cache;
use cache_definition;
use core_cache\data_source_interface;
use core_cache\definition;
/**
* Class quiz_overrides
@ -35,8 +36,7 @@ use cache_definition;
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class overrides implements \cache_data_source {
class overrides implements data_source_interface {
/** @var overrides the singleton instance of this class. */
protected static $instance = null;
@ -44,10 +44,10 @@ class overrides implements \cache_data_source {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @return stdClass
* @param definition $definition
* @return overrides
*/
public static function get_instance_for_cache(cache_definition $definition): overrides {
public static function get_instance_for_cache(definition $definition): overrides {
if (is_null(self::$instance)) {
self::$instance = new overrides();
}
@ -64,7 +64,7 @@ class overrides implements \cache_data_source {
public function load_for_cache($key) {
global $DB;
// Ignore getting data if this is a cache invalidation - {@see \cache_helper::purge_by_event()}.
// Ignore getting data if this is a cache invalidation - {@see \core_cache\helper::purge_by_event()}.
if ($key == 'lastinvalidation') {
return null;
}

View File

@ -15,8 +15,11 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace mod_scorm\cache;
use cache_definition;
use core_cache\data_source_interface;
use core_cache\definition;
/**
* Cache data source for the scorm elements.
@ -26,7 +29,7 @@ use cache_definition;
* @author Dan Marsden <dan@danmarsden.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class elements implements \cache_data_source {
class elements implements data_source_interface {
/** @var elements the singleton instance of this class. */
protected static $instance = null;
@ -35,10 +38,10 @@ class elements implements \cache_data_source {
* Returns an instance of the data source class that the cache can use for loading data using the other methods
* specified by this interface.
*
* @param cache_definition $definition
* @param definition $definition
* @return object
*/
public static function get_instance_for_cache(cache_definition $definition): elements {
public static function get_instance_for_cache(definition $definition): elements {
if (is_null(self::$instance)) {
self::$instance = new elements();
}

View File

@ -27,9 +27,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_cache\application_cache;
use core_cache\data_source_interface;
use core_cache\definition;
use core_question\local\bank\question_version_status;
use core_question\output\question_version_info;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../type/questiontypebase.php');
@ -486,7 +490,7 @@ abstract class question_bank {
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_finder implements cache_data_source {
class question_finder implements data_source_interface {
/** @var question_finder the singleton instance of this class. */
protected static $questionfinder = null;
@ -500,13 +504,13 @@ class question_finder implements cache_data_source {
return self::$questionfinder;
}
/* See cache_data_source::get_instance_for_cache. */
public static function get_instance_for_cache(cache_definition $definition) {
#[\Override]
public static function get_instance_for_cache(definition $definition) {
return self::get_instance();
}
/**
* @return cache_application the question definition cache we are using.
* @return application_cache the question definition cache we are using.
*/
protected function get_data_cache() {
// Do not double cache here because it may break cache resetting.
@ -672,7 +676,7 @@ class question_finder implements cache_data_source {
$qubaids->from_where_params() + $params + $extraparams);
}
/* See cache_data_source::load_for_cache. */
#[\Override]
public function load_for_cache($questionid) {
global $DB;
@ -696,7 +700,7 @@ class question_finder implements cache_data_source {
return $questiondata;
}
/* See cache_data_source::load_many_for_cache. */
#[\Override]
public function load_many_for_cache(array $questionids) {
global $DB;