Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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

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

Defines 7 classes

cache_definition:: (1 method):
  load()

file:: (0 methods):

file:: (0 methods):

does:: (39 methods):
  load_adhoc()
  get_cache_class()
  get_id()
  get_name()
  get_mode()
  get_area()
  get_component()
  uses_simple_keys()
  get_identifiers()
  get_ttl()
  get_maxsize()
  is_for_mappings_only()
  uses_simple_data()
  require_data_guarantee()
  require_multiple_identifiers()
  require_locking()
  require_locking_read()
  require_locking_write()
  can_use_localstore()
  require_searchable()
  has_data_source()
  get_data_source()
  set_identifiers()
  get_requirements_bin()
  should_be_persistent()
  use_static_acceleration()
  get_persistent_max_size()
  get_static_acceleration_size()
  generate_definition_hash()
  generate_single_key_prefix()
  generate_multi_key_parts()
  invalidates_on_event()
  has_invalidation_events()
  get_invalidation_events()
  get_cache_identifier()
  has_required_identifiers()
  get_sharing_options()
  get_user_input_sharing_key()
  get_selected_sharing_option()

does:: (39 methods):
  load_adhoc()
  get_cache_class()
  get_id()
  get_name()
  get_mode()
  get_area()
  get_component()
  uses_simple_keys()
  get_identifiers()
  get_ttl()
  get_maxsize()
  is_for_mappings_only()
  uses_simple_data()
  require_data_guarantee()
  require_multiple_identifiers()
  require_locking()
  require_locking_read()
  require_locking_write()
  can_use_localstore()
  require_searchable()
  has_data_source()
  get_data_source()
  set_identifiers()
  get_requirements_bin()
  should_be_persistent()
  use_static_acceleration()
  get_persistent_max_size()
  get_static_acceleration_size()
  generate_definition_hash()
  generate_single_key_prefix()
  generate_multi_key_parts()
  invalidates_on_event()
  has_invalidation_events()
  get_invalidation_events()
  get_cache_identifier()
  has_required_identifiers()
  get_sharing_options()
  get_user_input_sharing_key()
  get_selected_sharing_option()

file:: (0 methods):

does:: (39 methods):
  load_adhoc()
  get_cache_class()
  get_id()
  get_name()
  get_mode()
  get_area()
  get_component()
  uses_simple_keys()
  get_identifiers()
  get_ttl()
  get_maxsize()
  is_for_mappings_only()
  uses_simple_data()
  require_data_guarantee()
  require_multiple_identifiers()
  require_locking()
  require_locking_read()
  require_locking_write()
  can_use_localstore()
  require_searchable()
  has_data_source()
  get_data_source()
  set_identifiers()
  get_requirements_bin()
  should_be_persistent()
  use_static_acceleration()
  get_persistent_max_size()
  get_static_acceleration_size()
  generate_definition_hash()
  generate_single_key_prefix()
  generate_multi_key_parts()
  invalidates_on_event()
  has_invalidation_events()
  get_invalidation_events()
  get_cache_identifier()
  has_required_identifiers()
  get_sharing_options()
  get_user_input_sharing_key()
  get_selected_sharing_option()


Class: cache_definition  - X-Ref

The cache definition class.

Cache definitions need to be defined in db/caches.php files.
They can be constructed with the following options.

Required settings:
+ mode
[int] Sets the mode for the definition. Must be one of cache_store::MODE_*

