Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Create and keep an instance of this class to allow temporary caches when caches are disabled.
21
 *
22
 * This class works together with code in {@see cache_factory_disabled}.
23
 *
24
 * The intention is that temporary cache should be short-lived (not for the entire install process),
25
 * which avoids two problems: first, that we might run out of memory for the caches, and second,
26
 * that some code e.g. install.php/upgrade.php files, is entitled to assume that caching is not
27
 * used and make direct database changes.
28
 *
29
 * @package core_cache
30
 * @copyright 2022 The Open University
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class allow_temporary_caches {
34
    /** @var int Number of references of this class; if more than 0, temporary caches are allowed */
35
    protected static $references = 0;
36
 
37
    /**
38
     * Constructs an instance of this class.
39
     *
40
     * Temporary caches will be allowed until this instance goes out of scope. Store this token
41
     * in a local variable, so that the caches have a limited life; do not save it outside your
42
     * function.
43
     *
44
     * If cache is not disabled then normal (non-temporary) caches will be used, and this class
45
     * does nothing.
46
     *
47
     * If an object of this class already exists then creating (or destroying) another one will
48
     * have no effect.
49
     */
50
    public function __construct() {
51
        self::$references++;
52
    }
53
 
54
    /**
55
     * Destroys an instance of this class.
56
     *
57
     * You do not need to call this manually; PHP will call it automatically when your variable
58
     * goes out of scope. If you do need to remove your token at other times, use unset($token);
59
     *
60
     * If there are no other instances of this object, then all temporary caches will be discarded.
61
     */
62
    public function __destruct() {
63
        global $CFG;
64
        require_once($CFG->dirroot . '/cache/disabledlib.php');
65
 
66
        self::$references--;
67
        if (self::$references === 0) {
68
            \cache_factory_disabled::clear_temporary_caches();
69
        }
70
    }
71
 
72
    /**
73
     * Checks if temp caches are currently allowed.
74
     *
75
     * @return bool True if allowed
76
     */
77
    public static function is_allowed(): bool {
78
        return self::$references > 0;
79
    }
80
}