2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Cache definition class
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
32 * The cache definition class.
34 * Cache definitions need to be defined in db/caches.php files.
35 * They can be constructed with the following options.
39 * [int] Sets the mode for the definition. Must be one of cache_store::MODE_*
43 * [bool] Set to true if your cache will only use simple keys for its items.
44 * Simple keys consist of digits, underscores and the 26 chars of the english language. a-zA-Z0-9_
45 * If true the keys won't be hashed before being passed to the cache store for gets/sets/deletes. It will be
46 * better for performance and possible only becase we know the keys are safe.
48 * [bool] If set to true we know that the data is scalar or array of scalar.
49 * + requireidentifiers
50 * [array] An array of identifiers that must be provided to the cache when it is created.
51 * + requiredataguarantee
52 * [bool] If set to true then only stores that can guarantee data will remain available once set will be used.
53 * + requiremultipleidentifiers
54 * [bool] If set to true then only stores that support multiple identifiers will be used.
55 * + requirelockingread
56 * [bool] If set to true then a lock will be gained before reading from the cache store. It is recommended not to use
57 * this setting unless 100% absolutely positively required. Remember 99.9% of caches will NOT need this setting.
58 * This setting will only be used for application caches presently.
59 * + requirelockingwrite
60 * [bool] If set to true then a lock will be gained before writing to the cache store. As above this is not recommended
61 * unless truly needed. Please think about the order of your code and deal with race conditions there first.
62 * This setting will only be used for application caches presently.
64 * [int] If set this will be used as the maximum number of entries within the cache store for this definition.
65 * Its important to note that cache stores don't actually have to acknowledge this setting or maintain it as a hard limit.
67 * [string] A class to use as the loader for this cache. This is an advanced setting and will allow the developer of the
68 * definition to take 100% control of the caching solution.
69 * Any class used here must inherit the cache_loader interface and must extend default cache loader for the mode they are
72 * [string] Suplements the above setting indicated the file containing the class to be used. This file is included when
75 * [string] A class to use as the data loader for this definition.
76 * Any class used here must inherit the cache_data_loader interface.
78 * [string] Supplements the above setting indicating the file containing the class to be used. This file is included when
80 * + staticacceleration
81 * The cache loader will keep an array of the items set and retrieved to the cache during the request.
82 * Consider using this setting when you know that there are going to be many calls to the cache for the same information.
83 * Requests for data in this array will be ultra fast, but it will cost memory.
84 * + staticaccelerationsize
85 * [int] This supplements the above setting by limiting the number of items in the static acceleration array.
86 * Tweaking this setting lower will allow you to minimise the memory implications above while hopefully still managing to
87 * offset calls to the cache store.
89 * [int] A time to live for the data (in seconds). It is strongly recommended that you don't make use of this and
90 * instead try to create an event driven invalidation system.
91 * Not all cache stores will support this natively and there are undesired performance impacts if the cache store does not.
93 * [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
94 * advanced setting and should not be used unless absolutely required. It allows you to avoid the default stores for one
96 * + invalidationevents
97 * [array] An array of events that should cause this cache to invalidate some or all of the items within it.
99 * [int] The sharing options that are appropriate for this definition. Should be the sum of the possible options.
101 * [int] The default sharing option to use. It's highly recommended that you don't set this unless there is a very
102 * specific reason not to use the system default.
104 * [bool] The cache is able to safely run with multiple copies on different webservers without any need for administrator
105 * intervention to ensure that data stays in sync across nodes. This is usually managed by a revision
106 * system as seen in modinfo cache or language cache. Requiring purge on upgrade is not sufficient as
107 * it requires administrator intervention on each node to make it work.
109 * For examples take a look at lib/db/caches.php
113 * @copyright 2012 Sam Hemelryk
114 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
116 class cache_definition
{
118 /** The cache can be shared with everyone */
119 const SHARING_ALL
= 1;
120 /** The cache can be shared with other sites using the same siteid. */
121 const SHARING_SITEID
= 2;
122 /** The cache can be shared with other sites of the same version. */
123 const SHARING_VERSION
= 4;
124 /** The cache can be shared with other sites using the same key */
125 const SHARING_INPUT
= 8;
128 * The default sharing options available.
129 * All + SiteID + Version + Input.
131 const SHARING_DEFAULTOPTIONS
= 15;
133 * The default sharing option that gets used if none have been selected.
134 * SiteID. It is the most restrictive.
136 const SHARING_DEFAULT
= 2;
139 * The identifier for the definition
145 * The mode for the defintion. One of cache_store::MODE_*
151 * The component this definition is associated with.
154 protected $component;
157 * The area this definition is associated with.
163 * If set to true we know the keys are simple. a-zA-Z0-9_
166 protected $simplekeys = false;
169 * Set to true if we know the data is scalar or array of scalar.
172 protected $simpledata = false;
175 * An array of identifiers that must be provided when the definition is used to create a cache.
178 protected $requireidentifiers = array();
181 * If set to true then only stores that guarantee data may be used with this definition.
184 protected $requiredataguarantee = false;
187 * If set to true then only stores that support multple identifiers may be used with this definition.
190 protected $requiremultipleidentifiers = false;
193 * If set to true then we know that this definition requires the locking functionality.
194 * This gets set during construction based upon the settings requirelockingread and requirelockingwrite.
197 protected $requirelocking = false;
200 * Set to true if this definition requires read locking.
203 protected $requirelockingread = false;
206 * Gets set to true if this definition requires write locking.
209 protected $requirelockingwrite = false;
212 * Gets set to true if this definition requires searchable stores.
213 * @since Moodle 2.4.4
216 protected $requiresearchable = false;
219 * Sets the maximum number of items that can exist in the cache.
220 * Please note this isn't a hard limit, and doesn't need to be enforced by the caches. They can choose to do so optionally.
223 protected $maxsize = null;
226 * The class to use as the cache loader for this definition.
229 protected $overrideclass = null;
232 * The file in which the override class exists. This will be included if required.
233 * @var string Absolute path
235 protected $overrideclassfile = null;
238 * The data source class to use with this definition.
241 protected $datasource = null;
244 * The file in which the data source class exists. This will be included if required.
247 protected $datasourcefile = null;
250 * Set to true if the cache should hold onto items passing through it to speed up subsequent requests.
253 protected $staticacceleration = false;
256 * The maximum number of items that static acceleration cache should hold onto.
259 protected $staticaccelerationsize = false;
262 * The TTL for data in this cache. Please don't use this, instead use event driven invalidation.
268 * Set to true if this cache should only use mapped cache stores and not the default mode cache store.
271 protected $mappingsonly = false;
274 * An array of events that should cause this cache to invalidate.
277 protected $invalidationevents = array();
280 * An array of identifiers provided to this cache when it was initialised.
283 protected $identifiers = null;
286 * Key prefix for use with single key cache stores
289 protected $keyprefixsingle = null;
292 * Key prefix to use with cache stores that support multi keys.
295 protected $keyprefixmulti = null;
298 * A hash identifier of this definition.
301 protected $definitionhash = null;
304 * The selected sharing mode for this definition.
307 protected $sharingoptions;
310 * Whether this cache supports local storages.
313 protected $canuselocalstore = false;
316 * The selected sharing option.
317 * @var int One of self::SHARING_*
319 protected $selectedsharingoption = self
::SHARING_DEFAULT
;
322 * The user input key to use if the SHARING_INPUT option has been selected.
323 * @var string Must be ALPHANUMEXT
325 protected $userinputsharingkey = '';
328 * Creates a cache definition given a definition from the cache configuration or from a caches.php file.
331 * @param array $definition
332 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
333 * @return cache_definition
334 * @throws coding_exception
336 public static function load($id, array $definition, $unused = null) {
339 if (!array_key_exists('mode', $definition)) {
340 throw new coding_exception('You must provide a mode when creating a cache definition');
342 if (!array_key_exists('component', $definition)) {
343 throw new coding_exception('You must provide a component when creating a cache definition');
345 if (!array_key_exists('area', $definition)) {
346 throw new coding_exception('You must provide an area when creating a cache definition');
348 $mode = (int)$definition['mode'];
349 $component = (string)$definition['component'];
350 $area = (string)$definition['area'];
355 $requireidentifiers = array();
356 $requiredataguarantee = false;
357 $requiremultipleidentifiers = false;
358 $requirelockingread = false;
359 $requirelockingwrite = false;
360 $requiresearchable = ($mode === cache_store
::MODE_SESSION
) ?
true : false;
362 $overrideclass = null;
363 $overrideclassfile = null;
365 $datasourcefile = null;
366 $staticacceleration = false;
367 $staticaccelerationsize = false;
369 $mappingsonly = false;
370 $invalidationevents = array();
371 $sharingoptions = self
::SHARING_DEFAULT
;
372 $selectedsharingoption = self
::SHARING_DEFAULT
;
373 $userinputsharingkey = '';
374 $canuselocalstore = false;
376 if (array_key_exists('simplekeys', $definition)) {
377 $simplekeys = (bool)$definition['simplekeys'];
379 if (array_key_exists('simpledata', $definition)) {
380 $simpledata = (bool)$definition['simpledata'];
382 if (array_key_exists('requireidentifiers', $definition)) {
383 $requireidentifiers = (array)$definition['requireidentifiers'];
385 if (array_key_exists('requiredataguarantee', $definition)) {
386 $requiredataguarantee = (bool)$definition['requiredataguarantee'];
388 if (array_key_exists('requiremultipleidentifiers', $definition)) {
389 $requiremultipleidentifiers = (bool)$definition['requiremultipleidentifiers'];
392 if (array_key_exists('requirelockingread', $definition)) {
393 $requirelockingread = (bool)$definition['requirelockingread'];
395 if (array_key_exists('requirelockingwrite', $definition)) {
396 $requirelockingwrite = (bool)$definition['requirelockingwrite'];
398 $requirelocking = $requirelockingwrite ||
$requirelockingread;
400 if (array_key_exists('requiresearchable', $definition)) {
401 $requiresearchable = (bool)$definition['requiresearchable'];
404 if (array_key_exists('maxsize', $definition)) {
405 $maxsize = (int)$definition['maxsize'];
408 if (array_key_exists('overrideclass', $definition)) {
409 $overrideclass = $definition['overrideclass'];
411 if (array_key_exists('overrideclassfile', $definition)) {
412 $overrideclassfile = $definition['overrideclassfile'];
415 if (array_key_exists('datasource', $definition)) {
416 $datasource = $definition['datasource'];
418 if (array_key_exists('datasourcefile', $definition)) {
419 $datasourcefile = $definition['datasourcefile'];
422 if (array_key_exists('persistent', $definition)) {
423 // Ahhh this is the legacy persistent option.
424 $staticacceleration = (bool)$definition['persistent'];
426 if (array_key_exists('staticacceleration', $definition)) {
427 $staticacceleration = (bool)$definition['staticacceleration'];
429 if (array_key_exists('persistentmaxsize', $definition)) {
430 // Ahhh this is the legacy persistentmaxsize option.
431 $staticaccelerationsize = (int)$definition['persistentmaxsize'];
433 if (array_key_exists('staticaccelerationsize', $definition)) {
434 $staticaccelerationsize = (int)$definition['staticaccelerationsize'];
436 if (array_key_exists('ttl', $definition)) {
437 $ttl = (int)$definition['ttl'];
439 if (array_key_exists('mappingsonly', $definition)) {
440 $mappingsonly = (bool)$definition['mappingsonly'];
442 if (array_key_exists('invalidationevents', $definition)) {
443 $invalidationevents = (array)$definition['invalidationevents'];
445 if (array_key_exists('sharingoptions', $definition)) {
446 $sharingoptions = (int)$definition['sharingoptions'];
448 if (array_key_exists('selectedsharingoption', $definition)) {
449 $selectedsharingoption = (int)$definition['selectedsharingoption'];
450 } else if (array_key_exists('defaultsharing', $definition)) {
451 $selectedsharingoption = (int)$definition['defaultsharing'];
452 } else if ($sharingoptions ^
$selectedsharingoption) {
453 if ($sharingoptions & self
::SHARING_SITEID
) {
454 $selectedsharingoption = self
::SHARING_SITEID
;
455 } else if ($sharingoptions & self
::SHARING_VERSION
) {
456 $selectedsharingoption = self
::SHARING_VERSION
;
458 $selectedsharingoption = self
::SHARING_ALL
;
461 if (array_key_exists('canuselocalstore', $definition)) {
462 $canuselocalstore = (bool)$definition['canuselocalstore'];
465 if (array_key_exists('userinputsharingkey', $definition) && !empty($definition['userinputsharingkey'])) {
466 $userinputsharingkey = (string)$definition['userinputsharingkey'];
469 if (!is_null($overrideclass)) {
470 if (!is_null($overrideclassfile)) {
471 if (strpos($overrideclassfile, $CFG->dirroot
) !== 0) {
472 $overrideclassfile = $CFG->dirroot
.'/'.$overrideclassfile;
474 if (strpos($overrideclassfile, '../') !== false) {
475 throw new coding_exception('No path craziness allowed within override class file path.');
477 if (!file_exists($overrideclassfile)) {
478 throw new coding_exception('The override class file does not exist.');
480 require_once($overrideclassfile);
482 if (!class_exists($overrideclass)) {
483 throw new coding_exception('The override class does not exist.');
486 // Make sure that the provided class extends the default class for the mode.
487 if (get_parent_class($overrideclass) !== cache_helper
::get_class_for_mode($mode)) {
488 throw new coding_exception('The override class does not immediately extend the relevant cache class.');
492 if (!is_null($datasource)) {
493 if (!is_null($datasourcefile)) {
494 if (strpos($datasourcefile, $CFG->dirroot
) !== 0) {
495 $datasourcefile = $CFG->dirroot
.'/'.$datasourcefile;
497 if (strpos($datasourcefile, '../') !== false) {
498 throw new coding_exception('No path craziness allowed within data source file path.');
500 if (!file_exists($datasourcefile)) {
501 throw new coding_exception('The data source class file does not exist.');
503 require_once($datasourcefile);
505 if (!class_exists($datasource)) {
506 throw new coding_exception('The data source class does not exist.');
508 if (!array_key_exists('cache_data_source', class_implements($datasource))) {
509 throw new coding_exception('Cache data source classes must implement the cache_data_source interface');
513 $cachedefinition = new cache_definition();
514 $cachedefinition->id
= $id;
515 $cachedefinition->mode
= $mode;
516 $cachedefinition->component
= $component;
517 $cachedefinition->area
= $area;
518 $cachedefinition->simplekeys
= $simplekeys;
519 $cachedefinition->simpledata
= $simpledata;
520 $cachedefinition->requireidentifiers
= $requireidentifiers;
521 $cachedefinition->requiredataguarantee
= $requiredataguarantee;
522 $cachedefinition->requiremultipleidentifiers
= $requiremultipleidentifiers;
523 $cachedefinition->requirelocking
= $requirelocking;
524 $cachedefinition->requirelockingread
= $requirelockingread;
525 $cachedefinition->requirelockingwrite
= $requirelockingwrite;
526 $cachedefinition->requiresearchable
= $requiresearchable;
527 $cachedefinition->maxsize
= $maxsize;
528 $cachedefinition->overrideclass
= $overrideclass;
529 $cachedefinition->overrideclassfile
= $overrideclassfile;
530 $cachedefinition->datasource
= $datasource;
531 $cachedefinition->datasourcefile
= $datasourcefile;
532 $cachedefinition->staticacceleration
= $staticacceleration;
533 $cachedefinition->staticaccelerationsize
= $staticaccelerationsize;
534 $cachedefinition->ttl
= $ttl;
535 $cachedefinition->mappingsonly
= $mappingsonly;
536 $cachedefinition->invalidationevents
= $invalidationevents;
537 $cachedefinition->sharingoptions
= $sharingoptions;
538 $cachedefinition->selectedsharingoption
= $selectedsharingoption;
539 $cachedefinition->userinputsharingkey
= $userinputsharingkey;
540 $cachedefinition->canuselocalstore
= $canuselocalstore;
542 return $cachedefinition;
546 * Creates an ah-hoc cache definition given the required params.
548 * Please note that when using an adhoc definition you cannot set any of the optional params.
549 * This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that.
551 * @param int $mode One of cache_store::MODE_*
552 * @param string $component The component this definition relates to.
553 * @param string $area The area this definition relates to.
554 * @param array $options An array of options, available options are:
555 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
556 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
557 * - overrideclass : The class to use as the loader.
558 * - staticacceleration : If set to true the cache will hold onto data passing through it.
559 * - staticaccelerationsize : Set it to an int to limit the size of the staticacceleration cache.
560 * @return cache_application|cache_session|cache_request
562 public static function load_adhoc($mode, $component, $area, array $options = array()) {
563 $id = 'adhoc/'.$component.'_'.$area;
566 'component' => $component,
569 if (!empty($options['simplekeys'])) {
570 $definition['simplekeys'] = $options['simplekeys'];
572 if (!empty($options['simpledata'])) {
573 $definition['simpledata'] = $options['simpledata'];
575 if (!empty($options['persistent'])) {
576 // Ahhh this is the legacy persistent option.
577 $definition['staticacceleration'] = (bool)$options['persistent'];
579 if (!empty($options['staticacceleration'])) {
580 $definition['staticacceleration'] = (bool)$options['staticacceleration'];
582 if (!empty($options['staticaccelerationsize'])) {
583 $definition['staticaccelerationsize'] = (int)$options['staticaccelerationsize'];
585 if (!empty($options['overrideclass'])) {
586 $definition['overrideclass'] = $options['overrideclass'];
588 if (!empty($options['sharingoptions'])) {
589 $definition['sharingoptions'] = $options['sharingoptions'];
591 return self
::load($id, $definition, null);
595 * Returns the cache loader class that should be used for this definition.
598 public function get_cache_class() {
599 if (!is_null($this->overrideclass
)) {
600 return $this->overrideclass
;
602 return cache_helper
::get_class_for_mode($this->mode
);
606 * Returns the id of this definition.
609 public function get_id() {
614 * Returns the name for this definition
617 public function get_name() {
618 $identifier = 'cachedef_'.clean_param($this->area
, PARAM_STRINGID
);
619 $component = $this->component
;
620 if ($component === 'core') {
621 $component = 'cache';
623 return new lang_string($identifier, $component);
627 * Returns the mode of this definition
628 * @return int One more cache_store::MODE_
630 public function get_mode() {
635 * Returns the area this definition is associated with.
638 public function get_area() {
643 * Returns the component this definition is associated with.
646 public function get_component() {
647 return $this->component
;
651 * Returns true if this definition is using simple keys.
653 * Simple keys contain only a-zA-Z0-9_
657 public function uses_simple_keys() {
658 return $this->simplekeys
;
662 * Returns the identifiers that are being used for this definition.
665 public function get_identifiers() {
666 if (!isset($this->identifiers
)) {
669 return $this->identifiers
;
673 * Returns the ttl in seconds for this definition if there is one, or null if not.
676 public function get_ttl() {
681 * Returns the maximum number of items allowed in this cache.
684 public function get_maxsize() {
685 return $this->maxsize
;
689 * Returns true if this definition should only be used with mappings.
692 public function is_for_mappings_only() {
693 return $this->mappingsonly
;
697 * Returns true if the data is known to be scalar or array of scalar.
700 public function uses_simple_data() {
701 return $this->simpledata
;
705 * Returns true if this definition requires a data guarantee from the cache stores being used.
708 public function require_data_guarantee() {
709 return $this->requiredataguarantee
;
713 * Returns true if this definition requires that the cache stores support multiple identifiers
716 public function require_multiple_identifiers() {
717 return $this->requiremultipleidentifiers
;
721 * Returns true if this definition requires locking functionality. Either read or write locking.
724 public function require_locking() {
725 return $this->requirelocking
;
729 * Returns true if this definition requires read locking.
732 public function require_locking_read() {
733 return $this->requirelockingread
;
737 * Returns true if this definition requires write locking.
740 public function require_locking_write() {
741 return $this->requirelockingwrite
;
745 * Returns true if this definition allows local storage to be used for caching.
746 * @since Moodle 3.1.0
749 public function can_use_localstore() {
750 return $this->canuselocalstore
;
754 * Returns true if this definition requires a searchable cache.
755 * @since Moodle 2.4.4
758 public function require_searchable() {
759 return $this->requiresearchable
;
763 * Returns true if this definition has an associated data source.
766 public function has_data_source() {
767 return !is_null($this->datasource
);
771 * Returns an instance of the data source class used for this definition.
773 * @return cache_data_source
774 * @throws coding_exception
776 public function get_data_source() {
777 if (!$this->has_data_source()) {
778 throw new coding_exception('This cache does not use a data source.');
780 return forward_static_call(array($this->datasource
, 'get_instance_for_cache'), $this);
784 * Sets the identifiers for this definition, or updates them if they have already been set.
786 * @param array $identifiers
787 * @return bool false if no identifiers where changed, true otherwise.
788 * @throws coding_exception
790 public function set_identifiers(array $identifiers = array()) {
791 if ($this->identifiers
!== null) {
792 throw new coding_exception("You can only set identifiers on initial definition creation." .
793 " Define a new cache to set different identifiers.");
795 if (!empty($identifiers) && !empty($this->invalidationevents
)) {
796 throw new coding_exception("You cannot use event invalidation and identifiers at the same time.");
799 foreach ($this->requireidentifiers
as $identifier) {
800 if (!isset($identifiers[$identifier])) {
801 throw new coding_exception('Identifier required for cache has not been provided: '.$identifier);
805 $this->identifiers
= array();
807 foreach ($identifiers as $name => $value) {
808 $this->identifiers
[$name] = (string)$value;
810 // Reset the key prefix's they need updating now.
811 $this->keyprefixsingle
= null;
812 $this->keyprefixmulti
= null;
818 * Returns the requirements of this definition as a binary flag.
821 public function get_requirements_bin() {
823 if ($this->require_data_guarantee()) {
824 $requires +
= cache_store
::SUPPORTS_DATA_GUARANTEE
;
826 if ($this->require_multiple_identifiers()) {
827 $requires +
= cache_store
::SUPPORTS_MULTIPLE_IDENTIFIERS
;
829 if ($this->require_searchable()) {
830 $requires +
= cache_store
::IS_SEARCHABLE
;
836 * Please call {@link cache_definition::use_static_acceleration()} instead.
838 * @see cache_definition::use_static_acceleration()
839 * @deprecated since 2.6
841 public function should_be_persistent() {
842 throw new coding_exception('cache_definition::should_be_persistent() can not be used anymore.' .
843 ' Please use cache_definition::use_static_acceleration() instead.');
847 * Returns true if we should hold onto the data flowing through the cache.
849 * If set to true data flowing through the cache will be stored in a static variable
850 * to make subsequent requests for the data much faster.
854 public function use_static_acceleration() {
855 if ($this->mode
=== cache_store
::MODE_REQUEST
) {
856 // Request caches should never use static acceleration - it just doesn't make sense.
859 return $this->staticacceleration
;
863 * Please call {@link cache_definition::get_static_acceleration_size()} instead.
865 * @see cache_definition::get_static_acceleration_size()
866 * @deprecated since 2.6
868 public function get_persistent_max_size() {
869 throw new coding_exception('cache_definition::get_persistent_max_size() can not be used anymore.' .
870 ' Please use cache_definition::get_static_acceleration_size() instead.');
874 * Returns the max size for the static acceleration array.
877 public function get_static_acceleration_size() {
878 return $this->staticaccelerationsize
;
882 * Generates a hash of this definition and returns it.
885 public function generate_definition_hash() {
886 if ($this->definitionhash
=== null) {
887 $this->definitionhash
= md5("{$this->mode} {$this->component} {$this->area}");
889 return $this->definitionhash
;
893 * Generates a single key prefix for this definition
897 public function generate_single_key_prefix() {
898 if ($this->keyprefixsingle
=== null) {
899 $this->keyprefixsingle
= $this->mode
.'/'.$this->component
.'/'.$this->area
;
900 $this->keyprefixsingle
.= '/'.$this->get_cache_identifier();
901 $identifiers = $this->get_identifiers();
903 foreach ($identifiers as $key => $value) {
904 $this->keyprefixsingle
.= '/'.$key.'='.$value;
907 $this->keyprefixsingle
= md5($this->keyprefixsingle
);
909 return $this->keyprefixsingle
;
913 * Generates a multi key prefix for this definition
917 public function generate_multi_key_parts() {
918 if ($this->keyprefixmulti
=== null) {
919 $this->keyprefixmulti
= array(
920 'mode' => $this->mode
,
921 'component' => $this->component
,
922 'area' => $this->area
,
923 'siteidentifier' => $this->get_cache_identifier()
925 if (isset($this->identifiers
) && !empty($this->identifiers
)) {
926 $identifiers = array();
927 foreach ($this->identifiers
as $key => $value) {
928 $identifiers[] = htmlentities($key, ENT_QUOTES
, 'UTF-8').'='.htmlentities($value, ENT_QUOTES
, 'UTF-8');
930 $this->keyprefixmulti
['identifiers'] = join('&', $identifiers);
933 return $this->keyprefixmulti
;
937 * Check if this definition should invalidate on the given event.
939 * @param string $event
940 * @return bool True if the definition should invalidate on the event. False otherwise.
942 public function invalidates_on_event($event) {
943 return (in_array($event, $this->invalidationevents
));
947 * Check if the definition has any invalidation events.
949 * @return bool True if it does, false otherwise
951 public function has_invalidation_events() {
952 return !empty($this->invalidationevents
);
956 * Returns all of the invalidation events for this definition.
960 public function get_invalidation_events() {
961 return $this->invalidationevents
;
965 * Returns a cache identification string.
967 * @return string A string to be used as part of keys.
969 protected function get_cache_identifier() {
970 $identifiers = array();
971 if ($this->selectedsharingoption
& self
::SHARING_ALL
) {
972 // Nothing to do here.
974 if ($this->selectedsharingoption
& self
::SHARING_SITEID
) {
975 $identifiers[] = cache_helper
::get_site_identifier();
977 if ($this->selectedsharingoption
& self
::SHARING_VERSION
) {
978 $identifiers[] = cache_helper
::get_site_version();
980 if ($this->selectedsharingoption
& self
::SHARING_INPUT
&& !empty($this->userinputsharingkey
)) {
981 $identifiers[] = $this->userinputsharingkey
;
984 return join('/', $identifiers);
988 * Returns true if this definition requires identifiers.
992 public function has_required_identifiers() {
993 return (count($this->requireidentifiers
) > 0);
997 * Returns the possible sharing options that can be used with this defintion.
1001 public function get_sharing_options() {
1002 return $this->sharingoptions
;
1006 * Returns the user entered sharing key for this definition.
1010 public function get_user_input_sharing_key() {
1011 return $this->userinputsharingkey
;
1015 * Returns the user selected sharing option for this definition.
1019 public function get_selected_sharing_option() {
1020 return $this->selectedsharingoption
;