Optional settings:
+ simplekeys
[bool] Set to true if your cache will only use simple keys for its items.
Simple keys consist of digits, underscores and the 26 chars of the english language. a-zA-Z0-9_
If true the keys won't be hashed before being passed to the cache store for gets/sets/deletes. It will be
better for performance and possible only becase we know the keys are safe.
+ simpledata
[bool] If set to true we know that the data is scalar or array of scalar.
+ requireidentifiers
[array] An array of identifiers that must be provided to the cache when it is created.
+ requiredataguarantee
[bool] If set to true then only stores that can guarantee data will remain available once set will be used.
+ requiremultipleidentifiers
[bool] If set to true then only stores that support multiple identifiers will be used.
+ requirelockingread
[bool] If set to true then a lock will be gained before reading from the cache store. It is recommended not to use
this setting unless 100% absolutely positively required. Remember 99.9% of caches will NOT need this setting.
This setting will only be used for application caches presently.
+ requirelockingwrite
[bool] If set to true then a lock will be gained before writing to the cache store. As above this is not recommended
unless truly needed. Please think about the order of your code and deal with race conditions there first.
This setting will only be used for application caches presently.
+ maxsize
[int] If set this will be used as the maximum number of entries within the cache store for this definition.
Its important to note that cache stores don't actually have to acknowledge this setting or maintain it as a hard limit.
+ overrideclass
[string] A class to use as the loader for this cache. This is an advanced setting and will allow the developer of the
definition to take 100% control of the caching solution.
Any class used here must inherit the cache_loader interface and must extend default cache loader for the mode they are
using.
+ overrideclassfile
[string] Suplements the above setting indicated the file containing the class to be used. This file is included when
required.
+ datasource
[string] A class to use as the data loader for this definition.
Any class used here must inherit the cache_data_loader interface.
+ datasourcefile
[string] Supplements the above setting indicating the file containing the class to be used. This file is included when
required.
+ staticacceleration
The cache loader will keep an array of the items set and retrieved to the cache during the request.
Consider using this setting when you know that there are going to be many calls to the cache for the same information.
Requests for data in this array will be ultra fast, but it will cost memory.
+ staticaccelerationsize
[int] This supplements the above setting by limiting the number of items in the static acceleration array.
Tweaking this setting lower will allow you to minimise the memory implications above while hopefully still managing to
offset calls to the cache store.
+ ttl
[int] A time to live for the data (in seconds). It is strongly recommended that you don't make use of this and
instead try to create an event driven invalidation system.
Not all cache stores will support this natively and there are undesired performance impacts if the cache store does not.
+ mappingsonly
[bool] If set to true only the mapped cache store(s) will be used and the default mode store will not. This is a super
advanced setting and should not be used unless absolutely required. It allows you to avoid the default stores for one
reason or another.
+ invalidationevents
[array] An array of events that should cause this cache to invalidate some or all of the items within it.
+ sharingoptions
[int] The sharing options that are appropriate for this definition. Should be the sum of the possible options.
+ defaultsharing
[int] The default sharing option to use. It's highly recommended that you don't set this unless there is a very
specific reason not to use the system default.
+ canuselocalstore
[bool] The cache is able to safely run with multiple copies on different webservers without any need for administrator
intervention to ensure that data stays in sync across nodes.  This is usually managed by a revision
system as seen in modinfo cache or language cache.  Requiring purge on upgrade is not sufficient as
it requires administrator intervention on each node to make it work.

For examples take a look at lib/db/caches.php

load($id, array $definition, $unused = null)   X-Ref
Creates a cache definition given a definition from the cache configuration or from a caches.php file.

param: string $id
param: array $definition
param: string $unused Used to be datasourceaggregate but that was removed and this is now unused.
return: cache_definition

Class: file  - X-Ref

Class: file  - X-Ref

Class: does  - X-Ref

