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 * Pretty much just includes the mandatory classes and contains the misc classes that arn't worth separating into individual files.
22 * This file is part of Moodle's cache API, affectionately called MUC.
23 * It contains the components that are requried in order to use caching.
27 * @copyright 2012 Sam Hemelryk
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 defined('MOODLE_INTERNAL') ||
die();
33 // Include the required classes.
34 require_once($CFG->dirroot
.'/cache/classes/interfaces.php');
35 require_once($CFG->dirroot
.'/cache/classes/config.php');
36 require_once($CFG->dirroot
.'/cache/classes/helper.php');
37 require_once($CFG->dirroot
.'/cache/classes/factory.php');
38 require_once($CFG->dirroot
.'/cache/classes/loaders.php');
39 require_once($CFG->dirroot
.'/cache/classes/store.php');
40 require_once($CFG->dirroot
.'/cache/classes/definition.php');
43 * A cached object wrapper.
45 * This class gets used when the data is an object that has implemented the cacheable_object interface.
49 * @copyright 2012 Sam Hemelryk
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 class cache_cached_object
{
55 * The class of the cacheable object
61 * The data returned by the cacheable_object prepare_to_cache method.
67 * Constructs a cached object wrapper.
68 * @param cacheable_object $obj
70 public function __construct(cacheable_object
$obj) {
71 $this->class = get_class($obj);
72 $this->data
= $obj->prepare_to_cache();
76 * Restores the data as an instance of the cacheable_object class.
79 public function restore_object() {
80 $class = $this->class;
81 return $class::wake_from_cache($this->data
);
86 * A wrapper class used to handle ttl when the cache store doesn't natively support it.
88 * This class is exactly why you should use event driving invalidation of cache data rather than relying on ttl.
92 * @copyright 2012 Sam Hemelryk
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
95 class cache_ttl_wrapper
{
98 * The data being stored.
104 * When the cache data expires as a timestamp.
110 * Constructs a ttl cache wrapper.
113 * @param int $ttl The time to live in seconds.
115 public function __construct($data, $ttl) {
117 $this->expires
= cache
::now() +
(int)$ttl;
121 * Returns true if the data has expired.
124 public function has_expired() {
125 return ($this->expires
< cache
::now());
130 * A cache exception class. Just allows people to catch cache exceptions.
134 * @copyright 2012 Sam Hemelryk
135 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
137 class cache_exception
extends moodle_exception
{
139 * Constructs a new exception
141 * @param string $errorcode
142 * @param string $module
143 * @param string $link
145 * @param mixed $debuginfo
147 public function __construct($errorcode, $module = 'cache', $link = '', $a = null, $debuginfo = null) {
148 // This may appear like a useless override but you will notice that we have set a MUCH more useful default for $module.
149 parent
::__construct($errorcode, $module, $link, $a, $debuginfo);