forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
150 lines (136 loc) · 4.33 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The core cache API.
*
* Pretty much just includes the mandatory classes and contains the misc classes that arn't worth separating into individual files.
*
* This file is part of Moodle's cache API, affectionately called MUC.
* It contains the components that are requried in order to use caching.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Include the required classes.
require_once($CFG->dirroot.'/cache/classes/interfaces.php');
require_once($CFG->dirroot.'/cache/classes/config.php');
require_once($CFG->dirroot.'/cache/classes/helper.php');
require_once($CFG->dirroot.'/cache/classes/factory.php');
require_once($CFG->dirroot.'/cache/classes/loaders.php');
require_once($CFG->dirroot.'/cache/classes/definition.php');
/**
* A cached object wrapper.
*
* This class gets used when the data is an object that has implemented the cacheable_object interface.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cache_cached_object {
/**
* The class of the cacheable object
* @var string
*/
protected $class;
/**
* The data returned by the cacheable_object prepare_to_cache method.
* @var mixed
*/
protected $data;
/**
* Constructs a cached object wrapper.
* @param cacheable_object $obj
*/
public function __construct(cacheable_object $obj) {
$this->class = get_class($obj);
$this->data = $obj->prepare_to_cache();
}
/**
* Restores the data as an instance of the cacheable_object class.
* @return object
*/
public function restore_object() {
$class = $this->class;
return $class::wake_from_cache($this->data);
}
}
/**
* A wrapper class used to handle ttl when the cache store doesn't natively support it.
*
* This class is exactly why you should use event driving invalidation of cache data rather than relying on ttl.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cache_ttl_wrapper {
/**
* The data being stored.
* @var mixed
*/
public $data;
/**
* When the cache data expires as a timestamp.
* @var int
*/
public $expires;
/**
* Constructs a ttl cache wrapper.
*
* @param mixed $data
* @param int $ttl The time to live in seconds.
*/
public function __construct($data, $ttl) {
$this->data = $data;
$this->expires = cache::now() + (int)$ttl;
}
/**
* Returns true if the data has expired.
* @return int
*/
public function has_expired() {
return ($this->expires < cache::now());
}
}
/**
* A cache exception class. Just allows people to catch cache exceptions.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cache_exception extends moodle_exception {
/**
* Constructs a new exception
*
* @param string $errorcode
* @param string $module
* @param string $link
* @param mixed $a
* @param mixed $debuginfo
*/
public function __construct($errorcode, $module = 'cache', $link = '', $a = null, $debuginfo = null) {
// This may appear like a useless override but you will notice that we have set a MUCH more useful default for $module.
parent::__construct($errorcode, $module, $link, $a, $debuginfo);
}
}