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 39 and 311]

This file contains the cache factory 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: 693 lines (27 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: cache_factory  - X-Ref

The cache factory class.

This factory class is important because it stores instances of objects used by the cache API and returns them upon requests.
This allows us to both reuse objects saving on overhead, and gives us an easy place to "reset" the cache API in situations that
we need such as unit testing.

instance($forcereload = false)   X-Ref
Returns an instance of the cache_factory class.

param: bool $forcereload If set to true a new cache_factory instance will be created and used.
return: cache_factory

__construct()   X-Ref
Protected constructor, please use the static instance method.


reset()   X-Ref
Resets the arrays containing instantiated caches, stores, and config instances.


reset_cache_instances()   X-Ref
Resets the stores, clearing the array of created stores.

Cache objects still held onto by the code that initialised them will remain as is
however all future requests for a cache/store will lead to a new instance being re-initialised.

create_cache_from_definition($component, $area, array $identifiers = array()   X-Ref
Creates a cache object given the parameters for a definition.

If a cache has already been created for the given definition then that cache instance will be returned.

param: string $component
param: string $area
param: array $identifiers
param: string $unused Used to be data source aggregate however that was removed and this is now unused.
return: cache_application|cache_session|cache_request

create_cache_from_params($mode, $component, $area, array $identifiers = array()   X-Ref
Creates an ad-hoc cache from the given param.

If a cache has already been created using the same params then that cache instance will be returned.

param: int $mode
param: string $component
param: string $area
param: array $identifiers
param: array $options An array of options, available options are:
return: cache_application|cache_session|cache_request

create_cache(cache_definition $definition)   X-Ref
Common public method to create a cache instance given a definition.

This is used by the static make methods.

param: cache_definition $definition
return: cache_application|cache_session|cache_store

create_store_from_config($name, array $details, cache_definition $definition)   X-Ref
Creates a store instance given its name and configuration.

If the store has already been instantiated then the original object will be returned. (reused)

param: string $name The name of the store (must be unique remember)
param: array $details
param: cache_definition $definition The definition to instantiate it for.
return: boolean|cache_store

get_store_instances_in_use(cache_definition $definition)   X-Ref
Returns an array of cache stores that have been initialised for use in definitions.

param: cache_definition $definition
return: array

get_caches_in_use()   X-Ref
Returns the cache instances that have been used within this request.

return: array

get_adhoc_caches_in_use()   X-Ref
Gets all adhoc caches that have been used within this request.

return: cache_store[] Caches currently in use

create_config_instance($writer = false)   X-Ref
Creates a cache config instance with the ability to write if required.

param: bool $writer If set to true an instance that can update the configuration will be returned.
return: cache_config|cache_config_writer

create_definition($component, $area, $unused = null)   X-Ref
Creates a definition instance or returns the existing one if it has already been created.

param: string $component
param: string $area
param: string $unused This used to be data source aggregate - however that functionality has been removed and
return: cache_definition

create_dummy_store(cache_definition $definition)   X-Ref
Creates a dummy store object for use when a loader has no potential stores to use.

param: cache_definition $definition
return: cachestore_dummy

create_lock_instance(array $config)   X-Ref
Returns a lock instance ready for use.

param: array $config
return: cache_lock_interface

get_state()   X-Ref
Returns the current state of the cache API.

return: int

set_state($state)   X-Ref
Updates the state fo the cache API.

param: int $state
return: bool

updating_started()   X-Ref
Informs the factory that the cache is currently updating itself.

This forces the state to upgrading and can only be called once the cache is ready to use.
Calling it ensure we don't try to reinstantite things when requesting cache definitions that don't exist yet.

updating_finished()   X-Ref
Informs the factory that the upgrading has finished.

This forces the state back to ready.

is_disabled()   X-Ref
Returns true if the cache API has been disabled.

return: bool

is_initialising()   X-Ref
Returns true if the cache is currently initialising itself.

This includes both initialisation and saving the cache config file as part of that initialisation.

return: bool

is_updating()   X-Ref
Returns true if the cache is currently updating itself.

return: bool

disable()   X-Ref
Disables as much of the cache API as possible.

All of the magic associated with the disabled cache is wrapped into this function.
In switching out the factory for the disabled factory it gains full control over the initialisation of objects
and can use all of the disabled alternatives.
Simple!

This function has been marked as protected so that it cannot be abused through the public API presently.
Perhaps in the future we will allow this, however as per the build up to the first release containing
MUC it was decided that this was just to risky and abusable.

stores_disabled()   X-Ref
Returns true if the cache stores have been disabled.

return: bool

disable_stores()   X-Ref
Disables cache stores.

The cache API will continue to function however none of the actual stores will be used.
Instead the dummy store will be provided for all cache requests.
This is useful in situations where you cannot be sure any stores are working.

In order to re-enable the cache you must call the cache factories static reset method:
<code>
// Disable the cache factory.
cache_factory::disable_stores();
// Re-enable the cache factory by resetting it.
cache_factory::reset();
</code>

get_administration_display_helper()   X-Ref
Returns an instance of the current display_helper.

return: core_cache\administration_helper

get_disabled_writer()   X-Ref
Gets the cache_config_writer to use when caching is disabled.
This should only be called from cache_factory_disabled.

return: cache_config_writer