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/>.
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 main cache class.
34 * This class if the first class that any end developer will interact with.
35 * In order to create an instance of a cache that they can work with they must call one of the static make methods belonging
40 * @copyright 2012 Sam Hemelryk
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class cache implements cache_loader, cache_is_key_aware {
46 * We need a timestamp to use within the cache API.
47 * This stamp needs to be used for all ttl and time based operations to ensure that we don't end up with
51 protected static $now;
54 * The definition used when loading this cache if there was one.
55 * @var cache_definition
57 private $definition = false;
60 * The cache store that this loader will make use of.
66 * The next cache loader in the chain if there is one.
67 * If a cache request misses for the store belonging to this loader then the loader
68 * stored here will be checked next.
69 * If there is a loader here then $datasource must be false.
70 * @var cache_loader|false
72 private $loader = false;
75 * The data source to use if we need to load data (because if doesn't exist in the cache store).
76 * If there is a data source here then $loader above must be false.
77 * @var cache_data_source|false
79 private $datasource = false;
82 * Used to quickly check if the store supports key awareness.
83 * This is set when the cache is initialised and is used to speed up processing.
86 private $supportskeyawareness = null;
89 * Used to quickly check if the store supports ttl natively.
90 * This is set when the cache is initialised and is used to speed up processing.
93 private $supportsnativettl = null;
96 * Gets set to true if the cache is going to be using the build in static "persist" cache.
97 * The persist cache statically caches items used during the lifetime of the request. This greatly speeds up interaction
98 * with the cache in areas where it will be repetitively hit for the same information such as with strings.
99 * There are several other variables to control how this persist cache works.
102 private $persist = false;
105 * The persist cache itself.
106 * Items will be stored in this cache as they were provided. This ensure there is no unnecessary processing taking place.
109 private $persistcache = array();
112 * The number of items in the persist cache. Avoids count calls like you wouldn't believe.
115 private $persistcount = 0;
118 * An array containing just the keys being used in the persist cache.
119 * This seems redundant perhaps but is used when managing the size of the persist cache.
122 private $persistkeys = array();
125 * The maximum size of the persist cache. If set to false there is no max size.
126 * Caches that make use of the persist cache should seriously consider setting this to something reasonably small, but
127 * still large enough to offset repetitive calls.
130 private $persistmaxsize = false;
133 * Gets set to true during initialisation if the definition is making use of a ttl.
134 * Used to speed up processing.
137 private $hasattl = false;
140 * Gets set to the class name of the store during initialisation. This is used several times in the cache class internally
141 * and having it here helps speed up processing.
144 private $storetype = 'unknown';
147 * Gets set to true if we want to collect performance information about the cache API.
150 protected $perfdebug = false;
153 * Determines if this loader is a sub loader, not the top of the chain.
156 protected $subloader = false;
159 * Creates a new cache instance for a pre-defined definition.
161 * @param string $component The component for the definition
162 * @param string $area The area for the definition
163 * @param array $identifiers Any additional identifiers that should be provided to the definition.
164 * @param string $aggregate Super advanced feature. More docs later.
165 * @return cache_application|cache_session|cache_store
167 public static function make($component, $area, array $identifiers = array(), $aggregate = null) {
168 $factory = cache_factory::instance();
169 return $factory->create_cache_from_definition($component, $area, $identifiers, $aggregate);
173 * Creates a new cache instance based upon the given params.
175 * @param int $mode One of cache_store::MODE_*
176 * @param string $component The component this cache relates to.
177 * @param string $area The area this cache relates to.
178 * @param array $identifiers Any additional identifiers that should be provided to the definition.
179 * @param bool $persistent If set to true the cache will persist construction requests.
180 * @return cache_application|cache_session|cache_store
182 public static function make_from_params($mode, $component, $area, array $identifiers = array(), $persistent = false) {
183 $factory = cache_factory::instance();
184 return $factory->create_cache_from_params($mode, $component, $area, $identifiers, $persistent);
188 * Constructs a new cache instance.
190 * You should not call this method from your code, instead you should use the cache::make methods.
192 * This method is public so that the cache_factory is able to instantiate cache instances.
193 * Ideally we would make this method protected and expose its construction to the factory method internally somehow.
194 * The factory class is responsible for this in order to centralise the storage of instances once created. This way if needed
195 * we can force a reset of the cache API (used during unit testing).
197 * @param cache_definition $definition The definition for the cache instance.
198 * @param cache_store $store The store that cache should use.
199 * @param cache_loader|cache_data_source $loader The next loader in the chain or the data source if there is one and there
200 * are no other cache_loaders in the chain.
202 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
204 $this->definition = $definition;
205 $this->store = $store;
206 $this->storetype = get_class($store);
207 $this->perfdebug = !empty($CFG->perfdebug);
208 if ($loader instanceof cache_loader) {
209 $this->loader = $loader;
210 // Mark the loader as a sub (chained) loader.
211 $this->loader->set_is_sub_loader(true);
212 } else if ($loader instanceof cache_data_source) {
213 $this->datasource = $loader;
215 $this->definition->generate_definition_hash();
216 $this->persist = $this->definition->should_be_persistent();
217 if ($this->persist) {
218 $this->persistmaxsize = $this->definition->get_persistent_max_size();
220 $this->hasattl = ($this->definition->get_ttl() > 0);
224 * Used to inform the loader of its state as a sub loader, or as the top of the chain.
226 * This is important as it ensures that we do not have more than one loader keeping persistent data.
227 * Subloaders need to be "pure" loaders in the sense that they are used to store and retrieve information from stores or the
228 * next loader/data source in the chain.
229 * Nothing fancy, nothing flash.
231 * @param bool $setting
233 protected function set_is_sub_loader($setting = true) {
235 $this->subloader = true;
236 // Subloaders should not keep persistent data.
237 $this->persist = false;
238 $this->persistmaxsize = false;
240 $this->subloader = true;
241 $this->persist = $this->definition->should_be_persistent();
242 if ($this->persist) {
243 $this->persistmaxsize = $this->definition->get_persistent_max_size();
249 * Alters the identifiers that have been provided to the definition.
251 * This is an advanced method and should not be used unless really needed.
252 * It allows the developer to slightly alter the definition without having to re-establish the cache.
253 * It will cause more processing as the definition will need to clear and reprepare some of its properties.
255 * @param array $identifiers
257 public function set_identifiers(array $identifiers) {
258 $this->definition->set_identifiers($identifiers);
262 * Retrieves the value for the given key from the cache.
264 * @param string|int $key The key for the data being requested.
265 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
266 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
267 * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
268 * @return mixed|false The data from the cache or false if the key did not exist within the cache.
269 * @throws moodle_exception
271 public function get($key, $strictness = IGNORE_MISSING) {
273 $parsedkey = $this->parse_key($key);
274 // 2. Get it from the persist cache if we can (only when persist is enabled and it has already been requested/set).
275 $result = $this->get_from_persist_cache($parsedkey);
276 if ($result !== false) {
277 if ($this->perfdebug) {
278 cache_helper::record_cache_hit('** static persist **', $this->definition->get_id());
281 } else if ($this->perfdebug) {
282 cache_helper::record_cache_miss('** static persist **', $this->definition->get_id());
284 // 3. Get it from the store. Obviously wasn't in the persist cache.
285 $result = $this->store->get($parsedkey);
286 if ($result !== false) {
287 if ($result instanceof cache_ttl_wrapper) {
288 if ($result->has_expired()) {
289 $this->store->delete($parsedkey);
292 $result = $result->data;
295 if ($result instanceof cache_cached_object) {
296 $result = $result->restore_object();
298 if ($this->is_using_persist_cache()) {
299 $this->set_in_persist_cache($parsedkey, $result);
302 // 4. Load if from the loader/datasource if we don't already have it.
303 $setaftervalidation = false;
304 if ($result === false) {
305 if ($this->perfdebug) {
306 cache_helper::record_cache_miss($this->storetype, $this->definition->get_id());
308 if ($this->loader !== false) {
309 $result = $this->loader->get($parsedkey);
310 } else if ($this->datasource !== false) {
311 $result = $this->datasource->load_for_cache($key);
313 $setaftervalidation = ($result !== false);
314 } else if ($this->perfdebug) {
315 cache_helper::record_cache_hit($this->storetype, $this->definition->get_id());
317 // 5. Validate strictness.
318 if ($strictness === MUST_EXIST && $result === false) {
319 throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
321 // 6. Set it to the store if we got it from the loader/datasource.
322 if ($setaftervalidation) {
323 $this->set($key, $result);
329 * Retrieves an array of values for an array of keys.
331 * Using this function comes with potential performance implications.
332 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
333 * the equivalent singular method for each item provided.
334 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
335 * does support it, but you should be aware of this fact.
337 * @param array $keys The keys of the data being requested.
338 * Each key can be any structure although using a scalar string or int is recommended in the interests of performance.
339 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
340 * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
341 * @return array An array of key value pairs for the items that could be retrieved from the cache.
342 * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
343 * Otherwise any key that did not exist will have a data value of false within the results.
344 * @throws moodle_exception
346 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
348 $parsedkeys = array();
349 $resultpersist = array();
350 $resultstore = array();
351 $keystofind = array();
353 // First up check the persist cache for each key.
354 $isusingpersist = $this->is_using_persist_cache();
355 foreach ($keys as $key) {
356 $pkey = $this->parse_key($key);
357 $parsedkeys[$pkey] = $key;
358 $keystofind[$pkey] = $key;
359 if ($isusingpersist) {
360 $value = $this->get_from_persist_cache($pkey);
361 if ($value !== false) {
362 $resultpersist[$pkey] = $value;
363 unset($keystofind[$pkey]);
368 // Next assuming we didn't find all of the keys in the persist cache try loading them from the store.
369 if (count($keystofind)) {
370 $resultstore = $this->store->get_many(array_keys($keystofind));
371 // Process each item in the result to "unwrap" it.
372 foreach ($resultstore as $key => $value) {
373 if ($value instanceof cache_ttl_wrapper) {
374 if ($value->has_expired()) {
377 $value = $value->data;
380 if ($value instanceof cache_cached_object) {
381 $value = $value->restore_object();
383 $resultstore[$key] = $value;
387 // Merge the result from the persis cache with the results from the store load.
388 $result = $resultpersist + $resultstore;
389 unset($resultpersist);
392 // Next we need to find any missing values and load them from the loader/datasource next in the chain.
393 $usingloader = ($this->loader !== false);
394 $usingsource = (!$usingloader && ($this->datasource !== false));
395 if ($usingloader || $usingsource) {
396 $missingkeys = array();
397 foreach ($result as $key => $value) {
398 if ($value === false) {
399 $missingkeys[] = ($usingloader) ? $key : $parsedkeys[$key];
402 if (!empty($missingkeys)) {
404 $resultmissing = $this->loader->get_many($missingkeys);
406 $resultmissing = $this->datasource->load_many_for_cache($missingkeys);
408 foreach ($resultmissing as $key => $value) {
409 $result[$key] = $value;
410 if ($value !== false) {
411 $this->set($parsedkeys[$key], $value);
414 unset($resultmissing);
419 // Create an array with the original keys and the found values. This will be what we return.
420 $fullresult = array();
421 foreach ($result as $key => $value) {
422 $fullresult[$parsedkeys[$key]] = $value;
426 // Final step is to check strictness.
427 if ($strictness === MUST_EXIST) {
428 foreach ($keys as $key) {
429 if (!array_key_exists($key, $fullresult)) {
430 throw new moodle_exception('Not all the requested keys existed within the cache stores.');
435 // Return the result. Phew!
440 * Sends a key => value pair to the cache.
443 * // This code will add four entries to the cache, one for each url.
444 * $cache->set('main', 'http://moodle.org');
445 * $cache->set('docs', 'http://docs.moodle.org');
446 * $cache->set('tracker', 'http://tracker.moodle.org');
447 * $cache->set('qa', 'http://qa.moodle.net');
450 * @param string|int $key The key for the data being requested.
451 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
452 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
453 * @param mixed $data The data to set against the key.
454 * @return bool True on success, false otherwise.
456 public function set($key, $data) {
457 if ($this->perfdebug) {
458 cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
460 if (is_object($data) && $data instanceof cacheable_object) {
461 $data = new cache_cached_object($data);
462 } else if (!is_scalar($data)) {
463 // If data is an object it will be a reference.
464 // If data is an array if may contain references.
465 // We want to break references so that the cache cannot be modified outside of itself.
466 // Call the function to unreference it (in the best way possible).
467 $data = $this->unref($data);
469 if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
470 $data = new cache_ttl_wrapper($data, $this->definition->get_ttl());
472 $parsedkey = $this->parse_key($key);
473 if ($this->is_using_persist_cache()) {
474 $this->set_in_persist_cache($parsedkey, $data);
476 return $this->store->set($parsedkey, $data);
480 * Removes references where required.
482 * @param stdClass|array $data
484 protected function unref($data) {
485 // Check if it requires serialisation in order to produce a reference free copy.
486 if ($this->requires_serialisation($data)) {
487 // Damn, its going to have to be serialise.
488 $data = serialize($data);
489 // We unserialise immediately so that we don't have to do it every time on get.
490 $data = unserialize($data);
491 } else if (!is_scalar($data)) {
492 // Its safe to clone, lets do it, its going to beat the pants of serialisation.
493 $data = $this->deep_clone($data);
499 * Checks to see if a var requires serialisation.
501 * @param mixed $value The value to check.
502 * @param int $depth Used to ensure we don't enter an endless loop (think recursion).
503 * @return bool Returns true if the value is going to require serialisation in order to ensure a reference free copy
504 * or false if its safe to clone.
506 protected function requires_serialisation($value, $depth = 1) {
507 if (is_scalar($value)) {
509 } else if (is_array($value) || $value instanceof stdClass || $value instanceof Traversable) {
511 // Skrew it, mega-deep object, developer you suck, we're just going to serialise.
514 foreach ($value as $key => $subvalue) {
515 if ($this->requires_serialisation($subvalue, $depth++)) {
520 // Its not scalar, array, or stdClass so we'll need to serialise.
525 * Creates a reference free clone of the given value.
527 * @param mixed $value
530 protected function deep_clone($value) {
531 if (is_object($value)) {
532 // Objects must be cloned to begin with.
533 $value = clone $value;
535 if (is_array($value) || is_object($value)) {
536 foreach ($value as $key => $subvalue) {
537 $value[$key] = $this->deep_clone($subvalue);
544 * Sends several key => value pairs to the cache.
546 * Using this function comes with potential performance implications.
547 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
548 * the equivalent singular method for each item provided.
549 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
550 * does support it, but you should be aware of this fact.
553 * // This code will add four entries to the cache, one for each url.
554 * $cache->set_many(array(
555 * 'main' => 'http://moodle.org',
556 * 'docs' => 'http://docs.moodle.org',
557 * 'tracker' => 'http://tracker.moodle.org',
558 * 'qa' => ''http://qa.moodle.net'
562 * @param array $keyvaluearray An array of key => value pairs to send to the cache.
563 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
564 * ... if they care that is.
566 public function set_many(array $keyvaluearray) {
568 $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
569 $usepersistcache = $this->is_using_persist_cache();
570 foreach ($keyvaluearray as $key => $value) {
571 if (is_object($value) && $value instanceof cacheable_object) {
572 $value = new cache_cached_object($value);
575 $value = new cache_ttl_wrapper($value, $this->definition->get_ttl());
578 'key' => $this->parse_key($key),
581 if ($usepersistcache) {
582 $this->set_in_persist_cache($data[$key]['key'], $value);
585 if ($this->perfdebug) {
586 cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
588 return $this->store->set_many($data);
592 * Test is a cache has a key.
594 * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
595 * test and any subsequent action (get, set, delete etc).
596 * Instead it is recommended to write your code in such a way they it performs the following steps:
598 * <li>Attempt to retrieve the information.</li>
599 * <li>Generate the information.</li>
600 * <li>Attempt to set the information</li>
603 * Its also worth mentioning that not all stores support key tests.
604 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
605 * Just one more reason you should not use these methods unless you have a very good reason to do so.
607 * @param string|int $key
608 * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or
609 * data source then the code will try load the key value from the next item in the chain.
610 * @return bool True if the cache has the requested key, false otherwise.
612 public function has($key, $tryloadifpossible = false) {
613 $parsedkey = $this->parse_key($key);
614 if ($this->is_in_persist_cache($parsedkey)) {
617 if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
618 if ($this->store_supports_key_awareness() && !$this->store->has($parsedkey)) {
621 $data = $this->store->get($parsedkey);
622 if (!$this->store_supports_native_ttl()) {
623 $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
625 $has = ($data !== false);
628 $has = $this->store->has($parsedkey);
630 if (!$has && $tryloadifpossible) {
631 if ($this->loader !== false) {
632 $result = $this->loader->get($parsedkey);
633 } else if ($this->datasource !== null) {
634 $result = $this->datasource->load_for_cache($key);
636 $has = ($result !== null);
638 $this->set($key, $result);
645 * Test is a cache has all of the given keys.
647 * It is strongly recommended to avoid the use of this function if not absolutely required.
648 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
650 * Its also worth mentioning that not all stores support key tests.
651 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
652 * Just one more reason you should not use these methods unless you have a very good reason to do so.
655 * @return bool True if the cache has all of the given keys, false otherwise.
657 public function has_all(array $keys) {
658 if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
659 foreach ($keys as $key) {
660 if (!$this->has($key)) {
666 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
667 return $this->store->has_all($parsedkeys);
671 * Test if a cache has at least one of the given keys.
673 * It is strongly recommended to avoid the use of this function if not absolutely required.
674 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
676 * Its also worth mentioning that not all stores support key tests.
677 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
678 * Just one more reason you should not use these methods unless you have a very good reason to do so.
681 * @return bool True if the cache has at least one of the given keys
683 public function has_any(array $keys) {
684 if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
685 foreach ($keys as $key) {
686 if ($this->has($key)) {
693 if ($this->is_using_persist_cache()) {
694 $parsedkeys = array();
695 foreach ($keys as $id => $key) {
696 $parsedkey = $this->parse_key($key);
697 if ($this->is_in_persist_cache($parsedkey)) {
700 $parsedkeys[] = $parsedkey;
703 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
705 return $this->store->has_any($parsedkeys);
709 * Delete the given key from the cache.
711 * @param string|int $key The key to delete.
712 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
713 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
714 * @return bool True of success, false otherwise.
716 public function delete($key, $recurse = true) {
717 $parsedkey = $this->parse_key($key);
718 $this->delete_from_persist_cache($parsedkey);
719 if ($recurse && !empty($this->loader)) {
720 // Delete from the bottom of the stack first.
721 $this->loader->delete($key, $recurse);
723 return $this->store->delete($parsedkey);
727 * Delete all of the given keys from the cache.
729 * @param array $keys The key to delete.
730 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
731 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
732 * @return int The number of items successfully deleted.
734 public function delete_many(array $keys, $recurse = true) {
735 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
736 if ($this->is_using_persist_cache()) {
737 foreach ($parsedkeys as $parsedkey) {
738 $this->delete_from_persist_cache($parsedkey);
741 if ($recurse && !empty($this->loader)) {
742 // Delete from the bottom of the stack first.
743 $this->loader->delete_many($keys, $recurse);
745 return $this->store->delete_many($parsedkeys);
749 * Purges the cache store, and loader if there is one.
751 * @return bool True on success, false otherwise
753 public function purge() {
754 // 1. Purge the persist cache.
755 $this->persistcache = array();
756 // 2. Purge the store.
757 $this->store->purge();
758 // 3. Optionally pruge any stacked loaders.
760 $this->loader->purge();
766 * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
768 * @param string|int $key As passed to get|set|delete etc.
769 * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
771 protected function parse_key($key) {
772 if ($this->store->supports_multiple_indentifiers()) {
773 $result = $this->definition->generate_multi_key_parts();
774 $result['key'] = $key;
777 return cache_helper::hash_key($this->definition->generate_single_key_prefix().'-'.$key);
781 * Returns true if the cache is making use of a ttl.
784 protected function has_a_ttl() {
785 return $this->hasattl;
789 * Returns true if the cache store supports native ttl.
792 protected function store_supports_native_ttl() {
793 if ($this->supportsnativettl === null) {
794 $this->supportsnativettl = ($this->store->supports_native_ttl());
796 return $this->supportsnativettl;
800 * Returns the cache definition.
802 * @return cache_definition
804 protected function get_definition() {
805 return $this->definition;
809 * Returns the cache store
811 * @return cache_store
813 protected function get_store() {
818 * Returns true if the store supports key awareness.
822 protected function store_supports_key_awareness() {
823 if ($this->supportskeyawareness === null) {
824 $this->supportskeyawareness = ($this->store instanceof cache_is_key_aware);
826 return $this->supportskeyawareness;
830 * Returns true if the store natively supports locking.
834 protected function store_supports_native_locking() {
835 if ($this->nativelocking === null) {
836 $this->nativelocking = ($this->store instanceof cache_is_lockable);
838 return $this->nativelocking;
842 * Returns true if this cache is making use of the persist cache.
846 protected function is_using_persist_cache() {
847 return $this->persist;
851 * Returns true if the requested key exists within the persist cache.
853 * @param string $key The parsed key
856 protected function is_in_persist_cache($key) {
857 if (is_array($key)) {
860 // This could be written as a single line, however it has been split because the ttl check is faster than the instanceof
861 // and has_expired calls.
862 if (!$this->persist || !array_key_exists($key, $this->persistcache)) {
865 if ($this->has_a_ttl() && $this->store_supports_native_ttl()) {
866 return !($this->persistcache[$key] instanceof cache_ttl_wrapper && $this->persistcache[$key]->has_expired());
872 * Returns the item from the persist cache if it exists there.
874 * @param string $key The parsed key
875 * @return mixed|false The data from the persist cache or false if it wasn't there.
877 protected function get_from_persist_cache($key) {
878 if (is_array($key)) {
881 if (!$this->persist || !array_key_exists($key, $this->persistcache)) {
884 $data = $this->persistcache[$key];
885 if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) {
886 if ($data instanceof cache_cached_object) {
887 $data = $data->restore_object();
891 if ($data->has_expired()) {
892 $this->delete_from_persist_cache($key);
895 if ($data instanceof cache_cached_object) {
896 $data = $data->restore_object();
903 * Sets a key value pair into the persist cache.
905 * @param string $key The parsed key
909 protected function set_in_persist_cache($key, $data) {
910 if (is_array($key)) {
913 $this->persistcache[$key] = $data;
914 if ($this->persistmaxsize !== false) {
915 $this->persistcount++;
916 if ($this->persistcount > $this->persistmaxsize) {
917 $dropkey = array_shift($this->persistkeys);
918 unset($this->persistcache[$dropkey]);
919 $this->persistcount--;
926 * Deletes an item from the persist cache.
928 * @param string|int $key As given to get|set|delete
929 * @return bool True on success, false otherwise.
931 protected function delete_from_persist_cache($key) {
932 unset($this->persistcache[$key]);
933 if ($this->persistmaxsize !== false) {
934 $dropkey = array_search($key, $this->persistkeys);
936 unset($this->persistkeys[$dropkey]);
937 $this->persistcount--;
944 * Returns the timestamp from the first request for the time from the cache API.
946 * This stamp needs to be used for all ttl and time based operations to ensure that we don't end up with
951 public static function now() {
952 if (self::$now === null) {
960 * An application cache.
962 * This class is used for application caches returned by the cache::make methods.
963 * On top of the standard functionality it also allows locking to be required and or manually operated.
965 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
966 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
967 * instance of this class back again.
969 * @internal don't use me directly.
973 * @copyright 2012 Sam Hemelryk
974 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
976 class cache_application extends cache implements cache_loader_with_locking {
980 * This is used to ensure the lock belongs to the cache instance + definition + user.
983 protected $lockidentifier;
986 * Gets set to true if the cache's primary store natively supports locking.
987 * If it does then we use that, otherwise we need to instantiate a second store to use for locking.
990 protected $nativelocking = null;
993 * Gets set to true if the cache is going to be using locking.
994 * This isn't a requirement, it doesn't need to use locking (most won't) and this bool is used to quickly check things.
995 * If required then locking will be forced for the get|set|delete operation.
998 protected $requirelocking = false;
1001 * Gets set to true if the cache must use read locking (get|has).
1004 protected $requirelockingread = false;
1007 * Gets set to true if the cache must use write locking (set|delete)
1010 protected $requirelockingwrite = false;
1013 * Gets set to a cache_store to use for locking if the caches primary store doesn't support locking natively.
1014 * @var cache_lock_interface
1016 protected $cachelockinstance;
1019 * Overrides the cache construct method.
1021 * You should not call this method from your code, instead you should use the cache::make methods.
1023 * @param cache_definition $definition
1024 * @param cache_store $store
1025 * @param cache_loader|cache_data_source $loader
1027 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
1028 parent::__construct($definition, $store, $loader);
1029 $this->nativelocking = $this->store_supports_native_locking();
1030 if ($definition->require_locking()) {
1031 $this->requirelocking = true;
1032 $this->requirelockingread = $definition->require_locking_read();
1033 $this->requirelockingwrite = $definition->require_locking_write();
1036 if ($definition->has_invalidation_events()) {
1037 $lastinvalidation = $this->get('lastinvalidation');
1038 if ($lastinvalidation === false) {
1039 // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
1041 $this->set('lastinvalidation', cache::now());
1043 } else if ($lastinvalidation == cache::now()) {
1044 // We've already invalidated during this request.
1048 // Get the event invalidation cache.
1049 $cache = cache::make('core', 'eventinvalidation');
1050 $events = $cache->get_many($definition->get_invalidation_events());
1051 $todelete = array();
1052 // Iterate the returned data for the events.
1053 foreach ($events as $event => $keys) {
1054 // Look at each key and check the timestamp.
1055 foreach ($keys as $key => $timestamp) {
1056 // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
1057 // invalidation and now)then we need to invaliate the key.
1058 if ($timestamp >= $lastinvalidation) {
1063 if (!empty($todelete)) {
1064 $todelete = array_unique($todelete);
1065 $this->delete_many($todelete);
1067 // Set the time of the last invalidation.
1068 $this->set('lastinvalidation', cache::now());
1073 * Returns the identifier to use
1075 * @staticvar int $instances Counts the number of instances. Used as part of the lock identifier.
1078 public function get_identifier() {
1079 static $instances = 0;
1080 if ($this->lockidentifier === null) {
1081 $this->lockidentifier = md5(
1082 $this->get_definition()->generate_definition_hash() .
1088 return $this->lockidentifier;
1092 * Fixes the instance up after a clone.
1094 public function __clone() {
1095 // Force a new idenfitier.
1096 $this->lockidentifier = null;
1100 * Acquires a lock on the given key.
1102 * This is done automatically if the definition requires it.
1103 * It is recommended to use a definition if you want to have locking although it is possible to do locking without having
1104 * it required by the definition.
1105 * The problem with such an approach is that you cannot ensure that code will consistently use locking. You will need to
1106 * rely on the integrators review skills.
1108 * @param string|int $key The key as given to get|set|delete
1109 * @return bool Returns true if the lock could be acquired, false otherwise.
1111 public function acquire_lock($key) {
1112 $key = $this->parse_key($key);
1113 if ($this->nativelocking) {
1114 return $this->get_store()->acquire_lock($key, $this->get_identifier());
1116 $this->ensure_cachelock_available();
1117 return $this->cachelockinstance->lock($key, $this->get_identifier());
1122 * Checks if this cache has a lock on the given key.
1124 * @param string|int $key The key as given to get|set|delete
1125 * @return bool|null Returns true if there is a lock and this cache has it, null if no one has a lock on that key, false if
1126 * someone else has the lock.
1128 public function check_lock_state($key) {
1129 $key = $this->parse_key($key);
1130 if ($this->nativelocking) {
1131 return $this->get_store()->check_lock_state($key, $this->get_identifier());
1133 $this->ensure_cachelock_available();
1134 return $this->cachelockinstance->check_state($key, $this->get_identifier());
1139 * Releases the lock this cache has on the given key
1141 * @param string|int $key
1142 * @return bool True if the operation succeeded, false otherwise.
1144 public function release_lock($key) {
1145 $key = $this->parse_key($key);
1146 if ($this->nativelocking) {
1147 return $this->get_store()->release_lock($key, $this->get_identifier());
1149 $this->ensure_cachelock_available();
1150 return $this->cachelockinstance->unlock($key, $this->get_identifier());
1155 * Ensure that the dedicated lock store is ready to go.
1157 * This should only happen if the cache store doesn't natively support it.
1159 protected function ensure_cachelock_available() {
1160 if ($this->cachelockinstance === null) {
1161 $this->cachelockinstance = cache_helper::get_cachelock_for_store($this->get_store());
1166 * Sends a key => value pair to the cache.
1169 * // This code will add four entries to the cache, one for each url.
1170 * $cache->set('main', 'http://moodle.org');
1171 * $cache->set('docs', 'http://docs.moodle.org');
1172 * $cache->set('tracker', 'http://tracker.moodle.org');
1173 * $cache->set('qa', 'http://qa.moodle.net');
1176 * @param string|int $key The key for the data being requested.
1177 * @param mixed $data The data to set against the key.
1178 * @return bool True on success, false otherwise.
1180 public function set($key, $data) {
1181 if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
1184 $result = parent::set($key, $data);
1185 if ($this->requirelockingwrite && !$this->release_lock($key)) {
1186 debugging('Failed to release cache lock on set operation... this should not happen.', DEBUG_DEVELOPER);
1192 * Sends several key => value pairs to the cache.
1194 * Using this function comes with potential performance implications.
1195 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1196 * the equivalent singular method for each item provided.
1197 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1198 * does support it, but you should be aware of this fact.
1201 * // This code will add four entries to the cache, one for each url.
1202 * $cache->set_many(array(
1203 * 'main' => 'http://moodle.org',
1204 * 'docs' => 'http://docs.moodle.org',
1205 * 'tracker' => 'http://tracker.moodle.org',
1206 * 'qa' => ''http://qa.moodle.net'
1210 * @param array $keyvaluearray An array of key => value pairs to send to the cache.
1211 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
1212 * ... if they care that is.
1214 public function set_many(array $keyvaluearray) {
1215 if ($this->requirelockingwrite) {
1217 foreach ($keyvaluearray as $id => $pair) {
1218 $key = $pair['key'];
1219 if ($this->acquire_lock($key)) {
1222 unset($keyvaluearray[$id]);
1226 $result = parent::set_many($keyvaluearray);
1227 if ($this->requirelockingwrite) {
1228 foreach ($locks as $key) {
1229 if ($this->release_lock($key)) {
1230 debugging('Failed to release cache lock on set_many operation... this should not happen.', DEBUG_DEVELOPER);
1238 * Retrieves the value for the given key from the cache.
1240 * @param string|int $key The key for the data being requested.
1241 * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
1242 * @return mixed|false The data from the cache or false if the key did not exist within the cache.
1243 * @throws moodle_exception
1245 public function get($key, $strictness = IGNORE_MISSING) {
1246 if ($this->requirelockingread && $this->check_lock_state($key) === false) {
1247 // Read locking required and someone else has the read lock.
1250 return parent::get($key, $strictness);
1254 * Retrieves an array of values for an array of keys.
1256 * Using this function comes with potential performance implications.
1257 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1258 * the equivalent singular method for each item provided.
1259 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1260 * does support it, but you should be aware of this fact.
1262 * @param array $keys The keys of the data being requested.
1263 * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
1264 * @return array An array of key value pairs for the items that could be retrieved from the cache.
1265 * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
1266 * Otherwise any key that did not exist will have a data value of false within the results.
1267 * @throws moodle_exception
1269 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
1270 if ($this->requirelockingread) {
1271 foreach ($keys as $id => $key) {
1272 $lock =$this->acquire_lock($key);
1274 if ($strictness === MUST_EXIST) {
1275 throw new coding_exception('Could not acquire read locks for all of the items being requested.');
1277 // Can't return this as we couldn't get a read lock.
1284 return parent::get_many($keys, $strictness);
1288 * Delete the given key from the cache.
1290 * @param string|int $key The key to delete.
1291 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1292 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1293 * @return bool True of success, false otherwise.
1295 public function delete($key, $recurse = true) {
1296 if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
1299 $result = parent::delete($key, $recurse);
1300 if ($this->requirelockingwrite && !$this->release_lock($key)) {
1301 debugging('Failed to release cache lock on delete operation... this should not happen.', DEBUG_DEVELOPER);
1307 * Delete all of the given keys from the cache.
1309 * @param array $keys The key to delete.
1310 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1311 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1312 * @return int The number of items successfully deleted.
1314 public function delete_many(array $keys, $recurse = true) {
1315 if ($this->requirelockingwrite) {
1317 foreach ($keys as $id => $key) {
1318 if ($this->acquire_lock($key)) {
1325 $result = parent::delete_many($keys, $recurse);
1326 if ($this->requirelockingwrite) {
1327 foreach ($locks as $key) {
1328 if ($this->release_lock($key)) {
1329 debugging('Failed to release cache lock on delete_many operation... this should not happen.', DEBUG_DEVELOPER);
1340 * This class is used for session caches returned by the cache::make methods.
1342 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
1343 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
1344 * instance of this class back again.
1346 * @todo we should support locking in the session as well. Should be pretty simple to set up.
1348 * @internal don't use me directly.
1352 * @copyright 2012 Sam Hemelryk
1353 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1355 class cache_session extends cache {
1357 * Override the cache::construct method.
1359 * This function gets overriden so that we can process any invalidation events if need be.
1360 * If the definition doesn't have any invalidation events then this occurs exactly as it would for the cache class.
1361 * Otherwise we look at the last invalidation time and then check the invalidation data for events that have occured
1364 * You should not call this method from your code, instead you should use the cache::make methods.
1366 * @param cache_definition $definition
1367 * @param cache_store $store
1368 * @param cache_loader|cache_data_source $loader
1371 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
1372 parent::__construct($definition, $store, $loader);
1373 if ($definition->has_invalidation_events()) {
1374 $lastinvalidation = $this->get('lastsessioninvalidation');
1375 if ($lastinvalidation === false) {
1376 // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
1378 $this->set('lastsessioninvalidation', cache::now());
1380 } else if ($lastinvalidation == cache::now()) {
1381 // We've already invalidated during this request.
1385 // Get the event invalidation cache.
1386 $cache = cache::make('core', 'eventinvalidation');
1387 $events = $cache->get_many($definition->get_invalidation_events());
1388 $todelete = array();
1389 // Iterate the returned data for the events.
1390 foreach ($events as $event => $keys) {
1391 if ($keys === false) {
1392 // No data to be invalidated yet.
1395 // Look at each key and check the timestamp.
1396 foreach ($keys as $key => $timestamp) {
1397 // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
1398 // invalidation and now)then we need to invaliate the key.
1399 if ($timestamp >= $lastinvalidation) {
1404 if (!empty($todelete)) {
1405 $todelete = array_unique($todelete);
1406 $this->delete_many($todelete);
1408 // Set the time of the last invalidation.
1409 $this->set('lastsessioninvalidation', cache::now());
1417 * This class is used for request caches returned by the cache::make methods.
1419 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
1420 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
1421 * instance of this class back again.
1423 * @internal don't use me directly.
1427 * @copyright 2012 Sam Hemelryk
1428 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1430 class cache_request extends cache {
1431 // This comment appeases code pre-checker ;) !