Moodle PHP Documentation 4.4
Moodle 4.4.1 (Build: 20240610) (db07c09afc5)
cachestore_redis Class Reference

Redis Cache Store. More...

Inheritance diagram for cachestore_redis:
cache_store cache_is_key_aware cache_is_lockable cache_is_configurable cache_is_searchable 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.
 
 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.
 

Detailed Description

Redis Cache 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.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

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

Member Function Documentation

◆ acquire_lock()

cachestore_redis::acquire_lock ( $key,
$ownerid )

Tries to acquire a lock with a given name.

See also
cache_is_lockable
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 cache_is_lockable.

◆ 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 cache_store_interface.

◆ cache_size_details()

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 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_dummy, cachestore_session, and cachestore_static.

◆ check_lock_state()

cachestore_redis::check_lock_state ( $key,
$ownerid )

Checks a lock with a given name and owner information.

See also
cache_is_lockable
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 cache_is_lockable.

◆ cleanup()

cache_store::cleanup ( )
inherited
Deprecated
since 2.5
See also
cache_store\instance_deleted()

Reimplemented in cachestore_dummy.

◆ 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
cache_is_configurable
Parameters
stdClass$data
Return values
array

Implements cache_is_configurable.

◆ 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
cache_is_configurable
Parameters
moodleform$editform
array$config

Implements cache_is_configurable.

◆ create_clone()

cache_store::create_clone ( array $details = array())
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
cache_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 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 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 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 cache_is_searchable.

◆ 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 cache_is_searchable.

◆ 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 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 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 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 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 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()

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
cache_is_key_aware
Parameters
string$keyThe key to check for.
Return values
boolTrue if the key exists, false if it does not.

Implements cache_is_key_aware.

◆ has_all()

cachestore_redis::has_all ( array $keys)

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

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

Implements cache_is_key_aware.

◆ has_any()

cachestore_redis::has_any ( array $keys)

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

See also
cache_is_key_aware
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 cache_is_key_aware.

◆ initialise()

cachestore_redis::initialise ( cache_definition $definition)

Initialize the store.

Parameters
cache_definition$definition
Return values
bool

Reimplemented from cache_store.

◆ initialise_test_instance()

static cachestore_redis::initialise_test_instance ( cache_definition $definition)
static

Creates an instance of the store for testing.

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

Implements cache_store_interface.

◆ instance_created()

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

◆ is_initialised()

cachestore_redis::is_initialised ( )

Determine if the store is initialized.

Return values
bool

Reimplemented from cache_store.

◆ is_ready()

cachestore_redis::is_ready ( )

Determine if the store is ready for use.

Return values
bool

Reimplemented from cache_store.

◆ is_searchable()

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

◆ my_name()

cachestore_redis::my_name ( )

Get the name of the store.

Return values
string

Reimplemented from 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 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 cache_store.

◆ release_lock()

cachestore_redis::release_lock ( $key,
$ownerid )

Releases a given lock if the owner information matches.

See also
cache_is_lockable
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 cache_is_lockable.

◆ 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 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 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 cache_store.

◆ supports_data_guarantee()

cache_store::supports_data_guarantee ( )
inherited

Returns true if the store instance guarantees data.

Return values
bool

◆ supports_dereferencing_objects()

cache_store::supports_dereferencing_objects ( )
inherited

Returns true if the store automatically dereferences objects.

Return values
bool

◆ supports_multiple_identifiers()

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

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

Member Data Documentation

◆ DEREFERENCES_OBJECTS

const 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 cache_store::MODE_APPLICATION = 1
inherited

Application caches.

These are shared caches.

◆ MODE_REQUEST

const cache_store::MODE_REQUEST = 4
inherited

Request caches.

Static caches really.

◆ MODE_SESSION

const 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: