MDL-25290 cache: Implemented Cache API aka MUC
[moodle.git] / cache / lib.php
CommitLineData
8139ad13
SH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * The core cache API.
19 *
20 * Pretty much just includes the mandatory classes and contains the misc classes that arn't worth separating into individual files.
21 *
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.
24 *
25 * @package core
26 * @category cache
27 * @copyright 2012 Sam Hemelryk
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30
31defined('MOODLE_INTERNAL') || die();
32
33// Include the required classes.
34require_once($CFG->dirroot.'/cache/classes/lock.php');
35require_once($CFG->dirroot.'/cache/classes/interfaces.php');
36require_once($CFG->dirroot.'/cache/classes/config.php');
37require_once($CFG->dirroot.'/cache/classes/helper.php');
38require_once($CFG->dirroot.'/cache/classes/factory.php');
39require_once($CFG->dirroot.'/cache/classes/loaders.php');
40require_once($CFG->dirroot.'/cache/classes/definition.php');
41
42/**
43 * A cached object wrapper.
44 *
45 * This class gets used when the data is an object that has implemented the cacheable_object interface.
46 *
47 * @package core
48 * @category cache
49 * @copyright 2012 Sam Hemelryk
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 */
52class cache_cached_object {
53
54 /**
55 * The class of the cacheable object
56 * @var string
57 */
58 protected $class;
59
60 /**
61 * The data returned by the cacheable_object prepare_to_cache method.
62 * @var mixed
63 */
64 protected $data;
65
66 /**
67 * Constructs a cached object wrapper.
68 * @param cacheable_object $obj
69 */
70 public function __construct(cacheable_object $obj) {
71 $this->class = get_class($obj);
72 $this->data = $obj->prepare_to_cache();
73 }
74
75 /**
76 * Restores the data as an instance of the cacheable_object class.
77 * @return object
78 */
79 public function restore_object() {
80 $class = $this->class;
81 return $class::wake_from_cache($this->data);
82 }
83}
84
85/**
86 * A wrapper class used to handle ttl when the cache store doesn't natively support it.
87 *
88 * This class is exactly why you should use event driving invalidation of cache data rather than relying on ttl.
89 *
90 * @package core
91 * @category cache
92 * @copyright 2012 Sam Hemelryk
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 */
95class cache_ttl_wrapper {
96
97 /**
98 * The data being stored.
99 * @var mixed
100 */
101 public $data;
102
103 /**
104 * When the cache data expires as a timestamp.
105 * @var int
106 */
107 public $expires;
108
109 /**
110 * Constructs a ttl cache wrapper.
111 *
112 * @param mixed $data
113 * @param int $ttl The time to live in seconds.
114 */
115 public function __construct($data, $ttl) {
116 $this->data = $data;
117 $this->expires = cache::now() + (int)$ttl;
118 }
119
120 /**
121 * Returns true if the data has expired.
122 * @return int
123 */
124 public function has_expired() {
125 return ($this->expires < cache::now());
126 }
127}
128
129/**
130 * A cache exception class. Just allows people to catch cache exceptions.
131 *
132 * @package core
133 * @category cache
134 * @copyright 2012 Sam Hemelryk
135 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
136 */
137class cache_exception extends moodle_exception {
138 public function __construct($errorcode, $module = 'cache', $link = '', $a = NULL, $debuginfo = null) {
139 parent::__construct($errorcode, $module, $link, $a, $debuginfo);
140 }
141}