Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

Redis Cache Store - Main library

Copyright: 2013 Adam Durana
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 974 lines (31 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: cachestore_redis  - X-Ref

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.

are_requirements_met()   X-Ref
Determines if the requirements for this type of store are met.

return: bool

is_supported_mode($mode)   X-Ref
Determines if this type of store supports a given mode.

param: int $mode
return: bool

get_supported_features(array $configuration = array()   X-Ref
Get the features of this type of cache store.

param: array $configuration
return: int

get_supported_modes(array $configuration = array()   X-Ref
Get the supported modes of this type of cache store.

param: array $configuration
return: int

__construct($name, array $configuration = array()   X-Ref
Constructs an instance of this type of store.

param: string $name
param: array $configuration

new_redis($server, $prefix = '', $password = '')   X-Ref
Create a new Redis instance and
connect to the server.

param: string $server The server connection string
param: string $prefix The key prefix
param: string $password The server connection password
return: Redis

ping(Redis $redis)   X-Ref
See if we can ping Redis server

param: Redis $redis
return: bool

my_name()   X-Ref
Get the name of the store.

return: string

initialise(cache_definition $definition)   X-Ref
Initialize the store.

param: cache_definition $definition
return: bool

is_initialised()   X-Ref
Determine if the store is initialized.

return: bool

is_ready()   X-Ref
Determine if the store is ready for use.

return: bool

get($key)   X-Ref
Get the value associated with a given key.

param: string $key The key to get the value of.
return: mixed The value of the key, or false if there is no value associated with the key.

get_many($keys)   X-Ref
Get the values associated with a list of keys.

param: array $keys The keys to get the values of.
return: array An array of the values of the given keys.

get_last_io_bytes()   X-Ref
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: int Bytes read or written

set($key, $value)   X-Ref
Set the value of a key.

param: string $key The key to set the value of.
param: mixed $value The value.
return: bool True if the operation succeeded, false otherwise.

set_many(array $keyvaluearray)   X-Ref
Set the values of many keys.

param: array $keyvaluearray An array of key/value pairs. Each item in the array is an associative array
return: int The number of key/value pairs successfuly set.

delete($key)   X-Ref
Delete the given key.

param: string $key The key to delete.
return: bool True if the delete operation succeeds, false otherwise.

delete_many(array $keys)   X-Ref
Delete many keys.

param: array $keys The keys to delete.
return: int The number of keys successfully deleted.

purge()   X-Ref
Purges all keys from the store.

return: bool

instance_deleted()   X-Ref
Cleans up after an instance of the store.


has($key)   X-Ref
Determines if the store has a given key.

param: string $key The key to check for.
return: bool True if the key exists, false if it does not.

has_any(array $keys)   X-Ref
Determines if the store has any of the keys in a list.

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.

has_all(array $keys)   X-Ref
Determines if the store has all of the keys in a list.

param: array $keys The keys to check for.
return: bool True if all of the keys are found, false otherwise.

acquire_lock($key, $ownerid)   X-Ref
Tries to acquire a lock with a given name.

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.

shutdown_release_locks()   X-Ref
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).

check_lock_state($key, $ownerid)   X-Ref
Checks a lock with a given name and owner information.

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

find_all()   X-Ref
Finds all of the keys being used by this cache store instance.

return: array of all keys in the hash as a numbered array.

find_by_prefix($prefix)   X-Ref
Finds all of the keys whose keys start with the given prefix.

param: string $prefix
return: array List of keys that match this prefix.

release_lock($key, $ownerid)   X-Ref
Releases a given lock if the owner information matches.

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.

expire_ttl()   X-Ref
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: array Various keys with information about how the expiry went

get_time()   X-Ref
Gets the current time for TTL functionality. This wrapper makes it easier to unit-test
the TTL behaviour.

return: int Current time

set_phpunit_time(int $time = 0)   X-Ref
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.

param: int $time Current time (set 0 to start using real time).

estimate_stored_size($key, $value)   X-Ref
Estimates the stored size, taking into account whether compression is turned on.

param: mixed $key Key name
param: mixed $value Value
return: int Approximate stored size

store_total_size()   X-Ref
Gets Redis reported memory usage.

return: int|null Memory used by Redis or null if we don't know

config_get_configuration_array($data)   X-Ref
Creates a configuration array from given 'add instance' form data.

param: stdClass $data
return: array

config_set_edit_form_data(moodleform $editform, array $config)   X-Ref
Sets form data from a configuration array.

param: moodleform $editform
param: array $config

initialise_test_instance(cache_definition $definition)   X-Ref
Creates an instance of the store for testing.

param: cache_definition $definition
return: mixed An instance of the store, or false if an instance cannot be created.

unit_test_configuration()   X-Ref
Return configuration to use when unit testing.

return: array

ready_to_be_used_for_testing()   X-Ref
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: bool

config_get_serializer_options()   X-Ref
Gets an array of options to use as the serialiser.

return: array

config_get_compressor_options()   X-Ref
Gets an array of options to use as the compressor.

return: array

compress($value)   X-Ref
Compress the given value, serializing it first.

param: mixed $value
return: string

uncompress($value)   X-Ref
Uncompresses (deflates) the data, unserialising it afterwards.

param: string $value
return: mixed

serialize($value)   X-Ref
Serializes the data according to the configured serializer.

param: mixed $value
return: string

unserialize($value)   X-Ref
Unserializes the data according to the configured serializer

param: string $value
return: mixed