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 * Deprecated - this is completely unused.
251 * @deprecated since 2.9
254 protected $datasourceaggregate = null;
257 * Set to true if the cache should hold onto items passing through it to speed up subsequent requests.
260 protected $staticacceleration = false;
263 * The maximum number of items that static acceleration cache should hold onto.
266 protected $staticaccelerationsize = false;
269 * The TTL for data in this cache. Please don't use this, instead use event driven invalidation.
275 * Set to true if this cache should only use mapped cache stores and not the default mode cache store.
278 protected $mappingsonly = false;
281 * An array of events that should cause this cache to invalidate.
284 protected $invalidationevents = array();
287 * An array of identifiers provided to this cache when it was initialised.
290 protected $identifiers = null;
293 * Key prefix for use with single key cache stores
296 protected $keyprefixsingle = null;
299 * Key prefix to use with cache stores that support multi keys.
302 protected $keyprefixmulti = null;
305 * A hash identifier of this definition.
308 protected $definitionhash = null;
311 * The selected sharing mode for this definition.
314 protected $sharingoptions;
317 * Whether this cache supports local storages.
320 protected $canuselocalstore = false;
323 * The selected sharing option.
324 * @var int One of self::SHARING_*
326 protected $selectedsharingoption = self
::SHARING_DEFAULT
;
329 * The user input key to use if the SHARING_INPUT option has been selected.
330 * @var string Must be ALPHANUMEXT
332 protected $userinputsharingkey = '';
335 * Creates a cache definition given a definition from the cache configuration or from a caches.php file.
338 * @param array $definition
339 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused.
340 * @return cache_definition
341 * @throws coding_exception
343 public static function load($id, array $definition, $unused = null) {
346 if (!array_key_exists('mode', $definition)) {
347 throw new coding_exception('You must provide a mode when creating a cache definition');
349 if (!array_key_exists('component', $definition)) {
350 throw new coding_exception('You must provide a component when creating a cache definition');
352 if (!array_key_exists('area', $definition)) {
353 throw new coding_exception('You must provide an area when creating a cache definition');
355 $mode = (int)$definition['mode'];
356 $component = (string)$definition['component'];
357 $area = (string)$definition['area'];
362 $requireidentifiers = array();
363 $requiredataguarantee = false;
364 $requiremultipleidentifiers = false;
365 $requirelockingread = false;
366 $requirelockingwrite = false;
367 $requiresearchable = ($mode === cache_store
::MODE_SESSION
) ?
true : false;
369 $overrideclass = null;
370 $overrideclassfile = null;
372 $datasourcefile = null;
373 $staticacceleration = false;
374 $staticaccelerationsize = false;
376 $mappingsonly = false;
377 $invalidationevents = array();
378 $sharingoptions = self
::SHARING_DEFAULT
;
379 $selectedsharingoption = self
::SHARING_DEFAULT
;
380 $userinputsharingkey = '';
381 $canuselocalstore = false;
383 if (array_key_exists('simplekeys', $definition)) {
384 $simplekeys = (bool)$definition['simplekeys'];
386 if (array_key_exists('simpledata', $definition)) {
387 $simpledata = (bool)$definition['simpledata'];
389 if (array_key_exists('requireidentifiers', $definition)) {
390 $requireidentifiers = (array)$definition['requireidentifiers'];
392 if (array_key_exists('requiredataguarantee', $definition)) {
393 $requiredataguarantee = (bool)$definition['requiredataguarantee'];
395 if (array_key_exists('requiremultipleidentifiers', $definition)) {
396 $requiremultipleidentifiers = (bool)$definition['requiremultipleidentifiers'];
399 if (array_key_exists('requirelockingread', $definition)) {
400 $requirelockingread = (bool)$definition['requirelockingread'];
402 if (array_key_exists('requirelockingwrite', $definition)) {
403 $requirelockingwrite = (bool)$definition['requirelockingwrite'];
405 $requirelocking = $requirelockingwrite ||
$requirelockingread;
407 if (array_key_exists('requiresearchable', $definition)) {
408 $requiresearchable = (bool)$definition['requiresearchable'];
411 if (array_key_exists('maxsize', $definition)) {
412 $maxsize = (int)$definition['maxsize'];
415 if (array_key_exists('overrideclass', $definition)) {
416 $overrideclass = $definition['overrideclass'];
418 if (array_key_exists('overrideclassfile', $definition)) {
419 $overrideclassfile = $definition['overrideclassfile'];
422 if (array_key_exists('datasource', $definition)) {
423 $datasource = $definition['datasource'];
425 if (array_key_exists('datasourcefile', $definition)) {
426 $datasourcefile = $definition['datasourcefile'];
429 if (array_key_exists('persistent', $definition)) {
430 // Ahhh this is the legacy persistent option.
431 $staticacceleration = (bool)$definition['persistent'];
433 if (array_key_exists('staticacceleration', $definition)) {
434 $staticacceleration = (bool)$definition['staticacceleration'];
436 if (array_key_exists('persistentmaxsize', $definition)) {
437 // Ahhh this is the legacy persistentmaxsize option.
438 $staticaccelerationsize = (int)$definition['persistentmaxsize'];
440 if (array_key_exists('staticaccelerationsize', $definition)) {
441 $staticaccelerationsize = (int)$definition['staticaccelerationsize'];
443 if (array_key_exists('ttl', $definition)) {
444 $ttl = (int)$definition['ttl'];
446 if (array_key_exists('mappingsonly', $definition)) {
447 $mappingsonly = (bool)$definition['mappingsonly'];
449 if (array_key_exists('invalidationevents', $definition)) {
450 $invalidationevents = (array)$definition['invalidationevents'];
452 if (array_key_exists('sharingoptions', $definition)) {
453 $sharingoptions = (int)$definition['sharingoptions'];
455 if (array_key_exists('selectedsharingoption', $definition)) {
456 $selectedsharingoption = (int)$definition['selectedsharingoption'];
457 } else if (array_key_exists('defaultsharing', $definition)) {
458 $selectedsharingoption = (int)$definition['defaultsharing'];
459 } else if ($sharingoptions ^
$selectedsharingoption) {
460 if ($sharingoptions & self
::SHARING_SITEID
) {
461 $selectedsharingoption = self
::SHARING_SITEID
;
462 } else if ($sharingoptions & self
::SHARING_VERSION
) {
463 $selectedsharingoption = self
::SHARING_VERSION
;
465 $selectedsharingoption = self
::SHARING_ALL
;
468 if (array_key_exists('canuselocalstore', $definition)) {
469 $canuselocalstore = (bool)$definition['canuselocalstore'];
472 if (array_key_exists('userinputsharingkey', $definition) && !empty($definition['userinputsharingkey'])) {
473 $userinputsharingkey = (string)$definition['userinputsharingkey'];
476 if (!is_null($overrideclass)) {
477 if (!is_null($overrideclassfile)) {
478 if (strpos($overrideclassfile, $CFG->dirroot
) !== 0) {
479 $overrideclassfile = $CFG->dirroot
.'/'.$overrideclassfile;
481 if (strpos($overrideclassfile, '../') !== false) {
482 throw new coding_exception('No path craziness allowed within override class file path.');
484 if (!file_exists($overrideclassfile)) {
485 throw new coding_exception('The override class file does not exist.');
487 require_once($overrideclassfile);
489 if (!class_exists($overrideclass)) {
490 throw new coding_exception('The override class does not exist.');
493 // Make sure that the provided class extends the default class for the mode.
494 if (get_parent_class($overrideclass) !== cache_helper
::get_class_for_mode($mode)) {
495 throw new coding_exception('The override class does not immediately extend the relevant cache class.');
499 if (!is_null($datasource)) {
500 if (!is_null($datasourcefile)) {
501 if (strpos($datasourcefile, $CFG->dirroot
) !== 0) {
502 $datasourcefile = $CFG->dirroot
.'/'.$datasourcefile;
504 if (strpos($datasourcefile, '../') !== false) {
505 throw new coding_exception('No path craziness allowed within data source file path.');
507 if (!file_exists($datasourcefile)) {
508 throw new coding_exception('The data source class file does not exist.');
510 require_once($datasourcefile);
512 if (!class_exists($datasource)) {
513 throw new coding_exception('The data source class does not exist.');
515 if (!array_key_exists('cache_data_source', class_implements($datasource))) {
516 throw new coding_exception('Cache data source classes must implement the cache_data_source interface');
520 $cachedefinition = new cache_definition();
521 $cachedefinition->id
= $id;
522 $cachedefinition->mode
= $mode;
523 $cachedefinition->component
= $component;
524 $cachedefinition->area
= $area;
525 $cachedefinition->simplekeys
= $simplekeys;
526 $cachedefinition->simpledata
= $simpledata;
527 $cachedefinition->requireidentifiers
= $requireidentifiers;
528 $cachedefinition->requiredataguarantee
= $requiredataguarantee;
529 $cachedefinition->requiremultipleidentifiers
= $requiremultipleidentifiers;
530 $cachedefinition->requirelocking
= $requirelocking;
531 $cachedefinition->requirelockingread
= $requirelockingread;
532 $cachedefinition->requirelockingwrite
= $requirelockingwrite;
533 $cachedefinition->requiresearchable
= $requiresearchable;
534 $cachedefinition->maxsize
= $maxsize;
535 $cachedefinition->overrideclass
= $overrideclass;
536 $cachedefinition->overrideclassfile
= $overrideclassfile;
537 $cachedefinition->datasource
= $datasource;
538 $cachedefinition->datasourcefile
= $datasourcefile;
539 $cachedefinition->staticacceleration
= $staticacceleration;
540 $cachedefinition->staticaccelerationsize
= $staticaccelerationsize;
541 $cachedefinition->ttl
= $ttl;
542 $cachedefinition->mappingsonly
= $mappingsonly;
543 $cachedefinition->invalidationevents
= $invalidationevents;
544 $cachedefinition->sharingoptions
= $sharingoptions;
545 $cachedefinition->selectedsharingoption
= $selectedsharingoption;
546 $cachedefinition->userinputsharingkey
= $userinputsharingkey;
547 $cachedefinition->canuselocalstore
= $canuselocalstore;
549 return $cachedefinition;
553 * Creates an ah-hoc cache definition given the required params.
555 * Please note that when using an adhoc definition you cannot set any of the optional params.
556 * This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that.
558 * @param int $mode One of cache_store::MODE_*
559 * @param string $component The component this definition relates to.
560 * @param string $area The area this definition relates to.
561 * @param array $options An array of options, available options are:
562 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
563 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
564 * - overrideclass : The class to use as the loader.
565 * - staticacceleration : If set to true the cache will hold onto data passing through it.
566 * - staticaccelerationsize : Set it to an int to limit the size of the staticacceleration cache.
567 * @return cache_application|cache_session|cache_request
569 public static function load_adhoc($mode, $component, $area, array $options = array()) {
570 $id = 'adhoc/'.$component.'_'.$area;
573 'component' => $component,
576 if (!empty($options['simplekeys'])) {
577 $definition['simplekeys'] = $options['simplekeys'];
579 if (!empty($options['simpledata'])) {
580 $definition['simpledata'] = $options['simpledata'];
582 if (!empty($options['persistent'])) {
583 // Ahhh this is the legacy persistent option.
584 $definition['staticacceleration'] = (bool)$options['persistent'];
586 if (!empty($options['staticacceleration'])) {
587 $definition['staticacceleration'] = (bool)$options['staticacceleration'];
589 if (!empty($options['staticaccelerationsize'])) {
590 $definition['staticaccelerationsize'] = (int)$options['staticaccelerationsize'];
592 if (!empty($options['overrideclass'])) {
593 $definition['overrideclass'] = $options['overrideclass'];
595 if (!empty($options['sharingoptions'])) {
596 $definition['sharingoptions'] = $options['sharingoptions'];
598 return self
::load($id, $definition, null);
602 * Returns the cache loader class that should be used for this definition.
605 public function get_cache_class() {
606 if (!is_null($this->overrideclass
)) {
607 return $this->overrideclass
;
609 return cache_helper
::get_class_for_mode($this->mode
);
613 * Returns the id of this definition.
616 public function get_id() {
621 * Returns the name for this definition
624 public function get_name() {
625 $identifier = 'cachedef_'.clean_param($this->area
, PARAM_STRINGID
);
626 $component = $this->component
;
627 if ($component === 'core') {
628 $component = 'cache';
630 return new lang_string($identifier, $component);
634 * Returns the mode of this definition
635 * @return int One more cache_store::MODE_
637 public function get_mode() {
642 * Returns the area this definition is associated with.
645 public function get_area() {
650 * Returns the component this definition is associated with.
653 public function get_component() {
654 return $this->component
;
658 * Returns true if this definition is using simple keys.
660 * Simple keys contain only a-zA-Z0-9_
664 public function uses_simple_keys() {
665 return $this->simplekeys
;
669 * Returns the identifiers that are being used for this definition.
672 public function get_identifiers() {
673 if (!isset($this->identifiers
)) {
676 return $this->identifiers
;
680 * Returns the ttl in seconds for this definition if there is one, or null if not.
683 public function get_ttl() {
688 * Returns the maximum number of items allowed in this cache.
691 public function get_maxsize() {
692 return $this->maxsize
;
696 * Returns true if this definition should only be used with mappings.
699 public function is_for_mappings_only() {
700 return $this->mappingsonly
;
704 * Returns true if the data is known to be scalar or array of scalar.
707 public function uses_simple_data() {
708 return $this->simpledata
;
712 * Returns true if this definition requires a data guarantee from the cache stores being used.
715 public function require_data_guarantee() {
716 return $this->requiredataguarantee
;
720 * Returns true if this definition requires that the cache stores support multiple identifiers
723 public function require_multiple_identifiers() {
724 return $this->requiremultipleidentifiers
;
728 * Returns true if this definition requires locking functionality. Either read or write locking.
731 public function require_locking() {
732 return $this->requirelocking
;
736 * Returns true if this definition requires read locking.
739 public function require_locking_read() {
740 return $this->requirelockingread
;
744 * Returns true if this definition requires write locking.
747 public function require_locking_write() {
748 return $this->requirelockingwrite
;
752 * Returns true if this definition allows local storage to be used for caching.
753 * @since Moodle 3.1.0
756 public function can_use_localstore() {
757 return $this->canuselocalstore
;
761 * Returns true if this definition requires a searchable cache.
762 * @since Moodle 2.4.4
765 public function require_searchable() {
766 return $this->requiresearchable
;
770 * Returns true if this definition has an associated data source.
773 public function has_data_source() {
774 return !is_null($this->datasource
);
778 * Returns an instance of the data source class used for this definition.
780 * @return cache_data_source
781 * @throws coding_exception
783 public function get_data_source() {
784 if (!$this->has_data_source()) {
785 throw new coding_exception('This cache does not use a data source.');
787 return forward_static_call(array($this->datasource
, 'get_instance_for_cache'), $this);
791 * Sets the identifiers for this definition, or updates them if they have already been set.
793 * @param array $identifiers
794 * @return bool false if no identifiers where changed, true otherwise.
795 * @throws coding_exception
797 public function set_identifiers(array $identifiers = array()) {
798 // If we are setting the exact same identifiers then just return as nothing really changed.
799 // We don't care about order as cache::make will use the same definition order all the time.
800 if ($identifiers === $this->identifiers
) {
804 foreach ($this->requireidentifiers
as $identifier) {
805 if (!isset($identifiers[$identifier])) {
806 throw new coding_exception('Identifier required for cache has not been provided: '.$identifier);
810 $this->identifiers
= array();
812 foreach ($identifiers as $name => $value) {
813 $this->identifiers
[$name] = (string)$value;
815 // Reset the key prefix's they need updating now.
816 $this->keyprefixsingle
= null;
817 $this->keyprefixmulti
= null;
823 * Returns the requirements of this definition as a binary flag.
826 public function get_requirements_bin() {
828 if ($this->require_data_guarantee()) {
829 $requires +
= cache_store
::SUPPORTS_DATA_GUARANTEE
;
831 if ($this->require_multiple_identifiers()) {
832 $requires +
= cache_store
::SUPPORTS_MULTIPLE_IDENTIFIERS
;
834 if ($this->require_searchable()) {
835 $requires +
= cache_store
::IS_SEARCHABLE
;
841 * Returns true if this definitions cache should be made persistent.
843 * Please call {@link cache_definition::use_static_acceleration()} instead.
845 * @see cache_definition::use_static_acceleration()
846 * @deprecated since 2.6
849 public function should_be_persistent() {
850 debugging('Please upgrade your code to use cache_definition::use_static_acceleration', DEBUG_DEVELOPER
);
851 return $this->use_static_acceleration();
855 * Returns true if we should hold onto the data flowing through the cache.
857 * If set to true data flowing through the cache will be stored in a static variable
858 * to make subsequent requests for the data much faster.
862 public function use_static_acceleration() {
863 if ($this->mode
=== cache_store
::MODE_REQUEST
) {
864 // Request caches should never use static acceleration - it just doesn't make sense.
867 return $this->staticacceleration
;
871 * Returns the max size for the static acceleration array.
873 * Please call {@link cache_definition::get_static_acceleration_size()} instead.
875 * @see cache_definition::get_static_acceleration_size()
876 * @deprecated since 2.6
879 public function get_persistent_max_size() {
880 debugging('Please upgrade your code to call cache_definition::get_static_acceleration_size', DEBUG_DEVELOPER
);
881 return $this->get_static_acceleration_size();
885 * Returns the max size for the static acceleration array.
888 public function get_static_acceleration_size() {
889 return $this->staticaccelerationsize
;
893 * Generates a hash of this definition and returns it.
896 public function generate_definition_hash() {
897 if ($this->definitionhash
=== null) {
898 $this->definitionhash
= md5("{$this->mode} {$this->component} {$this->area}");
900 return $this->definitionhash
;
904 * Generates a single key prefix for this definition
908 public function generate_single_key_prefix() {
909 if ($this->keyprefixsingle
=== null) {
910 $this->keyprefixsingle
= $this->mode
.'/'.$this->component
.'/'.$this->area
;
911 $this->keyprefixsingle
.= '/'.$this->get_cache_identifier();
912 $identifiers = $this->get_identifiers();
914 foreach ($identifiers as $key => $value) {
915 $this->keyprefixsingle
.= '/'.$key.'='.$value;
918 $this->keyprefixsingle
= md5($this->keyprefixsingle
);
920 return $this->keyprefixsingle
;
924 * Generates a multi key prefix for this definition
928 public function generate_multi_key_parts() {
929 if ($this->keyprefixmulti
=== null) {
930 $this->keyprefixmulti
= array(
931 'mode' => $this->mode
,
932 'component' => $this->component
,
933 'area' => $this->area
,
934 'siteidentifier' => $this->get_cache_identifier()
936 if (isset($this->identifiers
) && !empty($this->identifiers
)) {
937 $identifiers = array();
938 foreach ($this->identifiers
as $key => $value) {
939 $identifiers[] = htmlentities($key, ENT_QUOTES
, 'UTF-8').'='.htmlentities($value, ENT_QUOTES
, 'UTF-8');
941 $this->keyprefixmulti
['identifiers'] = join('&', $identifiers);
944 return $this->keyprefixmulti
;
948 * Check if this definition should invalidate on the given event.
950 * @param string $event
951 * @return bool True if the definition should invalidate on the event. False otherwise.
953 public function invalidates_on_event($event) {
954 return (in_array($event, $this->invalidationevents
));
958 * Check if the definition has any invalidation events.
960 * @return bool True if it does, false otherwise
962 public function has_invalidation_events() {
963 return !empty($this->invalidationevents
);
967 * Returns all of the invalidation events for this definition.
971 public function get_invalidation_events() {
972 return $this->invalidationevents
;
976 * Returns a cache identification string.
978 * @return string A string to be used as part of keys.
980 protected function get_cache_identifier() {
981 $identifiers = array();
982 if ($this->selectedsharingoption
& self
::SHARING_ALL
) {
983 // Nothing to do here.
985 if ($this->selectedsharingoption
& self
::SHARING_SITEID
) {
986 $identifiers[] = cache_helper
::get_site_identifier();
988 if ($this->selectedsharingoption
& self
::SHARING_VERSION
) {
989 $identifiers[] = cache_helper
::get_site_version();
991 if ($this->selectedsharingoption
& self
::SHARING_INPUT
&& !empty($this->userinputsharingkey
)) {
992 $identifiers[] = $this->userinputsharingkey
;
995 return join('/', $identifiers);
999 * Returns true if this definition requires identifiers.
1003 public function has_required_identifiers() {
1004 return (count($this->requireidentifiers
) > 0);
1008 * Returns the possible sharing options that can be used with this defintion.
1012 public function get_sharing_options() {
1013 return $this->sharingoptions
;
1017 * Returns the user entered sharing key for this definition.
1021 public function get_user_input_sharing_key() {
1022 return $this->userinputsharingkey
;
1026 * Returns the user selected sharing option for this definition.
1030 public function get_selected_sharing_option() {
1031 return $this->selectedsharingoption
;