Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

Cache store - base class This file is part of Moodle's cache API, affectionately called MUC. It contains the components that are required in order to use caching.

Copyright: 2012 Sam Hemelryk
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 536 lines (18 kb)
Included or required:0 times
Referenced: 1 time
Includes or requires: 0 files

Defines 2 classes


Interface: cache_store_interface  - X-Ref

Cache store interface.

This interface defines the static methods that must be implemented by every cache store plugin.
To ensure plugins implement this class the abstract cache_store class implements this interface.

are_requirements_met()   X-Ref
Static method to check if the store requirements are met.

return: bool True if the stores software/hardware requirements have been met and it can be used. False otherwise.

is_supported_mode($mode)   X-Ref
Static method to check if a store is usable with the given mode.

param: int $mode One of cache_store::MODE_*

get_supported_features(array $configuration = array()   X-Ref
Returns the supported features as a binary flag.

param: array $configuration The configuration of a store to consider specifically.
return: int The supported features.

get_supported_modes(array $configuration = array()   X-Ref
Returns the supported modes as a binary flag.

param: array $configuration The configuration of a store to consider specifically.
return: int The supported modes.

initialise_test_instance(cache_definition $definition)   X-Ref
Generates an instance of the cache store that can be used for testing.

Returns an instance of the cache store, or false if one cannot be created.

param: cache_definition $definition
return: cache_store|false

unit_test_configuration()   X-Ref
Generates the appropriate configuration required for unit testing.

return: array Array of unit test configuration data to be used by initialise().

Class: cache_store  - X-Ref

Abstract cache store class.

All cache store plugins must extend this base class.
It lays down the foundation for what is required of a cache store plugin.

is_ready()   X-Ref
Returns true if this cache store instance is ready to use.

return: bool

cleanup()   X-Ref


instance_created()   X-Ref
Performs any necessary operation when the store instance has been created.


instance_deleted()   X-Ref
Performs any necessary operation when the store instance is being deleted.

This method may be called before the store has been initialised.


can_add_instance()   X-Ref
Returns true if the user can add an instance of the store plugin.

return: bool

supports_data_guarantee()   X-Ref
Returns true if the store instance guarantees data.

return: bool

supports_multiple_identifiers()   X-Ref
Returns true if the store instance supports multiple identifiers.

return: bool

supports_native_ttl()   X-Ref
Returns true if the store instance supports native ttl.

return: bool

is_searchable()   X-Ref
Returns true if the store instance is searchable.

return: bool

supports_dereferencing_objects()   X-Ref
Returns true if the store automatically dereferences objects.

return: bool

create_clone(array $details = array()   X-Ref
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.

param: array $details An array containing the details of the store from the cache config.
return: cache_store

get_warnings()   X-Ref
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: string[] An array of warning strings from the store instance.

estimate_stored_size($key, $value)   X-Ref
Estimates the storage size used within this cache if the given value is stored with the
given key.

This function is not exactly accurate; it does not necessarily take into account all the
overheads involved. It is only intended to give a good idea of the relative size of
different caches.

The default implementation serializes both key and value and sums the lengths (as a rough
estimate which is probably good enough for everything unless the cache offers compression).

param: mixed $key Key
param: mixed $value Value
return: int Size in bytes

store_total_size()   X-Ref
Gets the amount of memory/storage currently used by this cache store if known.

This value should be obtained quickly from the store itself, if available.

This is the total memory usage of the entire store, not for ther specific cache in question.

Where not supported (default), will always return null.

return: int|null Amount of memory used in bytes or null

cache_size_details(int $samplekeys = 50)   X-Ref
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)

param: int $samplekeys Number of samples to use
return: stdClass Object with information about the store size

ready_to_be_used_for_testing()   X-Ref
Returns true if this cache store instance is both suitable for testing, and ready for testing.

Cache stores that support being used as the default store for unit and acceptance testing should
override this function and return true if there requirements have been met.

return: bool

get_last_io_bytes()   X-Ref
Gets the number of bytes read from or written to cache as a result of the last action.

This includes calls to the functions get(), get_many(), set(), and set_many(). The number
is reset by calling any of these functions.

This should be the actual number of bytes of the value read from or written to cache,
giving an impression of the network or other load. It will not be exactly the same amount
as netowrk traffic because of protocol overhead, key text, etc.

If not supported, returns IO_BYTES_NOT_SUPPORTED.

return: int Bytes read (or 0 if none/not supported)