Moodle APIs 4.3
Moodle 4.3.6 (Build: 20240812)
cachestore_session Class Reference

The Session store class. More...

Inheritance diagram for cachestore_session:

Public Member Functions

 __construct ($name, array $configuration=array())
 Constructs the store instance.
 
 cache_size_details (int $samplekeys=50)
 Gets the amount of memory used by this specific cache within the store, if known.
 
 cleanup ()
 
 create_clone (array $details=array())
 Creates a clone of this store instance ready to be initialised.
 
 delete ($key)
 Deletes an item from the cache store.
 
 delete_many (array $keys)
 Deletes several keys from the cache in a single action.
 
 estimate_stored_size ($key, $value)
 Estimates the storage size used within this cache if the given value is stored with the given key.
 
 find_all ()
 Finds all of the keys being stored in the cache store instance.
 
 find_by_prefix ($prefix)
 Finds all of the keys whose keys start with the given prefix.
 
 get ($key)
 Retrieves an item from the cache store given its 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)
 Retrieves several items from the cache store in a single transaction.
 
 get_warnings ()
 Can be overridden to return any warnings this store instance should make to the admin.
 
 has ($key)
 Checks if the store has a record for the given key and returns true if so.
 
 has_all (array $keys)
 Returns true if the store contains records for all of the given keys.
 
 has_any (array $keys)
 Returns true if the store contains records for any of the given keys.
 
 initialise (cache_definition $definition)
 Initialises the cache.
 
 instance_created ()
 Performs any necessary operation when the store instance has been created.
 
 instance_deleted ()
 Performs any necessary clean up when the store instance is being deleted.
 
 is_initialised ()
 Returns true once this instance has been initialised.
 
 is_ready ()
 Returns true if this cache store instance is ready to use.
 
 is_searchable ()
 Returns true if the store instance is searchable.
 
 my_name ()
 Returns the name of this instance.
 
 purge ()
 Purges the cache deleting all items within it.
 
 set ($key, $data)
 Sets an item in the cache given its key and data value.
 
 set ($key, $data, $testmaxsize=true)
 Sets an item in the cache given its key and data value.
 
 set_many (array $keyvaluearray)
 Sets many items in the cache in a single transaction.
 
 store_supports_native_ttl ()
 This store supports native TTL handling.
 
 store_total_size ()
 Gets the amount of memory/storage currently used by this cache store if known.
 
 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 false as this store does not support multiple identifiers.
 
 supports_native_ttl ()
 Returns true if the store instance supports native ttl.
 

Static Public Member Functions

static are_requirements_met ()
 Returns true if the store requirements are met.
 
static can_add_instance ()
 Returns true if the user can add an instance of the store plugin.
 
static get_supported_features (array $configuration=array())
 Returns the supported features as a combined int.
 
static get_supported_modes (array $configuration=array())
 Returns the supported modes as a combined int.
 
static initialise_test_instance (cache_definition $definition)
 Generates an instance of the cache store that can be used for testing.
 
static is_supported_mode ($mode)
 Returns true if the given mode is supported by this store.
 
static ready_to_be_used_for_testing ()
 Returns true if this cache store instance is both suitable for testing, and ready for testing.
 
static unit_test_configuration ()
 Generates the appropriate configuration required for unit testing.
 

Public Attributes

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.
 

Protected Member Functions

 check_ttl ()
 Removes expired elements.
 
 reduce_for_maxsize ()
 Reduces the size of the array if maxsize has been hit.
 

Static Protected Member Functions

static flush_store ()
 Flushes the store of all data.
 
static flush_store_by_id ($id)
 Flushes the data belong to the given store id.
 
static & register_store_id ($id)
 Returns a static store by reference... REFERENCE SUPER IMPORTANT.
 

Protected Attributes

bool int $maxsize = false
 The maximum size for the store, or false if there isn't one.
 
store $name
 The name of the store.
 
array $store
 The store we use for data.
 
int $storecount = 0
 The number of items currently being stored.
 
string $storeid
 The store id (should be unique)
 
int $ttl = 0
 The ttl if there is one.
 

Detailed Description

The Session store class.

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

Constructor & Destructor Documentation

◆ __construct()

