Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
|
Public Member Functions | |
__construct ( $name, array $configuration=[],) | |
Constructs an instance of this type of store. | |
acquire_lock ($key, $ownerid) | |
Tries to acquire a lock with a given name. | |
cache_size_details (int $samplekeys=50) | |
Gets the amount of memory used by this specific cache within the store, if known. | |
check_lock_state ($key, $ownerid) | |
Checks a lock with a given name and owner information. | |
cleanup () | |
create_clone (array $details=array()) | |
Creates a clone of this store instance ready to be initialised. | |
delete ($key) | |
Delete the given key. | |
delete_many (array $keys) | |
Delete many keys. | |
estimate_stored_size ($key, $value) | |
Estimates the stored size, taking into account whether compression is turned on. | |
expire_ttl () | |
Runs TTL expiry process for this cache. | |
find_all () | |
Finds all of the keys being used by this cache store instance. | |
find_by_prefix ($prefix) | |
Finds all of the keys whose keys start with the given prefix. | |
get ($key) | |
Get the value associated with a given key. | |
get_last_io_bytes () | |
Gets the number of bytes read from or written to cache as a result of the last action. | |
get_many ($keys) | |
Get the values associated with a list of keys. | |
get_warnings () | |
Can be overridden to return any warnings this store instance should make to the admin. | |
has ($key) | |
Determines if the store has a given key. | |
has_all (array $keys) | |
Determines if the store has all of the keys in a list. | |
has_any (array $keys) | |
Determines if the store has any of the keys in a list. | |
initialise (cache_definition $definition) | |
Initialize the store. | |
instance_created () | |
Performs any necessary operation when the store instance has been created. | |
instance_deleted () | |
Cleans up after an instance of the store. | |
is_initialised () | |
Determine if the store is initialized. | |
is_ready () | |
Determine if the store is ready for use. | |
is_searchable () | |
Returns true if the store instance is searchable. | |
my_name () | |
Get the name of the store. | |
purge () | |
Purges all keys from the store. | |
release_lock ($key, $ownerid) | |
Releases a given lock if the owner information matches. | |
set ($key, $value) | |
Set the value of a key. | |
set_many (array $keyvaluearray) | |
Set the values of many keys. | |
shutdown_release_locks () | |
Releases any locks when the system shuts down, in case there is a crash or somebody forgets to use 'try-finally'. | |
store_total_size () | |
Gets Redis reported memory usage. | |
supports_data_guarantee () | |
Returns true if the store instance guarantees data. | |
supports_dereferencing_objects () | |
Returns true if the store automatically dereferences objects. | |
supports_multiple_identifiers () | |
Returns true if the store instance supports multiple identifiers. | |
supports_native_ttl () | |
Returns true if the store instance supports native ttl. | |
Static Public Member Functions | |
static | are_requirements_met () |
Determines if the requirements for this type of store are met. | |
static | can_add_instance () |
Returns true if the user can add an instance of the store plugin. | |
static | config_get_compressor_options () |
Gets an array of options to use as the compressor. | |
static | config_get_configuration_array ($data) |
Creates a configuration array from given 'add instance' form data. | |
static | config_get_serializer_options () |
Gets an array of options to use as the serialiser. | |
static | config_set_edit_form_data (moodleform $editform, array $config) |
Sets form data from a configuration array. | |
static | get_supported_features (array $configuration=array()) |
Get the features of this type of cache store. | |
static | get_supported_modes (array $configuration=array()) |
Get the supported modes of this type of cache store. | |
static | initialise_test_instance (cache_definition $definition) |
Creates an instance of the store for testing. | |
static | is_supported_mode ($mode) |
Determines if this type of store supports a given mode. | |
static | ready_to_be_used_for_testing () |
Returns true if this cache store instance is both suitable for testing, and ready for testing. | |
static | set_phpunit_time (int $time=0) |
Sets the current time (within unit test) for TTL functionality. | |
static | unit_test_configuration () |
Return configuration to use when unit testing. | |
Public Attributes | |
const | COMPRESSOR_NONE = 0 |
Compressor: none. | |
const | COMPRESSOR_PHP_GZIP = 1 |
Compressor: PHP GZip. | |
const | COMPRESSOR_PHP_ZSTD = 2 |
Compressor: PHP Zstandard. | |
const | DEREFERENCES_OBJECTS = 16 |
The cache store dereferences objects. | |
const | IO_BYTES_NOT_SUPPORTED = -1 |
Returned from get_last_io_bytes if this cache store doesn't support counting bytes read/sent. | |
const | IS_SEARCHABLE = 8 |
The cache is searchable by key. | |
const | MODE_APPLICATION = 1 |
Application caches. | |
const | MODE_REQUEST = 4 |
Request caches. | |
const | MODE_SESSION = 2 |
Session caches. | |
const | STATIC_ACCEL = '** static accel. **' |
Static caches. | |
const | SUPPORTS_DATA_GUARANTEE = 2 |
Ensures data remains in the cache once set. | |
const | SUPPORTS_MULTIPLE_IDENTIFIERS = 1 |
Supports multi-part keys. | |
const | SUPPORTS_NATIVE_TTL = 4 |
Supports a native ttl system. | |
int const | TTL_EXPIRE_BATCH = 10000 |
Number of items to delete from cache in one batch when expiring old TTL data. | |
string const | TTL_SUFFIX = '_ttl' |
Suffix used on key name (for hash) to store the TTL sorted list. | |
Protected Member Functions | |
new_redis (array $configuration) | |
Create a new Redis or RedisCluster instance and connect to the server. | |
ping (RedisCluster|Redis $redis) | |
See if we can ping Redis server. | |
Static Protected Member Functions | |
static | get_time () |
Gets the current time for TTL functionality. | |
Protected Attributes | |
int | $compressor = self::COMPRESSOR_NONE |
Compressor for this store. | |
array | $currentlocks = null |
Array of current locks, or null if we haven't registered shutdown function. | |
cache_definition | $definition = null |
Cache definition for this store. | |
string | $hash |
The definition hash, used for hash key. | |
boolean | $isready = false |
Flag for readiness! | |
int | $lastiobytes = 0 |
Bytes read or written by last call to set()/get() or set_many()/get_many(). | |
int | $locktimeout = 600 |
Timeout before lock is automatically released (in case of crashes) | |
int | $lockwait = 60 |
Maximum number of seconds to wait for a lock before giving up. | |
string | $name |
Name of this store. | |
Redis RedisCluster | $redis |
Connection to Redis for this store. | |
int | $serializer = Redis::SERIALIZER_PHP |
Serializer for this store. | |
To allow separation of definitions in Moodle and faster purging, each cache is implemented as a Redis hash. That is a trade-off between having functionality of TTL and being able to manage many caches in a single redis instance. Given the recommendation 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.
cachestore_redis::__construct | ( | $name, | |
array | $configuration = [] ) |
Constructs an instance of this type of store.
string | $name | |
array | $configuration |
Reimplemented from cache_store.
cachestore_redis::acquire_lock | ( | $key, | |
$ownerid ) |
Tries to acquire a lock with a given name.
string | $key | Name of the lock to acquire. |
string | $ownerid | Information to identify owner of lock if acquired. |
bool | True if the lock was acquired, false if it was not. |
Implements cache_is_lockable.
|
static |
Determines if the requirements for this type of store are met.
bool |
Implements cache_store_interface.
|
inherited |
Gets the amount of memory used by this specific cache within the store, if known.
This function may be slow and should not be called in normal usage, only for administration pages. The value is usually an estimate, and may not be available at all.
When estimating, a number of sample items will be used for the estimate. If set to 50 (default), then this function will retrieve 50 random items and use that to estimate the total size.
The return value has the following fields:
int | $samplekeys | Number of samples to use |
stdClass | Object with information about the store size |
Reimplemented in cachestore_file.
|
staticinherited |
Returns true if the user can add an instance of the store plugin.
bool |
Reimplemented in cachestore_dummy, cachestore_session, and cachestore_static.
cachestore_redis::check_lock_state | ( | $key, | |
$ownerid ) |
Checks a lock with a given name and owner information.
string | $key | Name of the lock to check. |
string | $ownerid | Owner information to check existing lock against. |
mixed | True if the lock exists and the owner information matches, null if the lock does not exist, and false otherwise. |
Implements cache_is_lockable.
|
inherited |
|
static |
Gets an array of options to use as the compressor.
array |
|
static |
Creates a configuration array from given 'add instance' form data.
stdClass | $data |
array |
Implements cache_is_configurable.
|
static |
Gets an array of options to use as the serialiser.
array |
|
static |
Sets form data from a configuration array.
moodleform | $editform | |
array | $config |
Implements cache_is_configurable.
|
inherited |
Creates a clone of this store instance ready to be initialised.
This method is used so that a cache store needs only be constructed once. Future requests for an instance of the store will be given a cloned instance.
If you are writing a cache store that isn't compatible with the clone operation you can override this method to handle any situations you want before cloning.
array | $details | An array containing the details of the store from the cache config. |
cache_store |
cachestore_redis::delete | ( | $key | ) |
Delete the given key.
string | $key | The key to delete. |
bool | True if the delete operation succeeds, false otherwise. |
Reimplemented from cache_store.
cachestore_redis::delete_many | ( | array | $keys | ) |
Delete many keys.
array | $keys | The keys to delete. |
int | The number of keys successfully deleted. |
Reimplemented from cache_store.
cachestore_redis::estimate_stored_size | ( | $key, | |
$value ) |
Estimates the stored size, taking into account whether compression is turned on.
mixed | $key | Key name |
mixed | $value | Value |
int | Approximate stored size |
Reimplemented from cache_store.
cachestore_redis::expire_ttl | ( | ) |
Runs TTL expiry process for this cache.
This is not part of the standard cache API and is intended for use by the scheduled task cachestore_redis\ttl.
array | Various keys with information about how the expiry went |
cachestore_redis::find_all | ( | ) |
Finds all of the keys being used by this cache store instance.
array | of all keys in the hash as a numbered array. |
Implements cache_is_searchable.
cachestore_redis::find_by_prefix | ( | $prefix | ) |
Finds all of the keys whose keys start with the given prefix.
string | $prefix |
array | List of keys that match this prefix. |
Implements cache_is_searchable.
cachestore_redis::get | ( | $key | ) |
Get the value associated with a given key.
string | $key | The key to get the value of. |
mixed | The value of the key, or false if there is no value associated with the key. |
Reimplemented from cache_store.
cachestore_redis::get_last_io_bytes | ( | ) |
Gets the number of bytes read from or written to cache as a result of the last action.
If compression is not enabled, this function always returns IO_BYTES_NOT_SUPPORTED. The reason is that when compression is not enabled, data sent to the cache is not serialized, and we would need to serialize it to compute the size, which would have a significant performance cost.
int | Bytes read or written |
Reimplemented from cache_store.
cachestore_redis::get_many | ( | $keys | ) |
Get the values associated with a list of keys.
array | $keys | The keys to get the values of. |
array | An array of the values of the given keys. |
Reimplemented from cache_store.
|
static |
Get the features of this type of cache store.
array | $configuration |
int |
Implements cache_store_interface.
|
static |
Get the supported modes of this type of cache store.
array | $configuration |
int |
Implements cache_store_interface.
|
staticprotected |
Gets the current time for TTL functionality.
This wrapper makes it easier to unit-test the TTL behaviour.
int | Current time |
|
inherited |
Can be overridden to return any warnings this store instance should make to the admin.
This should be used to notify things like configuration conflicts etc. The warnings returned here will be displayed on the cache configuration screen.
string[] | An array of warning strings from the store instance. |
cachestore_redis::has | ( | $key | ) |
Determines if the store has a given key.
string | $key | The key to check for. |
bool | True if the key exists, false if it does not. |
Implements cache_is_key_aware.
cachestore_redis::has_all | ( | array | $keys | ) |
Determines if the store has all of the keys in a list.
array | $keys | The keys to check for. |
bool | True if all of the keys are found, false otherwise. |
Implements cache_is_key_aware.
cachestore_redis::has_any | ( | array | $keys | ) |
Determines if the store has any of the keys in a list.
array | $keys | The keys to check for. |
bool | True if any of the keys are found, false none of the keys are found. |
Implements cache_is_key_aware.
cachestore_redis::initialise | ( | cache_definition | $definition | ) |
Initialize the store.
cache_definition | $definition |
bool |
Reimplemented from cache_store.
|
static |
Creates an instance of the store for testing.
cache_definition | $definition |
mixed | An instance of the store, or false if an instance cannot be created. |
Implements cache_store_interface.
|
inherited |
Performs any necessary operation when the store instance has been created.
Reimplemented in cachestore_file.
cachestore_redis::instance_deleted | ( | ) |
Cleans up after an instance of the store.
Reimplemented from cache_store.
cachestore_redis::is_initialised | ( | ) |
cachestore_redis::is_ready | ( | ) |
|
inherited |
Returns true if the store instance is searchable.
bool |
|
static |
Determines if this type of store supports a given mode.
int | $mode |
bool |
Implements cache_store_interface.
cachestore_redis::my_name | ( | ) |
|
protected |
Create a new Redis or RedisCluster instance and connect to the server.
array | $configuration | The redis instance configuration. |
Redis|RedisCluster|null |
|
protected |
cachestore_redis::purge | ( | ) |
|
static |
Returns true if this cache store instance is both suitable for testing, and ready for testing.
When TEST_CACHESTORE_REDIS_TESTSERVERS is set, then we are ready to be use d for testing.
bool |
Reimplemented from cache_store.
cachestore_redis::release_lock | ( | $key, | |
$ownerid ) |
Releases a given lock if the owner information matches.
string | $key | Name of the lock to release. |
string | $ownerid | Owner information to use. |
bool | True if the lock is released, false if it is not. |
Implements cache_is_lockable.
cachestore_redis::set | ( | $key, | |
$value ) |
Set the value of a key.
string | $key | The key to set the value of. |
mixed | $value | The value. |
bool | True if the operation succeeded, false otherwise. |
Reimplemented from cache_store.
cachestore_redis::set_many | ( | array | $keyvaluearray | ) |
Set the values of many keys.
array | $keyvaluearray | An array of key/value pairs. Each item in the array is an associative array with two keys, 'key' and 'value'. |
int | The number of key/value pairs successfuly set. |
Reimplemented from cache_store.
|
static |
Sets the current time (within unit test) for TTL functionality.
This setting is stored in $CFG so will be automatically reset if you use resetAfterTest.
int | $time | Current time (set 0 to start using real time). |
cachestore_redis::shutdown_release_locks | ( | ) |
Releases any locks when the system shuts down, in case there is a crash or somebody forgets to use 'try-finally'.
Do not call this function manually (except from unit test).
cachestore_redis::store_total_size | ( | ) |
Gets Redis reported memory usage.
int|null | Memory used by Redis or null if we don't know |
Reimplemented from cache_store.
|
inherited |
Returns true if the store instance guarantees data.
bool |
|
inherited |
Returns true if the store automatically dereferences objects.
bool |
|
inherited |
Returns true if the store instance supports multiple identifiers.
bool |
Reimplemented in cachestore_file, cachestore_session, and cachestore_static.
|
inherited |
Returns true if the store instance supports native ttl.
bool |
|
static |
Return configuration to use when unit testing.
array |
Implements cache_store_interface.
|
inherited |
The cache store dereferences objects.
When set, loaders will assume that all data coming from this store has already had all references resolved. So even for complex object structures it will not try to remove references again.
|
inherited |
Application caches.
These are shared caches.
|
inherited |
Request caches.
Static caches really.
|
inherited |
Session caches.
Just access to the PHP session.