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 required 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 {
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.
120 * Items are added to the end of the array and the when we need to reduce the size of the cache we use the
121 * key that is first on this array.
124 private $persistkeys = array();
127 * The maximum size of the persist cache. If set to false there is no max size.
128 * Caches that make use of the persist cache should seriously consider setting this to something reasonably small, but
129 * still large enough to offset repetitive calls.
132 private $persistmaxsize = false;
135 * Gets set to true during initialisation if the definition is making use of a ttl.
136 * Used to speed up processing.
139 private $hasattl = false;
142 * Gets set to the class name of the store during initialisation. This is used several times in the cache class internally
143 * and having it here helps speed up processing.
146 protected $storetype = 'unknown';
149 * Gets set to true if we want to collect performance information about the cache API.
152 protected $perfdebug = false;
155 * Determines if this loader is a sub loader, not the top of the chain.
158 protected $subloader = false;
161 * Creates a new cache instance for a pre-defined definition.
163 * @param string $component The component for the definition
164 * @param string $area The area for the definition
165 * @param array $identifiers Any additional identifiers that should be provided to the definition.
166 * @param string $aggregate Super advanced feature. More docs later.
167 * @return cache_application|cache_session|cache_store
169 public static function make($component, $area, array $identifiers = array(), $aggregate = null) {
170 $factory = cache_factory::instance();
171 return $factory->create_cache_from_definition($component, $area, $identifiers, $aggregate);
175 * Creates a new cache instance based upon the given params.
177 * @param int $mode One of cache_store::MODE_*
178 * @param string $component The component this cache relates to.
179 * @param string $area The area this cache relates to.
180 * @param array $identifiers Any additional identifiers that should be provided to the definition.
181 * @param array $options An array of options, available options are:
182 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
183 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
184 * - persistent : If set to true the cache will persist construction requests.
185 * @return cache_application|cache_session|cache_store
187 public static function make_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
188 $factory = cache_factory::instance();
189 return $factory->create_cache_from_params($mode, $component, $area, $identifiers, $options);
193 * Constructs a new cache instance.
195 * You should not call this method from your code, instead you should use the cache::make methods.
197 * This method is public so that the cache_factory is able to instantiate cache instances.
198 * Ideally we would make this method protected and expose its construction to the factory method internally somehow.
199 * The factory class is responsible for this in order to centralise the storage of instances once created. This way if needed
200 * we can force a reset of the cache API (used during unit testing).
202 * @param cache_definition $definition The definition for the cache instance.
203 * @param cache_store $store The store that cache should use.
204 * @param cache_loader|cache_data_source $loader The next loader in the chain or the data source if there is one and there
205 * are no other cache_loaders in the chain.
207 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
209 $this->definition = $definition;
210 $this->store = $store;
211 $this->storetype = get_class($store);
212 $this->perfdebug = !empty($CFG->perfdebug);
213 if ($loader instanceof cache_loader) {
214 $this->loader = $loader;
215 // Mark the loader as a sub (chained) loader.
216 $this->loader->set_is_sub_loader(true);
217 } else if ($loader instanceof cache_data_source) {
218 $this->datasource = $loader;
220 $this->definition->generate_definition_hash();
221 $this->persist = $this->definition->should_be_persistent();
222 if ($this->persist) {
223 $this->persistmaxsize = $this->definition->get_persistent_max_size();
225 $this->hasattl = ($this->definition->get_ttl() > 0);
229 * Used to inform the loader of its state as a sub loader, or as the top of the chain.
231 * This is important as it ensures that we do not have more than one loader keeping persistent data.
232 * Subloaders need to be "pure" loaders in the sense that they are used to store and retrieve information from stores or the
233 * next loader/data source in the chain.
234 * Nothing fancy, nothing flash.
236 * @param bool $setting
238 protected function set_is_sub_loader($setting = true) {
240 $this->subloader = true;
241 // Subloaders should not keep persistent data.
242 $this->persist = false;
243 $this->persistmaxsize = false;
245 $this->subloader = true;
246 $this->persist = $this->definition->should_be_persistent();
247 if ($this->persist) {
248 $this->persistmaxsize = $this->definition->get_persistent_max_size();
254 * Alters the identifiers that have been provided to the definition.
256 * This is an advanced method and should not be used unless really needed.
257 * It allows the developer to slightly alter the definition without having to re-establish the cache.
258 * It will cause more processing as the definition will need to clear and reprepare some of its properties.
260 * @param array $identifiers
262 public function set_identifiers(array $identifiers) {
263 $this->definition->set_identifiers($identifiers);
267 * Retrieves the value for the given key from the cache.
269 * @param string|int $key The key for the data being requested.
270 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
271 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
272 * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
273 * @return mixed|false The data from the cache or false if the key did not exist within the cache.
274 * @throws moodle_exception
276 public function get($key, $strictness = IGNORE_MISSING) {
278 $parsedkey = $this->parse_key($key);
279 // 2. Get it from the persist cache if we can (only when persist is enabled and it has already been requested/set).
281 if ($this->is_using_persist_cache()) {
282 $result = $this->get_from_persist_cache($parsedkey);
284 if ($result !== false) {
285 if (!is_scalar($result)) {
286 // If data is an object it will be a reference.
287 // If data is an array if may contain references.
288 // We want to break references so that the cache cannot be modified outside of itself.
289 // Call the function to unreference it (in the best way possible).
290 $result = $this->unref($result);
294 // 3. Get it from the store. Obviously wasn't in the persist cache.
295 $result = $this->store->get($parsedkey);
296 if ($result !== false) {
297 if ($result instanceof cache_ttl_wrapper) {
298 if ($result->has_expired()) {
299 $this->store->delete($parsedkey);
302 $result = $result->data;
305 if ($result instanceof cache_cached_object) {
306 $result = $result->restore_object();
308 if ($this->is_using_persist_cache()) {
309 $this->set_in_persist_cache($parsedkey, $result);
312 // 4. Load if from the loader/datasource if we don't already have it.
313 $setaftervalidation = false;
314 if ($result === false) {
315 if ($this->perfdebug) {
316 cache_helper::record_cache_miss($this->storetype, $this->definition->get_id());
318 if ($this->loader !== false) {
319 // We must pass the original (unparsed) key to the next loader in the chain.
320 // The next loader will parse the key as it sees fit. It may be parsed differently
321 // depending upon the capabilities of the store associated with the loader.
322 $result = $this->loader->get($key);
323 } else if ($this->datasource !== false) {
324 $result = $this->datasource->load_for_cache($key);
326 $setaftervalidation = ($result !== false);
327 } else if ($this->perfdebug) {
328 cache_helper::record_cache_hit($this->storetype, $this->definition->get_id());
330 // 5. Validate strictness.
331 if ($strictness === MUST_EXIST && $result === false) {
332 throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
334 // 6. Set it to the store if we got it from the loader/datasource.
335 if ($setaftervalidation) {
336 $this->set($key, $result);
338 // 7. Make sure we don't pass back anything that could be a reference.
339 // We don't want people modifying the data in the cache.
340 if (!is_scalar($result)) {
341 // If data is an object it will be a reference.
342 // If data is an array if may contain references.
343 // We want to break references so that the cache cannot be modified outside of itself.
344 // Call the function to unreference it (in the best way possible).
345 $result = $this->unref($result);
351 * Retrieves an array of values for an array of keys.
353 * Using this function comes with potential performance implications.
354 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
355 * the equivalent singular method for each item provided.
356 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
357 * does support it, but you should be aware of this fact.
359 * @param array $keys The keys of the data being requested.
360 * Each key can be any structure although using a scalar string or int is recommended in the interests of performance.
361 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
362 * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
363 * @return array An array of key value pairs for the items that could be retrieved from the cache.
364 * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
365 * Otherwise any key that did not exist will have a data value of false within the results.
366 * @throws moodle_exception
368 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
370 $keysparsed = array();
371 $parsedkeys = array();
372 $resultpersist = array();
373 $resultstore = array();
374 $keystofind = array();
376 // First up check the persist cache for each key.
377 $isusingpersist = $this->is_using_persist_cache();
378 foreach ($keys as $key) {
379 $pkey = $this->parse_key($key);
380 $keysparsed[$key] = $pkey;
381 $parsedkeys[$pkey] = $key;
382 $keystofind[$pkey] = $key;
383 if ($isusingpersist) {
384 $value = $this->get_from_persist_cache($pkey);
385 if ($value !== false) {
386 $resultpersist[$pkey] = $value;
387 unset($keystofind[$pkey]);
392 // Next assuming we didn't find all of the keys in the persist cache try loading them from the store.
393 if (count($keystofind)) {
394 $resultstore = $this->store->get_many(array_keys($keystofind));
395 // Process each item in the result to "unwrap" it.
396 foreach ($resultstore as $key => $value) {
397 if ($value instanceof cache_ttl_wrapper) {
398 if ($value->has_expired()) {
401 $value = $value->data;
404 if ($value instanceof cache_cached_object) {
405 $value = $value->restore_object();
407 $resultstore[$key] = $value;
411 // Merge the result from the persis cache with the results from the store load.
412 $result = $resultpersist + $resultstore;
413 unset($resultpersist);
416 // Next we need to find any missing values and load them from the loader/datasource next in the chain.
417 $usingloader = ($this->loader !== false);
418 $usingsource = (!$usingloader && ($this->datasource !== false));
419 if ($usingloader || $usingsource) {
420 $missingkeys = array();
421 foreach ($result as $key => $value) {
422 if ($value === false) {
423 $missingkeys[] = ($usingloader) ? $key : $parsedkeys[$key];
426 if (!empty($missingkeys)) {
428 $resultmissing = $this->loader->get_many($missingkeys);
430 $resultmissing = $this->datasource->load_many_for_cache($missingkeys);
432 foreach ($resultmissing as $key => $value) {
433 $pkey = ($usingloader) ? $key : $keysparsed[$key];
434 $realkey = ($usingloader) ? $parsedkeys[$key] : $key;
435 $result[$pkey] = $value;
436 if ($value !== false) {
437 $this->set($realkey, $value);
440 unset($resultmissing);
445 // Create an array with the original keys and the found values. This will be what we return.
446 $fullresult = array();
447 foreach ($result as $key => $value) {
448 $fullresult[$parsedkeys[$key]] = $value;
452 // Final step is to check strictness.
453 if ($strictness === MUST_EXIST) {
454 foreach ($keys as $key) {
455 if (!array_key_exists($key, $fullresult)) {
456 throw new moodle_exception('Not all the requested keys existed within the cache stores.');
461 // Return the result. Phew!
466 * Sends a key => value pair to the cache.
469 * // This code will add four entries to the cache, one for each url.
470 * $cache->set('main', 'http://moodle.org');
471 * $cache->set('docs', 'http://docs.moodle.org');
472 * $cache->set('tracker', 'http://tracker.moodle.org');
473 * $cache->set('qa', 'http://qa.moodle.net');
476 * @param string|int $key The key for the data being requested.
477 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
478 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
479 * @param mixed $data The data to set against the key.
480 * @return bool True on success, false otherwise.
482 public function set($key, $data) {
483 if ($this->perfdebug) {
484 cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
486 if (is_object($data) && $data instanceof cacheable_object) {
487 $data = new cache_cached_object($data);
488 } else if (!is_scalar($data)) {
489 // If data is an object it will be a reference.
490 // If data is an array if may contain references.
491 // We want to break references so that the cache cannot be modified outside of itself.
492 // Call the function to unreference it (in the best way possible).
493 $data = $this->unref($data);
495 if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
496 $data = new cache_ttl_wrapper($data, $this->definition->get_ttl());
498 $parsedkey = $this->parse_key($key);
499 if ($this->is_using_persist_cache()) {
500 $this->set_in_persist_cache($parsedkey, $data);
502 return $this->store->set($parsedkey, $data);
506 * Removes references where required.
508 * @param stdClass|array $data
510 protected function unref($data) {
511 if ($this->definition->uses_simple_data()) {
514 // Check if it requires serialisation in order to produce a reference free copy.
515 if ($this->requires_serialisation($data)) {
516 // Damn, its going to have to be serialise.
517 $data = serialize($data);
518 // We unserialise immediately so that we don't have to do it every time on get.
519 $data = unserialize($data);
520 } else if (!is_scalar($data)) {
521 // Its safe to clone, lets do it, its going to beat the pants of serialisation.
522 $data = $this->deep_clone($data);
528 * Checks to see if a var requires serialisation.
530 * @param mixed $value The value to check.
531 * @param int $depth Used to ensure we don't enter an endless loop (think recursion).
532 * @return bool Returns true if the value is going to require serialisation in order to ensure a reference free copy
533 * or false if its safe to clone.
535 protected function requires_serialisation($value, $depth = 1) {
536 if (is_scalar($value)) {
538 } else if (is_array($value) || $value instanceof stdClass || $value instanceof Traversable) {
540 // Skrew it, mega-deep object, developer you suck, we're just going to serialise.
543 foreach ($value as $key => $subvalue) {
544 if ($this->requires_serialisation($subvalue, $depth++)) {
549 // Its not scalar, array, or stdClass so we'll need to serialise.
554 * Creates a reference free clone of the given value.
556 * @param mixed $value
559 protected function deep_clone($value) {
560 if (is_object($value)) {
561 // Objects must be cloned to begin with.
562 $value = clone $value;
564 if (is_array($value) || is_object($value)) {
565 foreach ($value as $key => $subvalue) {
566 $value[$key] = $this->deep_clone($subvalue);
573 * Sends several key => value pairs to the cache.
575 * Using this function comes with potential performance implications.
576 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
577 * the equivalent singular method for each item provided.
578 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
579 * does support it, but you should be aware of this fact.
582 * // This code will add four entries to the cache, one for each url.
583 * $cache->set_many(array(
584 * 'main' => 'http://moodle.org',
585 * 'docs' => 'http://docs.moodle.org',
586 * 'tracker' => 'http://tracker.moodle.org',
587 * 'qa' => ''http://qa.moodle.net'
591 * @param array $keyvaluearray An array of key => value pairs to send to the cache.
592 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
593 * ... if they care that is.
595 public function set_many(array $keyvaluearray) {
597 $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
598 $usepersistcache = $this->is_using_persist_cache();
599 foreach ($keyvaluearray as $key => $value) {
600 if (is_object($value) && $value instanceof cacheable_object) {
601 $value = new cache_cached_object($value);
602 } else if (!is_scalar($value)) {
603 // If data is an object it will be a reference.
604 // If data is an array if may contain references.
605 // We want to break references so that the cache cannot be modified outside of itself.
606 // Call the function to unreference it (in the best way possible).
607 $value = $this->unref($value);
610 $value = new cache_ttl_wrapper($value, $this->definition->get_ttl());
613 'key' => $this->parse_key($key),
616 if ($usepersistcache) {
617 $this->set_in_persist_cache($data[$key]['key'], $value);
620 if ($this->perfdebug) {
621 cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
623 return $this->store->set_many($data);
627 * Test is a cache has a key.
629 * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
630 * test and any subsequent action (get, set, delete etc).
631 * Instead it is recommended to write your code in such a way they it performs the following steps:
633 * <li>Attempt to retrieve the information.</li>
634 * <li>Generate the information.</li>
635 * <li>Attempt to set the information</li>
638 * Its also worth mentioning that not all stores support key tests.
639 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
640 * Just one more reason you should not use these methods unless you have a very good reason to do so.
642 * @param string|int $key
643 * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or
644 * data source then the code will try load the key value from the next item in the chain.
645 * @return bool True if the cache has the requested key, false otherwise.
647 public function has($key, $tryloadifpossible = false) {
648 $parsedkey = $this->parse_key($key);
649 if ($this->is_in_persist_cache($parsedkey)) {
650 // Hoorah, that was easy. It exists in the persist cache so we definitely have it.
653 if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
654 // The data has a TTL and the store doesn't support it natively.
655 // We must fetch the data and expect a ttl wrapper.
656 $data = $this->store->get($parsedkey);
657 $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
658 } else if (!$this->store_supports_key_awareness()) {
659 // The store doesn't support key awareness, get the data and check it manually... puke.
660 // Either no TTL is set of the store supports its handling natively.
661 $data = $this->store->get($parsedkey);
662 $has = ($data !== false);
664 // The store supports key awareness, this is easy!
665 // Either no TTL is set of the store supports its handling natively.
666 $has = $this->store->has($parsedkey);
668 if (!$has && $tryloadifpossible) {
669 if ($this->loader !== false) {
670 $result = $this->loader->get($parsedkey);
671 } else if ($this->datasource !== null) {
672 $result = $this->datasource->load_for_cache($key);
674 $has = ($result !== null);
676 $this->set($key, $result);
683 * Test is a cache has all of the given keys.
685 * It is strongly recommended to avoid the use of this function if not absolutely required.
686 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
688 * Its also worth mentioning that not all stores support key tests.
689 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
690 * Just one more reason you should not use these methods unless you have a very good reason to do so.
693 * @return bool True if the cache has all of the given keys, false otherwise.
695 public function has_all(array $keys) {
696 if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
697 foreach ($keys as $key) {
698 if (!$this->has($key)) {
704 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
705 return $this->store->has_all($parsedkeys);
709 * Test if a cache has at least one of the given keys.
711 * It is strongly recommended to avoid the use of this function if not absolutely required.
712 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
714 * Its also worth mentioning that not all stores support key tests.
715 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
716 * Just one more reason you should not use these methods unless you have a very good reason to do so.
719 * @return bool True if the cache has at least one of the given keys
721 public function has_any(array $keys) {
722 if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
723 foreach ($keys as $key) {
724 if ($this->has($key)) {
731 if ($this->is_using_persist_cache()) {
732 $parsedkeys = array();
733 foreach ($keys as $id => $key) {
734 $parsedkey = $this->parse_key($key);
735 if ($this->is_in_persist_cache($parsedkey)) {
738 $parsedkeys[] = $parsedkey;
741 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
743 return $this->store->has_any($parsedkeys);
747 * Delete the given key from the cache.
749 * @param string|int $key The key to delete.
750 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
751 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
752 * @return bool True of success, false otherwise.
754 public function delete($key, $recurse = true) {
755 $parsedkey = $this->parse_key($key);
756 $this->delete_from_persist_cache($parsedkey);
757 if ($recurse && $this->loader !== false) {
758 // Delete from the bottom of the stack first.
759 $this->loader->delete($key, $recurse);
761 return $this->store->delete($parsedkey);
765 * Delete all of the given keys from the cache.
767 * @param array $keys The key to delete.
768 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
769 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
770 * @return int The number of items successfully deleted.
772 public function delete_many(array $keys, $recurse = true) {
773 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
774 if ($this->is_using_persist_cache()) {
775 foreach ($parsedkeys as $parsedkey) {
776 $this->delete_from_persist_cache($parsedkey);
779 if ($recurse && $this->loader !== false) {
780 // Delete from the bottom of the stack first.
781 $this->loader->delete_many($keys, $recurse);
783 return $this->store->delete_many($parsedkeys);
787 * Purges the cache store, and loader if there is one.
789 * @return bool True on success, false otherwise
791 public function purge() {
792 // 1. Purge the persist cache.
793 $this->persistcache = array();
794 // 2. Purge the store.
795 $this->store->purge();
796 // 3. Optionally pruge any stacked loaders.
798 $this->loader->purge();
804 * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
806 * @param string|int $key As passed to get|set|delete etc.
807 * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
809 protected function parse_key($key) {
810 // First up if the store supports multiple keys we'll go with that.
811 if ($this->store->supports_multiple_identifiers()) {
812 $result = $this->definition->generate_multi_key_parts();
813 $result['key'] = $key;
816 // If not we need to generate a hash and to for that we use the cache_helper.
817 return cache_helper::hash_key($key, $this->definition);
821 * Returns true if the cache is making use of a ttl.
824 protected function has_a_ttl() {
825 return $this->hasattl;
829 * Returns true if the cache store supports native ttl.
832 protected function store_supports_native_ttl() {
833 if ($this->supportsnativettl === null) {
834 $this->supportsnativettl = ($this->store->supports_native_ttl());
836 return $this->supportsnativettl;
840 * Returns the cache definition.
842 * @return cache_definition
844 protected function get_definition() {
845 return $this->definition;
849 * Returns the cache store
851 * @return cache_store
853 protected function get_store() {
858 * Returns the loader associated with this instance.
861 * @return cache_loader|false
863 protected function get_loader() {
864 return $this->loader;
868 * Returns the data source associated with this cache.
871 * @return cache_data_source|false
873 protected function get_datasource() {
874 return $this->datasource;
878 * Returns true if the store supports key awareness.
882 protected function store_supports_key_awareness() {
883 if ($this->supportskeyawareness === null) {
884 $this->supportskeyawareness = ($this->store instanceof cache_is_key_aware);
886 return $this->supportskeyawareness;
890 * Returns true if the store natively supports locking.
894 protected function store_supports_native_locking() {
895 if ($this->nativelocking === null) {
896 $this->nativelocking = ($this->store instanceof cache_is_lockable);
898 return $this->nativelocking;
902 * Returns true if this cache is making use of the persist cache.
906 protected function is_using_persist_cache() {
907 return $this->persist;
911 * Returns true if the requested key exists within the persist cache.
913 * @param string $key The parsed key
916 protected function is_in_persist_cache($key) {
917 // This method of checking if an array was supplied is faster than is_array.
918 if ($key === (array)$key) {
921 // This could be written as a single line, however it has been split because the ttl check is faster than the instanceof
922 // and has_expired calls.
923 if (!$this->persist || !array_key_exists($key, $this->persistcache)) {
926 if ($this->has_a_ttl() && $this->store_supports_native_ttl()) {
927 return !($this->persistcache[$key] instanceof cache_ttl_wrapper && $this->persistcache[$key]->has_expired());
933 * Returns the item from the persist cache if it exists there.
935 * @param string $key The parsed key
936 * @return mixed|false The data from the persist cache or false if it wasn't there.
938 protected function get_from_persist_cache($key) {
939 // This method of checking if an array was supplied is faster than is_array.
940 if ($key === (array)$key) {
943 // This isset check is faster than array_key_exists but will return false
944 // for null values, meaning null values will come from backing store not
945 // the persist cache. We think this okay because null usage should be
946 // very rare (see comment in MDL-39472).
947 if (!$this->persist || !isset($this->persistcache[$key])) {
950 $data = $this->persistcache[$key];
951 if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) {
952 if ($data instanceof cache_cached_object) {
953 $data = $data->restore_object();
956 } else if ($data->has_expired()) {
957 $this->delete_from_persist_cache($key);
960 if ($data instanceof cache_cached_object) {
961 $data = $data->restore_object();
963 $result = $data->data;
967 if ($this->perfdebug) {
968 cache_helper::record_cache_hit('** static persist **', $this->definition->get_id());
970 if ($this->persistmaxsize > 1 && $this->persistcount > 1) {
971 // Check to see if this is the last item on the persist keys array.
972 if (end($this->persistkeys) !== $key) {
973 // It isn't the last item.
974 // Move the item to the end of the array so that it is last to be removed.
975 unset($this->persistkeys[$key]);
976 $this->persistkeys[$key] = $key;
981 if ($this->perfdebug) {
982 cache_helper::record_cache_miss('** static persist **', $this->definition->get_id());
989 * Sets a key value pair into the persist cache.
991 * @param string $key The parsed key
995 protected function set_in_persist_cache($key, $data) {
996 // This method of checking if an array was supplied is faster than is_array.
997 if ($key === (array)$key) {
1000 $this->persistcache[$key] = $data;
1001 if ($this->persistmaxsize !== false) {
1002 $this->persistcount++;
1003 $this->persistkeys[$key] = $key;
1004 if ($this->persistcount > $this->persistmaxsize) {
1005 $dropkey = array_shift($this->persistkeys);
1006 unset($this->persistcache[$dropkey]);
1007 $this->persistcount--;
1014 * Deletes an item from the persist cache.
1016 * @param string|int $key As given to get|set|delete
1017 * @return bool True on success, false otherwise.
1019 protected function delete_from_persist_cache($key) {
1020 unset($this->persistcache[$key]);
1021 if ($this->persistmaxsize !== false) {
1022 $dropkey = array_search($key, $this->persistkeys);
1024 unset($this->persistkeys[$dropkey]);
1025 $this->persistcount--;
1032 * Returns the timestamp from the first request for the time from the cache API.
1034 * This stamp needs to be used for all ttl and time based operations to ensure that we don't end up with
1039 public static function now() {
1040 if (self::$now === null) {
1041 self::$now = time();
1048 * An application cache.
1050 * This class is used for application caches returned by the cache::make methods.
1051 * On top of the standard functionality it also allows locking to be required and or manually operated.
1053 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
1054 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
1055 * instance of this class back again.
1057 * @internal don't use me directly.
1061 * @copyright 2012 Sam Hemelryk
1062 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1064 class cache_application extends cache implements cache_loader_with_locking {
1068 * This is used to ensure the lock belongs to the cache instance + definition + user.
1071 protected $lockidentifier;
1074 * Gets set to true if the cache's primary store natively supports locking.
1075 * If it does then we use that, otherwise we need to instantiate a second store to use for locking.
1078 protected $nativelocking = null;
1081 * Gets set to true if the cache is going to be using locking.
1082 * This isn't a requirement, it doesn't need to use locking (most won't) and this bool is used to quickly check things.
1083 * If required then locking will be forced for the get|set|delete operation.
1086 protected $requirelocking = false;
1089 * Gets set to true if the cache must use read locking (get|has).
1092 protected $requirelockingread = false;
1095 * Gets set to true if the cache must use write locking (set|delete)
1098 protected $requirelockingwrite = false;
1101 * Gets set to a cache_store to use for locking if the caches primary store doesn't support locking natively.
1102 * @var cache_lock_interface
1104 protected $cachelockinstance;
1107 * Overrides the cache construct method.
1109 * You should not call this method from your code, instead you should use the cache::make methods.
1111 * @param cache_definition $definition
1112 * @param cache_store $store
1113 * @param cache_loader|cache_data_source $loader
1115 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
1116 parent::__construct($definition, $store, $loader);
1117 $this->nativelocking = $this->store_supports_native_locking();
1118 if ($definition->require_locking()) {
1119 $this->requirelocking = true;
1120 $this->requirelockingread = $definition->require_locking_read();
1121 $this->requirelockingwrite = $definition->require_locking_write();
1124 if ($definition->has_invalidation_events()) {
1125 $lastinvalidation = $this->get('lastinvalidation');
1126 if ($lastinvalidation === false) {
1127 // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
1129 $this->set('lastinvalidation', cache::now());
1131 } else if ($lastinvalidation == cache::now()) {
1132 // We've already invalidated during this request.
1136 // Get the event invalidation cache.
1137 $cache = cache::make('core', 'eventinvalidation');
1138 $events = $cache->get_many($definition->get_invalidation_events());
1139 $todelete = array();
1141 // Iterate the returned data for the events.
1142 foreach ($events as $event => $keys) {
1143 if ($keys === false) {
1144 // No data to be invalidated yet.
1147 // Look at each key and check the timestamp.
1148 foreach ($keys as $key => $timestamp) {
1149 // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
1150 // invalidation and now)then we need to invaliate the key.
1151 if ($timestamp >= $lastinvalidation) {
1152 if ($key === 'purged') {
1163 } else if (!empty($todelete)) {
1164 $todelete = array_unique($todelete);
1165 $this->delete_many($todelete);
1167 // Set the time of the last invalidation.
1168 $this->set('lastinvalidation', cache::now());
1173 * Returns the identifier to use
1175 * @staticvar int $instances Counts the number of instances. Used as part of the lock identifier.
1178 public function get_identifier() {
1179 static $instances = 0;
1180 if ($this->lockidentifier === null) {
1181 $this->lockidentifier = md5(
1182 $this->get_definition()->generate_definition_hash() .
1188 return $this->lockidentifier;
1192 * Fixes the instance up after a clone.
1194 public function __clone() {
1195 // Force a new idenfitier.
1196 $this->lockidentifier = null;
1200 * Acquires a lock on the given key.
1202 * This is done automatically if the definition requires it.
1203 * It is recommended to use a definition if you want to have locking although it is possible to do locking without having
1204 * it required by the definition.
1205 * The problem with such an approach is that you cannot ensure that code will consistently use locking. You will need to
1206 * rely on the integrators review skills.
1208 * @param string|int $key The key as given to get|set|delete
1209 * @return bool Returns true if the lock could be acquired, false otherwise.
1211 public function acquire_lock($key) {
1212 $key = $this->parse_key($key);
1213 if ($this->nativelocking) {
1214 return $this->get_store()->acquire_lock($key, $this->get_identifier());
1216 $this->ensure_cachelock_available();
1217 return $this->cachelockinstance->lock($key, $this->get_identifier());
1222 * Checks if this cache has a lock on the given key.
1224 * @param string|int $key The key as given to get|set|delete
1225 * @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
1226 * someone else has the lock.
1228 public function check_lock_state($key) {
1229 $key = $this->parse_key($key);
1230 if ($this->nativelocking) {
1231 return $this->get_store()->check_lock_state($key, $this->get_identifier());
1233 $this->ensure_cachelock_available();
1234 return $this->cachelockinstance->check_state($key, $this->get_identifier());
1239 * Releases the lock this cache has on the given key
1241 * @param string|int $key
1242 * @return bool True if the operation succeeded, false otherwise.
1244 public function release_lock($key) {
1245 $key = $this->parse_key($key);
1246 if ($this->nativelocking) {
1247 return $this->get_store()->release_lock($key, $this->get_identifier());
1249 $this->ensure_cachelock_available();
1250 return $this->cachelockinstance->unlock($key, $this->get_identifier());
1255 * Ensure that the dedicated lock store is ready to go.
1257 * This should only happen if the cache store doesn't natively support it.
1259 protected function ensure_cachelock_available() {
1260 if ($this->cachelockinstance === null) {
1261 $this->cachelockinstance = cache_helper::get_cachelock_for_store($this->get_store());
1266 * Sends a key => value pair to the cache.
1269 * // This code will add four entries to the cache, one for each url.
1270 * $cache->set('main', 'http://moodle.org');
1271 * $cache->set('docs', 'http://docs.moodle.org');
1272 * $cache->set('tracker', 'http://tracker.moodle.org');
1273 * $cache->set('qa', 'http://qa.moodle.net');
1276 * @param string|int $key The key for the data being requested.
1277 * @param mixed $data The data to set against the key.
1278 * @return bool True on success, false otherwise.
1280 public function set($key, $data) {
1281 if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
1284 $result = parent::set($key, $data);
1285 if ($this->requirelockingwrite && !$this->release_lock($key)) {
1286 debugging('Failed to release cache lock on set operation... this should not happen.', DEBUG_DEVELOPER);
1292 * Sends several key => value pairs to the cache.
1294 * Using this function comes with potential performance implications.
1295 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1296 * the equivalent singular method for each item provided.
1297 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1298 * does support it, but you should be aware of this fact.
1301 * // This code will add four entries to the cache, one for each url.
1302 * $cache->set_many(array(
1303 * 'main' => 'http://moodle.org',
1304 * 'docs' => 'http://docs.moodle.org',
1305 * 'tracker' => 'http://tracker.moodle.org',
1306 * 'qa' => ''http://qa.moodle.net'
1310 * @param array $keyvaluearray An array of key => value pairs to send to the cache.
1311 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
1312 * ... if they care that is.
1314 public function set_many(array $keyvaluearray) {
1315 if ($this->requirelockingwrite) {
1317 foreach ($keyvaluearray as $id => $pair) {
1318 $key = $pair['key'];
1319 if ($this->acquire_lock($key)) {
1322 unset($keyvaluearray[$id]);
1326 $result = parent::set_many($keyvaluearray);
1327 if ($this->requirelockingwrite) {
1328 foreach ($locks as $key) {
1329 if ($this->release_lock($key)) {
1330 debugging('Failed to release cache lock on set_many operation... this should not happen.', DEBUG_DEVELOPER);
1338 * Retrieves the value for the given key from the cache.
1340 * @param string|int $key The key for the data being requested.
1341 * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
1342 * @return mixed|false The data from the cache or false if the key did not exist within the cache.
1343 * @throws moodle_exception
1345 public function get($key, $strictness = IGNORE_MISSING) {
1346 if ($this->requirelockingread && $this->check_lock_state($key) === false) {
1347 // Read locking required and someone else has the read lock.
1350 return parent::get($key, $strictness);
1354 * Retrieves an array of values for an array of keys.
1356 * Using this function comes with potential performance implications.
1357 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1358 * the equivalent singular method for each item provided.
1359 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1360 * does support it, but you should be aware of this fact.
1362 * @param array $keys The keys of the data being requested.
1363 * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
1364 * @return array An array of key value pairs for the items that could be retrieved from the cache.
1365 * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
1366 * Otherwise any key that did not exist will have a data value of false within the results.
1367 * @throws moodle_exception
1369 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
1370 if ($this->requirelockingread) {
1371 foreach ($keys as $id => $key) {
1372 $lock =$this->acquire_lock($key);
1374 if ($strictness === MUST_EXIST) {
1375 throw new coding_exception('Could not acquire read locks for all of the items being requested.');
1377 // Can't return this as we couldn't get a read lock.
1384 return parent::get_many($keys, $strictness);
1388 * Delete the given key from the cache.
1390 * @param string|int $key The key to delete.
1391 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1392 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1393 * @return bool True of success, false otherwise.
1395 public function delete($key, $recurse = true) {
1396 if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
1399 $result = parent::delete($key, $recurse);
1400 if ($this->requirelockingwrite && !$this->release_lock($key)) {
1401 debugging('Failed to release cache lock on delete operation... this should not happen.', DEBUG_DEVELOPER);
1407 * Delete all of the given keys from the cache.
1409 * @param array $keys The key to delete.
1410 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1411 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1412 * @return int The number of items successfully deleted.
1414 public function delete_many(array $keys, $recurse = true) {
1415 if ($this->requirelockingwrite) {
1417 foreach ($keys as $id => $key) {
1418 if ($this->acquire_lock($key)) {
1425 $result = parent::delete_many($keys, $recurse);
1426 if ($this->requirelockingwrite) {
1427 foreach ($locks as $key) {
1428 if ($this->release_lock($key)) {
1429 debugging('Failed to release cache lock on delete_many operation... this should not happen.', DEBUG_DEVELOPER);
1440 * This class is used for session caches returned by the cache::make methods.
1442 * It differs from the application loader in a couple of noteable ways:
1443 * 1. Sessions are always expected to be persistent.
1444 * Because of this we don't ever use the persist cache and instead a session array
1445 * containing all of the data is maintained by this object.
1446 * 2. Session data for a loader instance (store + definition) is consolidate into a
1447 * single array for storage within the store.
1448 * Along with this we embed a lastaccessed time with the data. This way we can
1449 * check sessions for a last access time.
1450 * 3. Session stores are required to support key searching and must
1451 * implement cache_is_searchable. This ensures stores used for the cache can be
1452 * targetted for garbage collection of session data.
1454 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
1455 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
1456 * instance of this class back again.
1458 * @todo we should support locking in the session as well. Should be pretty simple to set up.
1460 * @internal don't use me directly.
1464 * @copyright 2012 Sam Hemelryk
1465 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1467 class cache_session extends cache {
1469 * The user the session has been established for.
1472 protected static $loadeduserid = null;
1475 * The userid this cache is currently using.
1478 protected $currentuserid = null;
1481 * The session id we are currently using.
1484 protected $sessionid = null;
1487 * The session data for the above session id.
1490 protected $session = null;
1493 * Constant used to prefix keys.
1495 const KEY_PREFIX = 'sess_';
1498 * Override the cache::construct method.
1500 * This function gets overriden so that we can process any invalidation events if need be.
1501 * If the definition doesn't have any invalidation events then this occurs exactly as it would for the cache class.
1502 * Otherwise we look at the last invalidation time and then check the invalidation data for events that have occured
1505 * You should not call this method from your code, instead you should use the cache::make methods.
1507 * @param cache_definition $definition
1508 * @param cache_store $store
1509 * @param cache_loader|cache_data_source $loader
1512 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
1513 // First up copy the loadeduserid to the current user id.
1514 $this->currentuserid = self::$loadeduserid;
1515 parent::__construct($definition, $store, $loader);
1516 if ($definition->has_invalidation_events()) {
1517 $lastinvalidation = $this->get('lastsessioninvalidation');
1518 if ($lastinvalidation === false) {
1519 // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
1521 $this->set('lastsessioninvalidation', cache::now());
1523 } else if ($lastinvalidation == cache::now()) {
1524 // We've already invalidated during this request.
1528 // Get the event invalidation cache.
1529 $cache = cache::make('core', 'eventinvalidation');
1530 $events = $cache->get_many($definition->get_invalidation_events());
1531 $todelete = array();
1533 // Iterate the returned data for the events.
1534 foreach ($events as $event => $keys) {
1535 if ($keys === false) {
1536 // No data to be invalidated yet.
1539 // Look at each key and check the timestamp.
1540 foreach ($keys as $key => $timestamp) {
1541 // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
1542 // invalidation and now)then we need to invaliate the key.
1543 if ($timestamp >= $lastinvalidation) {
1544 if ($key === 'purged') {
1555 } else if (!empty($todelete)) {
1556 $todelete = array_unique($todelete);
1557 $this->delete_many($todelete);
1559 // Set the time of the last invalidation.
1560 $this->set('lastsessioninvalidation', cache::now());
1565 * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
1567 * This function is called for every operation that uses keys. For this reason we use this function to also check
1568 * that the current user is the same as the user who last used this cache.
1570 * On top of that if prepends the string 'sess_' to the start of all keys. The _ ensures things are easily identifiable.
1572 * @param string|int $key As passed to get|set|delete etc.
1573 * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
1575 protected function parse_key($key) {
1576 if ($key === 'lastaccess') {
1577 $key = '__lastaccess__';
1579 return 'sess_'.parent::parse_key($key);
1583 * Check that this cache instance is tracking the current user.
1585 protected function check_tracked_user() {
1586 if (isset($_SESSION['USER']->id)) {
1587 // Get the id of the current user.
1588 $new = $_SESSION['USER']->id;
1590 // No user set up yet.
1593 if ($new !== self::$loadeduserid) {
1594 // The current user doesn't match the tracked userid for this request.
1595 if (!is_null(self::$loadeduserid)) {
1596 // Purge the data we have for the old user.
1597 // This way we don't bloat the session.
1599 // Update the session id just in case!
1600 $this->sessionid = session_id();
1602 self::$loadeduserid = $new;
1603 $this->currentuserid = $new;
1604 } else if ($new !== $this->currentuserid) {
1605 // The current user matches the loaded user but not the user last used by this cache.
1607 $this->currentuserid = $new;
1608 // Update the session id just in case!
1609 $this->sessionid = session_id();
1614 * Gets the session data.
1616 * @param bool $force If true the session data will be loaded from the store again.
1617 * @return array An array of session data.
1619 protected function get_session_data($force = false) {
1620 if ($this->sessionid === null) {
1621 $this->sessionid = session_id();
1623 if (is_array($this->session) && !$force) {
1624 return $this->session;
1626 $session = parent::get($this->sessionid);
1627 if ($session === false) {
1630 // We have to write here to ensure that the lastaccess time is recorded.
1631 // And also in order to ensure the session entry exists as when we save it on __destruct
1632 // $CFG is likely to have already been destroyed.
1633 $this->save_session($session);
1634 return $this->session;
1638 * Saves the session data.
1640 * This function also updates the last access time.
1642 * @param array $session
1645 protected function save_session(array $session) {
1646 $session['lastaccess'] = time();
1647 $this->session = $session;
1648 return parent::set($this->sessionid, $this->session);
1652 * Retrieves the value for the given key from the cache.
1654 * @param string|int $key The key for the data being requested.
1655 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
1656 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
1657 * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
1658 * @return mixed|false The data from the cache or false if the key did not exist within the cache.
1659 * @throws moodle_exception
1661 public function get($key, $strictness = IGNORE_MISSING) {
1662 // Check the tracked user.
1663 $this->check_tracked_user();
1664 // 2. Parse the key.
1665 $parsedkey = $this->parse_key($key);
1666 // 3. Get it from the store.
1668 $session = $this->get_session_data();
1669 if (array_key_exists($parsedkey, $session)) {
1670 $result = $session[$parsedkey];
1671 if ($result instanceof cache_ttl_wrapper) {
1672 if ($result->has_expired()) {
1673 $this->get_store()->delete($parsedkey);
1676 $result = $result->data;
1679 if ($result instanceof cache_cached_object) {
1680 $result = $result->restore_object();
1683 // 4. Load if from the loader/datasource if we don't already have it.
1684 $setaftervalidation = false;
1685 if ($result === false) {
1686 if ($this->perfdebug) {
1687 cache_helper::record_cache_miss('**static session**', $this->get_definition()->get_id());
1689 if ($this->get_loader() !== false) {
1690 // We must pass the original (unparsed) key to the next loader in the chain.
1691 // The next loader will parse the key as it sees fit. It may be parsed differently
1692 // depending upon the capabilities of the store associated with the loader.
1693 $result = $this->get_loader()->get($key);
1694 } else if ($this->get_datasource() !== false) {
1695 $result = $this->get_datasource()->load_for_cache($key);
1697 $setaftervalidation = ($result !== false);
1698 } else if ($this->perfdebug) {
1699 cache_helper::record_cache_hit('**static session**', $this->get_definition()->get_id());
1701 // 5. Validate strictness.
1702 if ($strictness === MUST_EXIST && $result === false) {
1703 throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
1705 // 6. Set it to the store if we got it from the loader/datasource.
1706 if ($setaftervalidation) {
1707 $this->set($key, $result);
1709 // 7. Make sure we don't pass back anything that could be a reference.
1710 // We don't want people modifying the data in the cache.
1711 if (!is_scalar($result)) {
1712 // If data is an object it will be a reference.
1713 // If data is an array if may contain references.
1714 // We want to break references so that the cache cannot be modified outside of itself.
1715 // Call the function to unreference it (in the best way possible).
1716 $result = $this->unref($result);
1722 * Sends a key => value pair to the cache.
1725 * // This code will add four entries to the cache, one for each url.
1726 * $cache->set('main', 'http://moodle.org');
1727 * $cache->set('docs', 'http://docs.moodle.org');
1728 * $cache->set('tracker', 'http://tracker.moodle.org');
1729 * $cache->set('qa', 'http://qa.moodle.net');
1732 * @param string|int $key The key for the data being requested.
1733 * It can be any structure although using a scalar string or int is recommended in the interests of performance.
1734 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
1735 * @param mixed $data The data to set against the key.
1736 * @return bool True on success, false otherwise.
1738 public function set($key, $data) {
1739 $this->check_tracked_user();
1740 if ($this->perfdebug) {
1741 cache_helper::record_cache_set('**static session**', $this->get_definition()->get_id());
1743 if (is_object($data) && $data instanceof cacheable_object) {
1744 $data = new cache_cached_object($data);
1745 } else if (!is_scalar($data)) {
1746 // If data is an object it will be a reference.
1747 // If data is an array if may contain references.
1748 // We want to break references so that the cache cannot be modified outside of itself.
1749 // Call the function to unreference it (in the best way possible).
1750 $data = $this->unref($data);
1752 // We dont' support native TTL here as we consolidate data for sessions.
1753 if ($this->has_a_ttl()) {
1754 $data = new cache_ttl_wrapper($data, $this->get_definition()->get_ttl());
1756 $session = $this->get_session_data();
1757 $session[$this->parse_key($key)] = $data;
1758 return $this->save_session($session);
1762 * Delete the given key from the cache.
1764 * @param string|int $key The key to delete.
1765 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1766 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1767 * @return bool True of success, false otherwise.
1769 public function delete($key, $recurse = true) {
1770 $this->check_tracked_user();
1771 $parsedkey = $this->parse_key($key);
1772 if ($recurse && $this->get_loader() !== false) {
1773 // Delete from the bottom of the stack first.
1774 $this->get_loader()->delete($key, $recurse);
1776 $session = $this->get_session_data();
1777 unset($session[$parsedkey]);
1778 return $this->save_session($session);
1782 * Retrieves an array of values for an array of keys.
1784 * Using this function comes with potential performance implications.
1785 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1786 * the equivalent singular method for each item provided.
1787 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1788 * does support it, but you should be aware of this fact.
1790 * @param array $keys The keys of the data being requested.
1791 * Each key can be any structure although using a scalar string or int is recommended in the interests of performance.
1792 * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
1793 * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
1794 * @return array An array of key value pairs for the items that could be retrieved from the cache.
1795 * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
1796 * Otherwise any key that did not exist will have a data value of false within the results.
1797 * @throws moodle_exception
1799 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
1800 $this->check_tracked_user();
1802 foreach ($keys as $key) {
1803 $return[$key] = $this->get($key, $strictness);
1809 * Delete all of the given keys from the cache.
1811 * @param array $keys The key to delete.
1812 * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
1813 * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
1814 * @return int The number of items successfully deleted.
1816 public function delete_many(array $keys, $recurse = true) {
1817 $this->check_tracked_user();
1818 $parsedkeys = array_map(array($this, 'parse_key'), $keys);
1819 if ($recurse && $this->get_loader() !== false) {
1820 // Delete from the bottom of the stack first.
1821 $this->get_loader()->delete_many($keys, $recurse);
1823 $session = $this->get_session_data();
1824 foreach ($parsedkeys as $parsedkey) {
1825 unset($session[$parsedkey]);
1827 $this->save_session($session);
1828 return count($keys);
1832 * Sends several key => value pairs to the cache.
1834 * Using this function comes with potential performance implications.
1835 * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
1836 * the equivalent singular method for each item provided.
1837 * This should not deter you from using this function as there is a performance benefit in situations where the cache store
1838 * does support it, but you should be aware of this fact.
1841 * // This code will add four entries to the cache, one for each url.
1842 * $cache->set_many(array(
1843 * 'main' => 'http://moodle.org',
1844 * 'docs' => 'http://docs.moodle.org',
1845 * 'tracker' => 'http://tracker.moodle.org',
1846 * 'qa' => ''http://qa.moodle.net'
1850 * @param array $keyvaluearray An array of key => value pairs to send to the cache.
1851 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
1852 * ... if they care that is.
1854 public function set_many(array $keyvaluearray) {
1855 $this->check_tracked_user();
1856 $session = $this->get_session_data();
1857 $simulatettl = $this->has_a_ttl();
1858 foreach ($keyvaluearray as $key => $value) {
1859 if (is_object($value) && $value instanceof cacheable_object) {
1860 $value = new cache_cached_object($value);
1861 } else if (!is_scalar($value)) {
1862 // If data is an object it will be a reference.
1863 // If data is an array if may contain references.
1864 // We want to break references so that the cache cannot be modified outside of itself.
1865 // Call the function to unreference it (in the best way possible).
1866 $value = $this->unref($value);
1869 $value = new cache_ttl_wrapper($value, $this->get_definition()->get_ttl());
1871 $parsedkey = $this->parse_key($key);
1872 $session[$parsedkey] = $value;
1874 if ($this->perfdebug) {
1875 cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
1877 $this->save_session($session);
1878 return count($keyvaluearray);
1882 * Purges the cache store, and loader if there is one.
1884 * @return bool True on success, false otherwise
1886 public function purge() {
1887 // 1. Purge the session object.
1888 $this->session = array();
1889 // 2. Delete the record for this users session from the store.
1890 $this->get_store()->delete($this->sessionid);
1891 // 3. Optionally purge any stacked loaders in the same way.
1892 if ($this->get_loader()) {
1893 $this->get_loader()->delete($this->sessionid);
1899 * Test is a cache has a key.
1901 * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
1902 * test and any subsequent action (get, set, delete etc).
1903 * Instead it is recommended to write your code in such a way they it performs the following steps:
1905 * <li>Attempt to retrieve the information.</li>
1906 * <li>Generate the information.</li>
1907 * <li>Attempt to set the information</li>
1910 * Its also worth mentioning that not all stores support key tests.
1911 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
1912 * Just one more reason you should not use these methods unless you have a very good reason to do so.
1914 * @param string|int $key
1915 * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or
1916 * data source then the code will try load the key value from the next item in the chain.
1917 * @return bool True if the cache has the requested key, false otherwise.
1919 public function has($key, $tryloadifpossible = false) {
1920 $this->check_tracked_user();
1921 $parsedkey = $this->parse_key($key);
1922 $session = $this->get_session_data();
1924 if ($this->has_a_ttl()) {
1925 // The data has a TTL and the store doesn't support it natively.
1926 // We must fetch the data and expect a ttl wrapper.
1927 if (array_key_exists($parsedkey, $session)) {
1928 $data = $session[$parsedkey];
1929 $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
1932 $has = array_key_exists($parsedkey, $session);
1934 if (!$has && $tryloadifpossible) {
1935 if ($this->get_loader() !== false) {
1936 $result = $this->get_loader()->get($key);
1937 } else if ($this->get_datasource() !== null) {
1938 $result = $this->get_datasource()->load_for_cache($key);
1940 $has = ($result !== null);
1942 $this->set($key, $result);
1949 * Test is a cache has all of the given keys.
1951 * It is strongly recommended to avoid the use of this function if not absolutely required.
1952 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
1954 * Its also worth mentioning that not all stores support key tests.
1955 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
1956 * Just one more reason you should not use these methods unless you have a very good reason to do so.
1958 * @param array $keys
1959 * @return bool True if the cache has all of the given keys, false otherwise.
1961 public function has_all(array $keys) {
1962 $this->check_tracked_user();
1963 $session = $this->get_session_data();
1964 foreach ($keys as $key) {
1966 $parsedkey = $this->parse_key($key);
1967 if ($this->has_a_ttl()) {
1968 // The data has a TTL and the store doesn't support it natively.
1969 // We must fetch the data and expect a ttl wrapper.
1970 if (array_key_exists($parsedkey, $session)) {
1971 $data = $session[$parsedkey];
1972 $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
1975 $has = array_key_exists($parsedkey, $session);
1985 * Test if a cache has at least one of the given keys.
1987 * It is strongly recommended to avoid the use of this function if not absolutely required.
1988 * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
1990 * Its also worth mentioning that not all stores support key tests.
1991 * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
1992 * Just one more reason you should not use these methods unless you have a very good reason to do so.
1994 * @param array $keys
1995 * @return bool True if the cache has at least one of the given keys
1997 public function has_any(array $keys) {
1998 $this->check_tracked_user();
1999 $session = $this->get_session_data();
2000 foreach ($keys as $key) {
2002 $parsedkey = $this->parse_key($key);
2003 if ($this->has_a_ttl()) {
2004 // The data has a TTL and the store doesn't support it natively.
2005 // We must fetch the data and expect a ttl wrapper.
2006 if (array_key_exists($parsedkey, $session)) {
2007 $data = $session[$parsedkey];
2008 $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
2011 $has = array_key_exists($parsedkey, $session);
2021 * The session loader never uses the persist cache.
2022 * Instead it stores things in the static $session variable. Shared between all session loaders.
2026 protected function is_using_persist_cache() {
2034 * This class is used for request caches returned by the cache::make methods.
2036 * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
2037 * It is technically possible to call those methods through this class however there is no guarantee that you will get an
2038 * instance of this class back again.
2040 * @internal don't use me directly.
2044 * @copyright 2012 Sam Hemelryk
2045 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2047 class cache_request extends cache {
2048 // This comment appeases code pre-checker ;) !