Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_cache;
18
 
19
/**
20
 * A wrapper class used to handle ttl when the cache store doesn't natively support it.
21
 *
22
 * This class is exactly why you should use event driving invalidation of cache data rather than relying on ttl.
23
 *
24
 * @package    core_cache
25
 * @category   cache
26
 * @copyright  2012 Sam Hemelryk
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class ttl_wrapper {
30
    /**
31
     * The data being stored.
32
     * @var mixed
33
     */
34
    public $data;
35
 
36
    /**
37
     * When the cache data expires as a timestamp.
38
     * @var int
39
     */
40
    public $expires;
41
 
42
    /**
43
     * Constructs a ttl cache wrapper.
44
     *
45
     * @param mixed $data
46
     * @param int $ttl The time to live in seconds.
47
     */
48
    public function __construct($data, $ttl) {
49
        $this->data = $data;
50
        $this->expires = cache::now() + (int)$ttl;
51
    }
52
 
53
    /**
54
     * Returns true if the data has expired.
55
     * @return int
56
     */
57
    public function has_expired() {
58
        return ($this->expires < cache::now());
59
    }
60
}
61
 
62
// Alias this class to the old name.
63
// This file will be autoloaded by the legacyclasses autoload system.
64
// In future all uses of this class will be corrected and the legacy references will be removed.
65
class_alias(ttl_wrapper::class, \cache_ttl_wrapper::class);