Merge "Disable expensive {{REVISIONID}} magic word in miser mode"
[lhc/web/wiklou.git] / includes / cache / dependency / DependencyWrapper.php
1 <?php
2 /**
3 * Data caching with dependencies.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * This class stores an arbitrary value along with its dependencies.
26 * Users should typically only use DependencyWrapper::getValueFromCache(),
27 * rather than instantiating one of these objects directly.
28 * @ingroup Cache
29 */
30 class DependencyWrapper {
31 private $value;
32 /** @var CacheDependency[] */
33 private $deps;
34
35 /**
36 * @param mixed $value The user-supplied value
37 * @param CacheDependency|CacheDependency[] $deps A dependency or dependency
38 * array. All dependencies must be objects implementing CacheDependency.
39 */
40 function __construct( $value = false, $deps = [] ) {
41 $this->value = $value;
42
43 if ( !is_array( $deps ) ) {
44 $deps = [ $deps ];
45 }
46
47 $this->deps = $deps;
48 }
49
50 /**
51 * Returns true if any of the dependencies have expired
52 *
53 * @return bool
54 */
55 function isExpired() {
56 foreach ( $this->deps as $dep ) {
57 if ( $dep->isExpired() ) {
58 return true;
59 }
60 }
61
62 return false;
63 }
64
65 /**
66 * Initialise dependency values in preparation for storing. This must be
67 * called before serialization.
68 */
69 function initialiseDeps() {
70 foreach ( $this->deps as $dep ) {
71 $dep->loadDependencyValues();
72 }
73 }
74
75 /**
76 * Get the user-defined value
77 * @return bool|mixed
78 */
79 function getValue() {
80 return $this->value;
81 }
82
83 /**
84 * Store the wrapper to a cache
85 *
86 * @param BagOStuff $cache
87 * @param string $key
88 * @param int $expiry
89 */
90 function storeToCache( $cache, $key, $expiry = 0 ) {
91 $this->initialiseDeps();
92 $cache->set( $key, $this, $expiry );
93 }
94
95 /**
96 * Attempt to get a value from the cache. If the value is expired or missing,
97 * it will be generated with the callback function (if present), and the newly
98 * calculated value will be stored to the cache in a wrapper.
99 *
100 * @param BagOStuff $cache
101 * @param string $key The cache key
102 * @param int $expiry The expiry timestamp or interval in seconds
103 * @param bool|callable $callback The callback for generating the value, or false
104 * @param array $callbackParams The function parameters for the callback
105 * @param array $deps The dependencies to store on a cache miss. Note: these
106 * are not the dependencies used on a cache hit! Cache hits use the stored
107 * dependency array.
108 *
109 * @return mixed The value, or null if it was not present in the cache and no
110 * callback was defined.
111 */
112 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
113 $callbackParams = [], $deps = []
114 ) {
115 $obj = $cache->get( $key );
116
117 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
118 $value = $obj->value;
119 } elseif ( $callback ) {
120 $value = $callback( ...$callbackParams );
121 # Cache the newly-generated value
122 $wrapper = new DependencyWrapper( $value, $deps );
123 $wrapper->storeToCache( $cache, $key, $expiry );
124 } else {
125 $value = null;
126 }
127
128 return $value;
129 }
130 }