|
| __construct (cache_definition $definition, cache_store $store, $loader=null) |
| Override the cache\construct method.
|
|
| delete ($key, $recurse=true) |
| Delete the given key from the cache.
|
|
| delete_many (array $keys, $recurse=true) |
| Delete all of the given keys from the cache.
|
|
| get ($key, $strictness=IGNORE_MISSING) |
| Retrieves the value for the given key from the cache.
|
|
| get_many (array $keys, $strictness=IGNORE_MISSING) |
| Retrieves an array of values for an array of keys.
|
|
| get_versioned ($key, int $requiredversion, int $strictness=IGNORE_MISSING, &$actualversion=null) |
| Retrieves the value and actual version for the given key, with at least the required version.
|
|
| has ($key) |
| Test is a cache has a key.
|
|
| has ($key, $tryloadifpossible=false) |
| Test is a cache has a key.
|
|
| has_all (array $keys) |
| Test is a cache has all of the given keys.
|
|
| has_any (array $keys) |
| Test if a cache has at least one of the given keys.
|
|
| purge () |
| Purges the cache store, and loader if there is one.
|
|
| purge_current_user () |
| Purges the session cache of all data belonging to the current user.
|
|
| set ($key, $data) |
| Sends a key => value pair to the cache.
|
|
| set_identifiers (array $identifiers) |
| Alters the identifiers that have been provided to the definition.
|
|
| set_many (array $keyvaluearray) |
| Sends several key => value pairs to the cache.
|
|
| set_versioned ($key, int $version, $data) |
| Sets the value for the given key with the given version.
|
|
|
static | compare_purge_tokens ($tokena, $tokenb) |
| Compare a pair of purge tokens.
|
|
static | get_purge_token ($reset=false) |
| Get a 'purge' token used for cache invalidation handling.
|
|
static | make ($component, $area, array $identifiers=array(), $unused=null) |
| Creates a new cache instance for a pre-defined definition.
|
|
static | make_from_params ($mode, $component, $area, array $identifiers=array(), array $options=array()) |
| Creates a new cache instance based upon the given params.
|
|
static | now ($float=false) |
| Returns the timestamp from the first request for the time from the cache API.
|
|
|
| check_tracked_user () |
| Check that this cache instance is tracking the current user.
|
|
| deep_clone ($value) |
| Creates a reference free clone of the given value.
|
|
| delete_from_persist_cache () |
|
| get_datasource () |
| Returns the data source associated with this cache.
|
|
| get_definition () |
| Returns the cache definition.
|
|
| get_from_persist_cache () |
|
| get_implementation ($key, int $requiredversion, int $strictness, &$actualversion=null) |
| Retrieves the value for the given key from the cache.
|
|
| get_key_prefix () |
| Returns the prefix used for all keys.
|
|
| get_loader () |
| Returns the loader associated with this instance.
|
|
| get_store () |
| Returns the cache store.
|
|
| handle_invalidation_events () |
| Process any outstanding invalidation events for the cache we are registering,.
|
|
| has_a_ttl () |
| Returns true if the cache is making use of a ttl.
|
|
| is_in_persist_cache () |
|
| is_using_persist_cache () |
|
| parse_key ($key) |
| Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
|
|
| requires_serialisation ($value, $depth=1) |
| Checks to see if a var requires serialisation.
|
|
| set_data_source (cache_data_source $datasource) |
| Set the data source for this cache.
|
|
| set_implementation ($key, int $version, $data, bool $setparents=true) |
| Sets the value for the given key, optionally with a version tag.
|
|
| set_in_persist_cache () |
|
| set_is_sub_loader ($setting=true) |
| Used to inform the loader of its state as a sub loader, or as the top of the chain.
|
|
| set_loader (cache_loader $loader) |
| Set the loader for this cache.
|
|
| set_session_id () |
| Sets the session id for the loader.
|
|
| static_acceleration_delete ($key) |
| Deletes an item from the static acceleration array.
|
|
| static_acceleration_get ($key) |
| Returns the item from the static acceleration array if it exists there.
|
|
| static_acceleration_has ($key) |
| Returns true if the requested key exists within the static acceleration array.
|
|
| static_acceleration_purge () |
| Purge the static acceleration cache.
|
|
| static_acceleration_set ($key, $data) |
| Sets a key value pair into the static acceleration array.
|
|
| store_supports_key_awareness () |
| Returns true if the store supports key awareness.
|
|
| store_supports_native_locking () |
| Returns true if the store natively supports locking.
|
|
| store_supports_native_ttl () |
| Returns true if the cache store supports native ttl.
|
|
| unref ($data) |
| Removes references where required.
|
|
| use_static_acceleration () |
| The session loader never uses static acceleration.
|
|
|
int | $currentuserid = null |
| The userid this cache is currently using.
|
|
cache_store null | $nativelocking = null |
| Gets set to true if the cache's primary store natively supports locking.
|
|
bool | $perfdebug = false |
| Gets set to true if we want to collect performance information about the cache API.
|
|
bool | $requirelockingbeforewrite = false |
| Gets set to true if the cache writes (set|delete) must have a manual lock created first.
|
|
array | $session = null |
| The session data for the above session id.
|
|
array | $sessionid = null |
| The session id we are currently using.
|
|
strubg | $storetype = 'unknown' |
| Gets set to the class name of the store during initialisation.
|
|
bool | $subloader = false |
| Determines if this loader is a sub loader, not the top of the chain.
|
|
A session cache.
This class is used for session caches returned by the cache\make methods.
It differs from the application loader in a couple of noteable ways:
- Sessions are always expected to exist. Because of this we don't ever use the static acceleration array.
- Session data for a loader instance (store + definition) is consolidate into a single array for storage within the store. Along with this we embed a lastaccessed time with the data. This way we can check sessions for a last access time.
- Session stores are required to support key searching and must implement cache_is_searchable. This ensures stores used for the cache can be targetted for garbage collection of session data.
This cache class should never be interacted with directly. Instead you should always use the cache\make methods. It is technically possible to call those methods through this class however there is no guarantee that you will get an instance of this class back again.
- Todo
- we should support locking in the session as well. Should be pretty simple to set up.
Override the cache\construct method.
This function gets overriden so that we can process any invalidation events if need be. If the definition doesn't have any invalidation events then this occurs exactly as it would for the cache class. Otherwise we look at the last invalidation time and then check the invalidation data for events that have occured between then now.
You should not call this method from your code, instead you should use the cache\make methods.
- Parameters
-
Reimplemented from cache.
static cache::compare_purge_tokens |
( |
| $tokena, |
|
|
| $tokenb ) |
|
staticinherited |
Compare a pair of purge tokens.
If the two tokens are identical, then the return value is 0. If the time component of token A is newer than token B, then a positive value is returned. If the time component of token B is newer than token A, then a negative value is returned.
Note: This function is intended for use from within the Cache API only and not by plugins, or cache stores.
- Parameters
-
string | $tokena | |
string | $tokenb | |
- Return values
-
Retrieves an array of values for an array of keys.
Using this function comes with potential performance implications. Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call the equivalent singular method for each item provided. This should not deter you from using this function as there is a performance benefit in situations where the cache store does support it, but you should be aware of this fact.
- Parameters
-
array | $keys | The keys of the data being requested. Each key can be any structure although using a scalar string or int is recommended in the interests of performance. In advanced cases an array may be useful such as in situations requiring the multi-key functionality. |
int | $strictness | One of IGNORE_MISSING or MUST_EXIST. |
- Return values
-
array | An array of key value pairs for the items that could be retrieved from the cache. If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown. Otherwise any key that did not exist will have a data value of false within the results. |
- Exceptions
-
Reimplemented from cache.
cache::get_versioned |
( |
| $key, |
|
|
int | $requiredversion, |
|
|
int | $strictness = IGNORE_MISSING, |
|
|
& | $actualversion = null ) |
|
inherited |
Retrieves the value and actual version for the given key, with at least the required version.
If there is no value for the key, or there is a value but it doesn't have the required version, then this function will return null (or throw an exception if you set strictness to MUST_EXIST).
This function can be used to make it easier to support localisable caches (where the cache could be stored on a local server as well as a shared cache). Specifying the version means that it will automatically retrieve the correct version if available, either from the local server or [if that has an older version] from the shared server.
If the cached version is newer than specified version, it will be returned regardless. For example, if you request version 4, but the locally cached version is 5, it will be returned. If you request version 6, and the locally cached version is 5, then the system will look in higher-level caches (if any); if there still isn't a version 6 or greater, it will return null.
You must use this function if you use set_versioned.
- Parameters
-
string | int | $key | The key for the data being requested. |
int | $requiredversion | Minimum required version of the data |
int | $strictness | One of IGNORE_MISSING or MUST_EXIST. |
mixed | $actualversion | If specified, will be set to the actual version number retrieved |
- Return values
-
mixed | Data from the cache, or false if the key did not exist or was too old |
- Exceptions
-
Implements cache_loader.
cache_loader::has |
( |
| $key | ) |
|
|
inherited |
Test is a cache has a key.
The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc). Instead it is recommended to write your code in such a way they it performs the following steps:
-
Attempt to retrieve the information.
-
Generate the information.
-
Attempt to set the information
Its also worth mentioning that not all stores support key tests. For stores that don't support key tests this functionality is mimicked by using the equivalent get method. Just one more reason you should not use these methods unless you have a very good reason to do so.
- Parameters
-
- Return values
-
bool | True if the cache has the requested key, false otherwise. |
cache_session::has |
( |
| $key, |
|
|
| $tryloadifpossible = false ) |
Test is a cache has a key.
The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc). Instead it is recommended to write your code in such a way they it performs the following steps:
-
Attempt to retrieve the information.
-
Generate the information.
-
Attempt to set the information
Its also worth mentioning that not all stores support key tests. For stores that don't support key tests this functionality is mimicked by using the equivalent get method. Just one more reason you should not use these methods unless you have a very good reason to do so.
- Parameters
-
string | int | $key | |
bool | $tryloadifpossible | If set to true, the cache doesn't contain the key, and there is another cache loader or data source then the code will try load the key value from the next item in the chain. |
- Return values
-
bool | True if the cache has the requested key, false otherwise. |
Reimplemented from cache.
cache_session::has_all |
( |
array | $keys | ) |
|
Test is a cache has all of the given keys.
It is strongly recommended to avoid the use of this function if not absolutely required. In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
Its also worth mentioning that not all stores support key tests. For stores that don't support key tests this functionality is mimicked by using the equivalent get method. Just one more reason you should not use these methods unless you have a very good reason to do so.
- Parameters
-
- Return values
-
bool | True if the cache has all of the given keys, false otherwise. |
Reimplemented from cache.
cache_session::has_any |
( |
array | $keys | ) |
|
Test if a cache has at least one of the given keys.
It is strongly recommended to avoid the use of this function if not absolutely required. In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
Its also worth mentioning that not all stores support key tests. For stores that don't support key tests this functionality is mimicked by using the equivalent get method. Just one more reason you should not use these methods unless you have a very good reason to do so.
- Parameters
-
- Return values
-
bool | True if the cache has at least one of the given keys |
Reimplemented from cache.
cache_session::parse_key |
( |
| $key | ) |
|
|
protected |
Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
This function is called for every operation that uses keys. For this reason we use this function to also check that the current user is the same as the user who last used this cache.
On top of that if prepends the string 'sess_' to the start of all keys. The _ ensures things are easily identifiable.
- Parameters
-
string | int | $key | As passed to get|set|delete etc. |
- Return values
-
string|array | String unless the store supports multi-identifiers in which case an array if returned. |
Reimplemented from cache.
cache_session::set |
( |
| $key, |
|
|
| $data ) |
Sends a key => value pair to the cache.
// This code will add four entries to the cache, one for each url. $cache->set('main', 'http://moodle.org'); $cache->set('docs', 'http://docs.moodle.org'); $cache->set('tracker', 'http://tracker.moodle.org'); $cache->set('qa', 'http://qa.moodle.net');
- Parameters
-
string | int | $key | The key for the data being requested. It can be any structure although using a scalar string or int is recommended in the interests of performance. In advanced cases an array may be useful such as in situations requiring the multi-key functionality. |
mixed | $data | The data to set against the key. |
- Return values
-
bool | True on success, false otherwise. |
Reimplemented from cache.
cache_session::set_many |
( |
array | $keyvaluearray | ) |
|
Sends several key => value pairs to the cache.
Using this function comes with potential performance implications. Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call the equivalent singular method for each item provided. This should not deter you from using this function as there is a performance benefit in situations where the cache store does support it, but you should be aware of this fact.
// This code will add four entries to the cache, one for each url. $cache->set_many(array( 'main' => 'http://moodle.org', 'docs' => 'http://docs.moodle.org', 'tracker' => 'http://tracker.moodle.org', 'qa' => ''http://qa.moodle.net' ));
- Parameters
-
array | $keyvaluearray | An array of key => value pairs to send to the cache. |
- Return values
-
int | The number of items successfully set. It is up to the developer to check this matches the number of items. ... if they care that is. |
Reimplemented from cache.
cache::set_versioned |
( |
| $key, |
|
|
int | $version, |
|
|
| $data ) |
|
inherited |
Sets the value for the given key with the given version.
The cache does not store multiple versions - any existing version will be overwritten with this one. This function should only be used if there is a known 'current version' (e.g. stored in a database table). It only ensures that the cache does not return outdated data.
This function can be used to help implement localisable caches (where the cache could be stored on a local server as well as a shared cache). The version will be recorded alongside the item and get_versioned will always return the correct version.
The version number must be an integer that always increases. This could be based on the current time, or a stored value that increases by 1 each time it changes, etc.
If you use this function you must use get_versioned to retrieve the data.
- Parameters
-
string | int | $key | The key for the data being set. |
int | $version | Integer for the version of the data |
mixed | $data | The data to set against the key. |
- Return values
-
bool | True on success, false otherwise. |
Implements cache_loader.