cachestore_session::__construct ( $name,
array $configuration = array() )

Constructs the store instance.

Noting that this function is not an initialisation. It is used to prepare the store for use. The store will be initialised when required and will be provided with a cache_definition at that time.

Parameters
string$name
array$configuration

Reimplemented from cache_store.

Member Function Documentation

◆ are_requirements_met()

static cachestore_session::are_requirements_met ( )
static

Returns true if the store requirements 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 cachestore_session::can_add_instance ( )
static

Returns true if the user can add an instance of the store plugin.

Return values
bool

Reimplemented from cache_store.

◆ check_ttl()

cachestore_session::check_ttl ( )
protected

Removes expired elements.

Return values
intnumber of removed elements

◆ cleanup()

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

Reimplemented in cachestore_dummy.

◆ 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_session::delete ( $key)

Deletes an item from the cache store.

Parameters
string$keyThe key to delete.
Return values
boolReturns true if the operation was a success, false otherwise.

Reimplemented from cache_store.

◆ delete_many()

cachestore_session::delete_many ( array $keys)

Deletes several keys from the cache in a single action.

Parameters
array$keysThe keys to delete
Return values
intThe number of items successfully deleted.

Reimplemented from cache_store.

◆ estimate_stored_size()

cache_store::estimate_stored_size ( $key,
$value )
inherited

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

Parameters
mixed$keyKey
mixed$valueValue
Return values
intSize in bytes

Reimplemented in cachestore_redis.

◆ find_all()

cachestore_session::find_all ( )

Finds all of the keys being stored in the cache store instance.

Return values
array

Implements cache_is_searchable.

◆ find_by_prefix()

cachestore_session::find_by_prefix ( $prefix)

Finds all of the keys whose keys start with the given prefix.

Parameters
string$prefix
Return values
arrayAn array of keys.

Implements cache_is_searchable.

◆ flush_store_by_id()

static session_data_store::flush_store_by_id ( $id)
staticprotectedinherited

Flushes the data belong to the given store id.

Parameters
string$id

◆ get()

cachestore_session::get ( $key)

Retrieves an item from the cache store given its key.

Parameters
string$keyThe key to retrieve
Return values
mixedThe data that was associated with the key, or false if the key did not exist.

Reimplemented from cache_store.

◆ get_last_io_bytes()

cache_store::get_last_io_bytes ( )
inherited

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 values
intBytes read (or 0 if none/not supported)
Since
Moodle 4.0

Reimplemented in cachestore_file, and cachestore_redis.

◆ get_many()

cachestore_session::get_many ( $keys)

Retrieves several items from the cache store in a single transaction.

If not all of the items are available in the cache then the data value for those that are missing will be set to false.

Parameters
array$keysThe array of keys to retrieve
Return values
arrayAn array of items from the cache. There will be an item for each key, those that were not in the store will be set to false.

Reimplemented from cache_store.

◆ get_supported_features()

static cachestore_session::get_supported_features ( array $configuration = array())
static

Returns the supported features as a combined int.

Parameters
array$configuration
Return values
int

Implements cache_store_interface.

◆ get_supported_modes()

static cachestore_session::get_supported_modes ( array $configuration = array())
static

Returns the supported modes as a combined int.

Parameters
array$configuration
Return values
int

Implements cache_store_interface.

◆ 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_session::has ( $key)

Checks if the store has a record for the given key and returns true if so.

Parameters
string$key
Return values
bool

Implements cache_is_key_aware.

◆ has_all()

cachestore_session::has_all ( array $keys)

Returns true if the store contains records for all of the given keys.

Parameters
array$keys
Return values
bool

Implements cache_is_key_aware.

◆ has_any()

cachestore_session::has_any ( array $keys)

Returns true if the store contains records for any of the given keys.

Parameters
array$keys
Return values
bool

Implements cache_is_key_aware.

◆ initialise()

cachestore_session::initialise ( cache_definition $definition)

Initialises the cache.

Once this has been done the cache is all set to be used.

Parameters
cache_definition$definition

Reimplemented from cache_store.

◆ initialise_test_instance()

static cachestore_session::initialise_test_instance ( cache_definition $definition)
static

Generates an instance of the cache store that can be used for testing.

