Moodle PHP Documentation 5.1
Moodle 5.1dev (Build: 20250706) (a6b17d8f6bd)
cachestore_redis Class Reference
Inheritance diagram for cachestore_redis:
core_cache\store core_cache\key_aware_cache_interface core_cache\configurable_cache_interface core_cache\searchable_cache_interface core_cache\lockable_cache_interface core_cache\store_interface

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.
 
 create_clone (array $details=[])
 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 (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 (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.
 
int const CONNECTION_TIMEOUT = 3
 The number of seconds to wait for a connection or response from the Redis server.
 
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.
 
int $connectiontimeout = self::CONNECTION_TIMEOUT
 The number of seconds to wait for a connection or response from the Redis server.
 
array $currentlocks = null
 Array of current locks, or null if we haven't registered shutdown function.
 
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.
 

Constructor & Destructor Documentation

◆ __construct()

cachestore_redis::__construct ( $name,
array $configuration = [] )

Constructs an instance of this type of store.

Parameters
string$name
array$configuration

Reimplemented from core_cache\store.

Member Function Documentation

◆ acquire_lock()

cachestore_redis::acquire_lock ( $key,
$ownerid )

Tries to acquire a lock with a given name.

See also
lockable_cache_interface
Parameters
string$keyName of the lock to acquire.
string$owneridInformation to identify owner of lock if acquired.
Return values
boolTrue if the lock was acquired, false if it was not.

Implements core_cache\lockable_cache_interface.

◆ are_requirements_met()

static cachestore_redis::are_requirements_met ( )
static

Determines if the requirements for this type of store are met.

Return values
bool

Implements core_cache\store_interface.

◆ cache_size_details()

core_cache\store::cache_size_details ( int $samplekeys = 50)
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:

  • supported (true if any other values are completed)
  • items (number of items)
  • mean (mean size of one item in bytes)
  • sd (standard deviation of item size in bytes, based on sample)
  • margin (95% confidence margin for mean - will be 0 if exactly computed)
Parameters
int$samplekeysNumber of samples to use
Return values
stdClassObject with information about the store size

Reimplemented in cachestore_file.

◆ can_add_instance()

static core_cache\store::can_add_instance ( )
staticinherited

Returns true if the user can add an instance of the store plugin.

Return values
bool

Reimplemented in cachestore_session, cachestore_static, and core_cache\dummy_cachestore.

◆ check_lock_state()

cachestore_redis::check_lock_state ( $key,
$ownerid )

Checks a lock with a given name and owner information.

See also
lockable_cache_interface
Parameters
string$keyName of the lock to check.
string$owneridOwner information to check existing lock against.
Return values
mixedTrue if the lock exists and the owner information matches, null if the lock does not exist, and false otherwise.

Implements core_cache\lockable_cache_interface.

◆ config_get_compressor_options()

static cachestore_redis::config_get_compressor_options ( )
static

Gets an array of options to use as the compressor.

Return values
array

◆ config_get_configuration_array()

static cachestore_redis::config_get_configuration_array ( $data)
static

Creates a configuration array from given 'add instance' form data.

See also
configurable_cache_interface
Parameters
stdClass$data
Return values
array

Implements core_cache\configurable_cache_interface.

◆ config_get_serializer_options()

static cachestore_redis::config_get_serializer_options ( )
static

Gets an array of options to use as the serialiser.

Return values
array

◆ config_set_edit_form_data()

static cachestore_redis::config_set_edit_form_data ( moodleform $editform,
array $config )
static

Sets form data from a configuration array.

See also
configurable_cache_interface
Parameters
moodleform$editform
array$config

Implements core_cache\configurable_cache_interface.

◆ create_clone()

core_cache\store::create_clone ( array $details = [])
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.

Parameters
array$detailsAn array containing the details of the store from the cache config.
Return values
store

◆ delete()

cachestore_redis::delete ( $key)

Delete the given key.

Parameters
string$keyThe key to delete.
Return values
boolTrue if the delete operation succeeds, false otherwise.

Reimplemented from core_cache\store.

◆ delete_many()

cachestore_redis::delete_many ( array $keys)

Delete many keys.

Parameters
array$keysThe keys to delete.
Return values
intThe number of keys successfully deleted.

Reimplemented from core_cache\store.

◆ estimate_stored_size()

cachestore_redis::estimate_stored_size ( $key,
$value )

Estimates the stored size, taking into account whether compression is turned on.

Parameters
mixed$keyKey name
mixed$valueValue
Return values
intApproximate stored size

Reimplemented from core_cache\store.

◆ expire_ttl()

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.

Return values
arrayVarious keys with information about how the expiry went

◆ find_all()

cachestore_redis::find_all ( )

Finds all of the keys being used by this cache store instance.

Return values
arrayof all keys in the hash as a numbered array.

Implements core_cache\searchable_cache_interface.

◆ find_by_prefix()

cachestore_redis::find_by_prefix ( $prefix)

Finds all of the keys whose keys start with the given prefix.

Parameters
string$prefix
Return values
arrayList of keys that match this prefix.

Implements core_cache\searchable_cache_interface.

◆ get()

cachestore_redis::get ( $key)

Get the value associated with a given key.

Parameters
string$keyThe key to get the value of.
Return values
mixedThe value of the key, or false if there is no value associated with the key.

Reimplemented from core_cache\store.

◆ get_last_io_bytes()

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.

Return values
intBytes read or written
Since
Moodle 4.0

Reimplemented from core_cache\store.

◆ get_many()

cachestore_redis::get_many ( $keys)

Get the values associated with a list of keys.

Parameters
array$keysThe keys to get the values of.
Return values
arrayAn array of the values of the given keys.

Reimplemented from core_cache\store.

◆ get_supported_features()

static cachestore_redis::get_supported_features ( array $configuration = array())
static

Get the features of this type of cache store.

Parameters
array$configuration
Return values
int

Implements core_cache\store_interface.

◆ get_supported_modes()

static cachestore_redis::get_supported_modes ( array $configuration = array())
static

Get the supported modes of this type of cache store.

Parameters
array$configuration
Return values
int

Implements core_cache\store_interface.

◆ get_time()

static cachestore_redis::get_time ( )
staticprotected

Gets the current time for TTL functionality.

This wrapper makes it easier to unit-test the TTL behaviour.

Return values
intCurrent time

◆ get_warnings()

core_cache\store::get_warnings ( )
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.

Return values
string[]An array of warning strings from the store instance.

◆ has()

cachestore_redis::has ( $key)

Determines if the store has a given key.

See also
key_aware_cache_interface
Parameters
string$keyThe key to check for.
Return values
boolTrue if the key exists, false if it does not.

Implements core_cache\key_aware_cache_interface.

◆ has_all()

cachestore_redis::has_all ( array $keys)

Determines if the store has all of the keys in a list.

See also
key_aware_cache_interface
Parameters
array$keysThe keys to check for.
Return values
boolTrue if all of the keys are found, false otherwise.

Implements core_cache\key_aware_cache_interface.

◆ has_any()

cachestore_redis::has_any ( array $keys)

Determines if the store has any of the keys in a list.

See also
key_aware_cache_interface
Parameters
array$keysThe keys to check for.
Return values
boolTrue if any of the keys are found, false none of the keys are found.

Implements core_cache\key_aware_cache_interface.

◆ initialise()

cachestore_redis::initialise ( definition $definition)

Initialize the store.

Parameters
definition$definition
Return values
bool

Reimplemented from core_cache\store.

◆ initialise_test_instance()

static cachestore_redis::initialise_test_instance ( definition $definition)
static

Creates an instance of the store for testing.

Parameters
definition$definition
Return values
mixedAn instance of the store, or false if an instance cannot be created.

Implements core_cache\store_interface.

◆ instance_created()

core_cache\store::instance_created ( )
inherited

Performs any necessary operation when the store instance has been created.

Since
Moodle 2.5

Reimplemented in cachestore_file.

◆ instance_deleted()

cachestore_redis::instance_deleted ( )

Cleans up after an instance of the store.

Reimplemented from core_cache\store.

◆ is_initialised()

cachestore_redis::is_initialised ( )

Determine if the store is initialized.

Return values
bool

Reimplemented from core_cache\store.

◆ is_ready()

cachestore_redis::is_ready ( )

Determine if the store is ready for use.

Return values
bool

Reimplemented from core_cache\store.

◆ is_searchable()

core_cache\store::is_searchable ( )
inherited

Returns true if the store instance is searchable.

Return values
bool

◆ is_supported_mode()

static cachestore_redis::is_supported_mode ( $mode)
static

Determines if this type of store supports a given mode.

Parameters
int$mode
Return values
bool

Implements core_cache\store_interface.

◆ my_name()

cachestore_redis::my_name ( )

Get the name of the store.

Return values
string

Reimplemented from core_cache\store.

◆ new_redis()

cachestore_redis::new_redis ( array $configuration)
protected

Create a new Redis or RedisCluster instance and connect to the server.

Parameters
array$configurationThe redis instance configuration.
Return values
Redis|RedisCluster|null

◆ ping()

cachestore_redis::ping ( RedisCluster|Redis $redis)
protected

See if we can ping Redis server.

Parameters
RedisCluster | Redis$redis
Return values
bool

◆ purge()

cachestore_redis::purge ( )

Purges all keys from the store.

Return values
bool

Reimplemented from core_cache\store.

◆ ready_to_be_used_for_testing()

static cachestore_redis::ready_to_be_used_for_testing ( )
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.

Return values
bool

Reimplemented from core_cache\store.

◆ release_lock()

cachestore_redis::release_lock ( $key,
$ownerid )

Releases a given lock if the owner information matches.

See also
lockable_cache_interface
Parameters
string$keyName of the lock to release.
string$owneridOwner information to use.
Return values
boolTrue if the lock is released, false if it is not.

Implements core_cache\lockable_cache_interface.

◆ set()

cachestore_redis::set ( $key,
$value )

Set the value of a key.

Parameters
string$keyThe key to set the value of.
mixed$valueThe value.
Return values
boolTrue if the operation succeeded, false otherwise.

Reimplemented from core_cache\store.

◆ set_many()

cachestore_redis::set_many ( array $keyvaluearray)

Set the values of many keys.

Parameters
array$keyvaluearrayAn array of key/value pairs. Each item in the array is an associative array with two keys, 'key' and 'value'.
Return values
intThe number of key/value pairs successfuly set.

Reimplemented from core_cache\store.

◆ set_phpunit_time()

static cachestore_redis::set_phpunit_time ( int $time = 0)
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.

Parameters
int$timeCurrent time (set 0 to start using real time).

◆ shutdown_release_locks()

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).

◆ store_total_size()

cachestore_redis::store_total_size ( )

Gets Redis reported memory usage.

Return values
int|nullMemory used by Redis or null if we don't know

Reimplemented from core_cache\store.

◆ supports_data_guarantee()

core_cache\store::supports_data_guarantee ( )
inherited

Returns true if the store instance guarantees data.

Return values
bool

◆ supports_dereferencing_objects()

core_cache\store::supports_dereferencing_objects ( )
inherited

Returns true if the store automatically dereferences objects.

Return values
bool

◆ supports_multiple_identifiers()

core_cache\store::supports_multiple_identifiers ( )
inherited

Returns true if the store instance supports multiple identifiers.

Return values
bool

Reimplemented in cachestore_file, cachestore_session, and cachestore_static.

◆ supports_native_ttl()

core_cache\store::supports_native_ttl ( )
inherited

Returns true if the store instance supports native ttl.

Return values
bool

◆ unit_test_configuration()

static cachestore_redis::unit_test_configuration ( )
static

Return configuration to use when unit testing.

Return values
array

Implements core_cache\store_interface.

Member Data Documentation

◆ DEREFERENCES_OBJECTS

const core_cache\store::DEREFERENCES_OBJECTS = 16
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.

◆ MODE_APPLICATION

const core_cache\store::MODE_APPLICATION = 1
inherited

Application caches.

These are shared caches.

◆ MODE_REQUEST

const core_cache\store::MODE_REQUEST = 4
inherited

Request caches.

Static caches really.

◆ MODE_SESSION

const core_cache\store::MODE_SESSION = 2
inherited

Session caches.

Just access to the PHP session.


The documentation for this class was generated from the following file: