Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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: 715 lines (21 kb)
Included or required: 2 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.

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.

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.

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