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 |
use core\exception\moodle_exception;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Cache Loader supporting locking.
|
|
|
23 |
*
|
|
|
24 |
* This interface should be given to classes already implementing core_cache\loader_interface that also wish to support locking.
|
|
|
25 |
* It outlines the required structure for utilising locking functionality when using a cache.
|
|
|
26 |
*
|
|
|
27 |
* Can be implemented by any class already implementing the core_cache\loader_interface interface.
|
|
|
28 |
* @package core_cache
|
|
|
29 |
* @copyright Sam Hemelryk
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
interface loader_with_locking_interface {
|
|
|
33 |
/**
|
|
|
34 |
* Acquires a lock for the given key.
|
|
|
35 |
*
|
|
|
36 |
* Please note that this happens automatically if the cache definition requires locking.
|
|
|
37 |
* it is still made a public method so that adhoc caches can use it if they choose.
|
|
|
38 |
* However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
|
|
|
39 |
* locks are acquired, checked, and released.
|
|
|
40 |
*
|
|
|
41 |
* Prior to Moodle 4,3 this function used to return false if the lock cannot be obtained. It
|
|
|
42 |
* now always returns true, and throws an exception if the lock cannot be obtained.
|
|
|
43 |
*
|
|
|
44 |
* @param string|int $key
|
|
|
45 |
* @return bool Always returns true (for backwards compatibility)
|
|
|
46 |
* @throws moodle_exception If the lock cannot be obtained after a timeout
|
|
|
47 |
*/
|
|
|
48 |
public function acquire_lock($key);
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Checks if the cache loader owns the lock for the given key.
|
|
|
52 |
*
|
|
|
53 |
* Please note that this happens automatically if the cache definition requires locking.
|
|
|
54 |
* it is still made a public method so that adhoc caches can use it if they choose.
|
|
|
55 |
* However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
|
|
|
56 |
* locks are acquired, checked, and released.
|
|
|
57 |
*
|
|
|
58 |
* @param string|int $key
|
|
|
59 |
* @return bool True if this code has the lock, false if there is a lock but this code doesn't have it,
|
|
|
60 |
* null if there is no lock.
|
|
|
61 |
*/
|
|
|
62 |
public function check_lock_state($key);
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Releases the lock for the given key.
|
|
|
66 |
*
|
|
|
67 |
* Please note that this happens automatically if the cache definition requires locking.
|
|
|
68 |
* it is still made a public method so that adhoc caches can use it if they choose.
|
|
|
69 |
* However this doesn't guarantee consistent access. It will become the responsibility of the calling code to ensure
|
|
|
70 |
* locks are acquired, checked, and released.
|
|
|
71 |
*
|
|
|
72 |
* @param string|int $key
|
|
|
73 |
* @return bool True if the lock has been released, false if there was a problem releasing the lock.
|
|
|
74 |
*/
|
|
|
75 |
public function release_lock($key);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Alias this class to the old name.
|
|
|
79 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
80 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
81 |
class_alias(loader_with_locking_interface::class, \cache_loader_with_locking::class);
|