load_adhoc($mode, $component, $area, array $options = array()   X-Ref
Creates an ah-hoc cache definition given the required params.

Please note that when using an adhoc definition you cannot set any of the optional params.
This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that.

param: int $mode One of cache_store::MODE_*
param: string $component The component this definition relates to.
param: string $area The area this definition relates to.
param: array $options An array of options, available options are:
return: cache_application|cache_session|cache_request

get_cache_class()   X-Ref
Returns the cache loader class that should be used for this definition.

return: string

get_id()   X-Ref
Returns the id of this definition.

return: string

get_name()   X-Ref
Returns the name for this definition

return: string

get_mode()   X-Ref
Returns the mode of this definition

return: int One more cache_store::MODE_

get_area()   X-Ref
Returns the area this definition is associated with.

return: string

get_component()   X-Ref
Returns the component this definition is associated with.

return: string

uses_simple_keys()   X-Ref
Returns true if this definition is using simple keys.

Simple keys contain only a-zA-Z0-9_

return: bool

get_identifiers()   X-Ref
Returns the identifiers that are being used for this definition.

return: array

get_ttl()   X-Ref
Returns the ttl in seconds for this definition if there is one, or null if not.

return: int|null

get_maxsize()   X-Ref
Returns the maximum number of items allowed in this cache.

return: int

is_for_mappings_only()   X-Ref
Returns true if this definition should only be used with mappings.

return: bool

uses_simple_data()   X-Ref
Returns true if the data is known to be scalar or array of scalar.

return: bool

require_data_guarantee()   X-Ref
Returns true if this definition requires a data guarantee from the cache stores being used.

return: bool

require_multiple_identifiers()   X-Ref
Returns true if this definition requires that the cache stores support multiple identifiers

return: bool

require_locking()   X-Ref
Returns true if this definition requires locking functionality. Either read or write locking.

return: bool

require_locking_read()   X-Ref
Returns true if this definition requires read locking.

return: bool

require_locking_write()   X-Ref
Returns true if this definition requires write locking.

return: bool

can_use_localstore()   X-Ref
Returns true if this definition allows local storage to be used for caching.

return: bool

require_searchable()   X-Ref
Returns true if this definition requires a searchable cache.

return: bool

has_data_source()   X-Ref
Returns true if this definition has an associated data source.

return: bool

get_data_source()   X-Ref
Returns an instance of the data source class used for this definition.

return: cache_data_source

set_identifiers(array $identifiers = array()   X-Ref
Sets the identifiers for this definition, or updates them if they have already been set.

param: array $identifiers
return: bool false if no identifiers where changed, true otherwise.

get_requirements_bin()   X-Ref
Returns the requirements of this definition as a binary flag.

return: int

should_be_persistent()   X-Ref
Please call {@link cache_definition::use_static_acceleration()} instead.


use_static_acceleration()   X-Ref
Returns true if we should hold onto the data flowing through the cache.

If set to true data flowing through the cache will be stored in a static variable
to make subsequent requests for the data much faster.

return: bool

get_persistent_max_size()   X-Ref
Please call {@link cache_definition::get_static_acceleration_size()} instead.


get_static_acceleration_size()   X-Ref
Returns the max size for the static acceleration array.

return: int

generate_definition_hash()   X-Ref
Generates a hash of this definition and returns it.

return: string

generate_single_key_prefix()   X-Ref
Generates a single key prefix for this definition

return: string

generate_multi_key_parts()   X-Ref
Generates a multi key prefix for this definition

return: array

invalidates_on_event($event)   X-Ref
Check if this definition should invalidate on the given event.

param: string $event
return: bool True if the definition should invalidate on the event. False otherwise.

has_invalidation_events()   X-Ref
Check if the definition has any invalidation events.

return: bool True if it does, false otherwise

get_invalidation_events()   X-Ref
Returns all of the invalidation events for this definition.

return: array

get_cache_identifier()   X-Ref
Returns a cache identification string.

return: string A string to be used as part of keys.

has_required_identifiers()   X-Ref
Returns true if this definition requires identifiers.

param: bool

get_sharing_options()   X-Ref
Returns the possible sharing options that can be used with this defintion.

return: int

get_user_input_sharing_key()   X-Ref
Returns the user entered sharing key for this definition.

return: string

get_selected_sharing_option()   X-Ref
Returns the user selected sharing option for this definition.

return: int

Class: does  - X-Ref

load_adhoc($mode, $component, $area, array $options = array()   X-Ref
Creates an ah-hoc cache definition given the required params.

Please note that when using an adhoc definition you cannot set any of the optional params.
This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that.

param: int $mode One of cache_store::MODE_*
param: string $component The component this definition relates to.
param: string $area The area this definition relates to.
param: array $options An array of options, available options are:
return: cache_application|cache_session|cache_request

get_cache_class()   X-Ref
Returns the cache loader class that should be used for this definition.

return: string

get_id()   X-Ref
Returns the id of this definition.

return: string

get_name()   X-Ref
Returns the name for this definition

return: string

get_mode()   X-Ref
Returns the mode of this definition

return: int One more cache_store::MODE_

get_area()   X-Ref
Returns the area this definition is associated with.

return: string

get_component()   X-Ref
Returns the component this definition is associated with.

return: string

uses_simple_keys()   X-Ref
Returns true if this definition is using simple keys.

Simple keys contain only a-zA-Z0-9_

return: bool

get_identifiers()   X-Ref
Returns the identifiers that are being used for this definition.

return: array

get_ttl()   X-Ref
Returns the ttl in seconds for this definition if there is one, or null if not.

return: int|null

get_maxsize()   X-Ref
Returns the maximum number of items allowed in this cache.

return: int

is_for_mappings_only()   X-Ref
Returns true if this definition should only be used with mappings.

return: bool

uses_simple_data()   X-Ref
Returns true if the data is known to be scalar or array of scalar.

return: bool

require_data_guarantee()   X-Ref
Returns true if this definition requires a data guarantee from the cache stores being used.

return: bool

require_multiple_identifiers()   X-Ref
Returns true if this definition requires that the cache stores support multiple identifiers

return: bool

require_locking()   X-Ref
Returns true if this definition requires locking functionality. Either read or write locking.

return: bool

require_locking_read()   X-Ref
Returns true if this definition requires read locking.

return: bool

require_locking_write()   X-Ref
Returns true if this definition requires write locking.

return: bool

can_use_localstore()   X-Ref
Returns true if this definition allows local storage to be used for caching.

return: bool

require_searchable()   X-Ref
Returns true if this definition requires a searchable cache.

return: bool

has_data_source()   X-Ref
Returns true if this definition has an associated data source.

return: bool

get_data_source()   X-Ref
Returns an instance of the data source class used for this definition.

return: cache_data_source

set_identifiers(array $identifiers = array()   X-Ref
Sets the identifiers for this definition, or updates them if they have already been set.

param: array $identifiers
return: bool false if no identifiers where changed, true otherwise.

get_requirements_bin()   X-Ref
Returns the requirements of this definition as a binary flag.

return: int

should_be_persistent()   X-Ref
Please call {@link cache_definition::use_static_acceleration()} instead.


use_static_acceleration()   X-Ref
Returns true if we should hold onto the data flowing through the cache.

If set to true data flowing through the cache will be stored in a static variable
to make subsequent requests for the data much faster.

return: bool

get_persistent_max_size()   X-Ref
Please call {@link cache_definition::get_static_acceleration_size()} instead.


get_static_acceleration_size()   X-Ref
Returns the max size for the static acceleration array.

return: int

generate_definition_hash()   X-Ref
Generates a hash of this definition and returns it.

return: string

generate_single_key_prefix()   X-Ref
Generates a single key prefix for this definition

return: string

generate_multi_key_parts()   X-Ref
Generates a multi key prefix for this definition

return: array

invalidates_on_event($event)   X-Ref
Check if this definition should invalidate on the given event.

param: string $event
return: bool True if the definition should invalidate on the event. False otherwise.

has_invalidation_events()   X-Ref
Check if the definition has any invalidation events.

return: bool True if it does, false otherwise

get_invalidation_events()   X-Ref
Returns all of the invalidation events for this definition.

return: array

get_cache_identifier()   X-Ref
Returns a cache identification string.

return: string A string to be used as part of keys.

has_required_identifiers()   X-Ref
Returns true if this definition requires identifiers.

param: bool

get_sharing_options()   X-Ref
Returns the possible sharing options that can be used with this defintion.

return: int

get_user_input_sharing_key()   X-Ref
Returns the user entered sharing key for this definition.

return: string

get_selected_sharing_option()   X-Ref
Returns the user selected sharing option for this definition.

return: int

Class: file  - X-Ref

Class: does  - X-Ref

load_adhoc($mode, $component, $area, array $options = array()   X-Ref
Creates an ah-hoc cache definition given the required params.

Please note that when using an adhoc definition you cannot set any of the optional params.
This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that.

param: int $mode One of cache_store::MODE_*
param: string $component The component this definition relates to.
param: string $area The area this definition relates to.
param: array $options An array of options, available options are:
return: cache_application|cache_session|cache_request

get_cache_class()   X-Ref
Returns the cache loader class that should be used for this definition.

return: string

get_id()   X-Ref
Returns the id of this definition.

return: string

get_name()   X-Ref
Returns the name for this definition

return: string

get_mode()   X-Ref
Returns the mode of this definition

return: int One more cache_store::MODE_

get_area()   X-Ref
Returns the area this definition is associated with.

return: string

get_component()   X-Ref
Returns the component this definition is associated with.

return: string

uses_simple_keys()   X-Ref
Returns true if this definition is using simple keys.

Simple keys contain only a-zA-Z0-9_

return: bool

get_identifiers()   X-Ref
Returns the identifiers that are being used for this definition.

return: array

get_ttl()   X-Ref
Returns the ttl in seconds for this definition if there is one, or null if not.

return: int|null

get_maxsize()   X-Ref
Returns the maximum number of items allowed in this cache.

return: int

is_for_mappings_only()   X-Ref
Returns true if this definition should only be used with mappings.

return: bool

uses_simple_data()   X-Ref
Returns true if the data is known to be scalar or array of scalar.

return: bool

require_data_guarantee()   X-Ref
Returns true if this definition requires a data guarantee from the cache stores being used.

return: bool

require_multiple_identifiers()   X-Ref
Returns true if this definition requires that the cache stores support multiple identifiers

return: bool

require_locking()   X-Ref
Returns true if this definition requires locking functionality. Either read or write locking.

return: bool

require_locking_read()   X-Ref
Returns true if this definition requires read locking.

return: bool

require_locking_write()   X-Ref
Returns true if this definition requires write locking.

return: bool

can_use_localstore()   X-Ref
Returns true if this definition allows local storage to be used for caching.

return: bool

require_searchable()   X-Ref
Returns true if this definition requires a searchable cache.

return: bool

has_data_source()   X-Ref
Returns true if this definition has an associated data source.

return: bool

get_data_source()   X-Ref
Returns an instance of the data source class used for this definition.

return: cache_data_source

set_identifiers(array $identifiers = array()   X-Ref
Sets the identifiers for this definition, or updates them if they have already been set.

param: array $identifiers
return: bool false if no identifiers where changed, true otherwise.

get_requirements_bin()   X-Ref
Returns the requirements of this definition as a binary flag.

return: int

should_be_persistent()   X-Ref
Please call {@link cache_definition::use_static_acceleration()} instead.


use_static_acceleration()   X-Ref
Returns true if we should hold onto the data flowing through the cache.

If set to true data flowing through the cache will be stored in a static variable
to make subsequent requests for the data much faster.

return: bool

get_persistent_max_size()   X-Ref
Please call {@link cache_definition::get_static_acceleration_size()} instead.


get_static_acceleration_size()   X-Ref
Returns the max size for the static acceleration array.

return: int

generate_definition_hash()   X-Ref
Generates a hash of this definition and returns it.

return: string

generate_single_key_prefix()   X-Ref
Generates a single key prefix for this definition

return: string

generate_multi_key_parts()   X-Ref
Generates a multi key prefix for this definition

return: array

invalidates_on_event($event)   X-Ref
Check if this definition should invalidate on the given event.

param: string $event
return: bool True if the definition should invalidate on the event. False otherwise.

has_invalidation_events()   X-Ref
Check if the definition has any invalidation events.

return: bool True if it does, false otherwise

get_invalidation_events()   X-Ref
Returns all of the invalidation events for this definition.

return: array

get_cache_identifier()   X-Ref
Returns a cache identification string.

return: string A string to be used as part of keys.

has_required_identifiers()   X-Ref
Returns true if this definition requires identifiers.

param: bool

get_sharing_options()   X-Ref
Returns the possible sharing options that can be used with this defintion.

return: int

get_user_input_sharing_key()   X-Ref
Returns the user entered sharing key for this definition.

return: string

get_selected_sharing_option()   X-Ref
Returns the user selected sharing option for this definition.

return: int