Parameters
cache_definition$definition
Return values
cachestore_session

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_session::instance_deleted ( )

Performs any necessary clean up when the store instance is being deleted.

Reimplemented from cache_store.

◆ is_initialised()

cachestore_session::is_initialised ( )

Returns true once this instance has been initialised.

Return values
bool

Reimplemented from cache_store.

◆ is_ready()

cache_store::is_ready ( )
inherited

Returns true if this cache store instance is ready to use.

Return values
bool

Reimplemented in cachestore_file, and cachestore_redis.

◆ is_searchable()

cache_store::is_searchable ( )
inherited

Returns true if the store instance is searchable.

Return values
bool

◆ is_supported_mode()

static cachestore_session::is_supported_mode ( $mode)
static

Returns true if the given mode is supported by this store.

Parameters
int$modeOne of cache_store\MODE_*
Return values
bool

Implements cache_store_interface.

◆ my_name()

cachestore_session::my_name ( )

Returns the name of this instance.

Return values
string

Reimplemented from cache_store.

◆ purge()

cachestore_session::purge ( )

Purges the cache deleting all items within it.

Return values
booleanTrue on success. False otherwise.

Reimplemented from cache_store.

◆ ready_to_be_used_for_testing()

static cache_store::ready_to_be_used_for_testing ( )
staticinherited

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 values
bool

Reimplemented in cachestore_apcu, and cachestore_redis.

◆ reduce_for_maxsize()

cachestore_session::reduce_for_maxsize ( )
protected

Reduces the size of the array if maxsize has been hit.

This function reduces the size of the store reducing it by 10% of its maxsize. It removes the oldest items in the store when doing this. The reason it does this an doesn't use a least recently used system is purely the overhead such a system requires. The current approach is focused on speed, MUC already adds enough overhead to static/session caches and avoiding more is of benefit.

Return values
int

◆ register_store_id()

static & session_data_store::register_store_id ( $id)
staticprotectedinherited

Returns a static store by reference... REFERENCE SUPER IMPORTANT.

Parameters
string$id
Return values
array

◆ set() [1/2]

cache_store::set ( $key,
$data )
abstractinherited

Sets an item in the cache given its key and data value.

Parameters
string$keyThe key to use.
mixed$dataThe data to set.
Return values
boolTrue if the operation was a success false otherwise.

Reimplemented in cachestore_apcu, cachestore_dummy, cachestore_file, and cachestore_redis.

◆ set() [2/2]

cachestore_session::set ( $key,
$data,
$testmaxsize = true )

Sets an item in the cache given its key and data value.

Parameters
string$keyThe key to use.
mixed$dataThe data to set.
bool$testmaxsizeIf set to true then we test the maxsize arg and reduce if required. If this is set to false you will need to perform these checks yourself. This allows for bulk set's to be performed and maxsize tests performed once.
Return values
boolTrue if the operation was a success false otherwise.

◆ set_many()

cachestore_session::set_many ( array $keyvaluearray)

Sets many items in the cache in a single transaction.

Parameters
array$keyvaluearrayAn array of key value pairs. Each item in the array will be an associative array with two keys, 'key' and 'value'.
Return values
intThe number of items successfully set. It is up to the developer to check this matches the number of items sent ... if they care that is.

Reimplemented from cache_store.

◆ store_supports_native_ttl()

cachestore_session::store_supports_native_ttl ( )

This store supports native TTL handling.

Return values
bool

◆ store_total_size()

cache_store::store_total_size ( )
inherited

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 values
int|nullAmount of memory used in bytes or null

Reimplemented in cachestore_file, and cachestore_redis.

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

cachestore_session::supports_multiple_identifiers ( )

Returns false as this store does not support multiple identifiers.

(This optional function is a performance optimisation; it must be consistent with the value from get_supported_features.)

Return values
boolFalse

Reimplemented from cache_store.

◆ 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_session::unit_test_configuration ( )
static

Generates the appropriate configuration required for unit testing.

Return values
arrayArray of unit test configuration data to be used by initialise().

Implements cache_store_interface.

Member Data Documentation

◆ $ttl

int cachestore_session::$ttl = 0
protected

The ttl if there is one.

Hopefully not.

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