Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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: 924 lines (29 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.

return: bool
param: int $mode

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

return: int
param: array $configuration

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

return: int
param: array $configuration

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

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

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

return: bool
param: Redis $redis

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

return: string

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

return: bool
param: cache_definition $definition

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.

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

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

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

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.

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

return: bool True if the lock was acquired, false if it was not.
param: string $key Name of the lock to acquire.
param: string $ownerid Information to identify owner of lock if acquired.

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

return: mixed True if the lock exists and the owner information matches, null if the lock does not
param: string $key Name of the lock to check.
param: string $ownerid Owner information to check existing lock against.

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.

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

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

return: bool True if the lock is released, false if it is not.
param: string $key Name of the lock to release.
param: string $ownerid Owner information to use.

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.

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

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.

return: array
param: stdClass $data

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.

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

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.

return: string
param: mixed $value

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

return: mixed
param: string $value

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

return: string
param: mixed $value

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

return: mixed
param: string $value