3 * Data caching with dependencies.
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.
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.
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
23 use MediaWiki\MediaWikiServices
;
26 * This class stores an arbitrary value along with its dependencies.
27 * Users should typically only use DependencyWrapper::getValueFromCache(),
28 * rather than instantiating one of these objects directly.
31 class DependencyWrapper
{
33 /** @var CacheDependency[] */
38 * @param mixed $value The user-supplied value
39 * @param CacheDependency|CacheDependency[] $deps A dependency or dependency
40 * array. All dependencies must be objects implementing CacheDependency.
42 function __construct( $value = false, $deps = [] ) {
43 $this->value
= $value;
45 if ( !is_array( $deps ) ) {
53 * Returns true if any of the dependencies have expired
57 function isExpired() {
58 foreach ( $this->deps
as $dep ) {
59 if ( $dep->isExpired() ) {
68 * Initialise dependency values in preparation for storing. This must be
69 * called before serialization.
71 function initialiseDeps() {
72 foreach ( $this->deps
as $dep ) {
73 $dep->loadDependencyValues();
78 * Get the user-defined value
86 * Store the wrapper to a cache
88 * @param BagOStuff $cache
92 function storeToCache( $cache, $key, $expiry = 0 ) {
93 $this->initialiseDeps();
94 $cache->set( $key, $this, $expiry );
98 * Attempt to get a value from the cache. If the value is expired or missing,
99 * it will be generated with the callback function (if present), and the newly
100 * calculated value will be stored to the cache in a wrapper.
102 * @param BagOStuff $cache A cache object
103 * @param string $key The cache key
104 * @param int $expiry The expiry timestamp or interval in seconds
105 * @param bool|callable $callback The callback for generating the value, or false
106 * @param array $callbackParams The function parameters for the callback
107 * @param array $deps The dependencies to store on a cache miss. Note: these
108 * are not the dependencies used on a cache hit! Cache hits use the stored
111 * @return mixed The value, or null if it was not present in the cache and no
112 * callback was defined.
114 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
115 $callbackParams = [], $deps = []
117 $obj = $cache->get( $key );
119 if ( is_object( $obj ) && $obj instanceof DependencyWrapper
&& !$obj->isExpired() ) {
120 $value = $obj->value
;
121 } elseif ( $callback ) {
122 $value = call_user_func_array( $callback, $callbackParams );
123 # Cache the newly-generated value
124 $wrapper = new DependencyWrapper( $value, $deps );
125 $wrapper->storeToCache( $cache, $key, $expiry );
137 abstract class CacheDependency
{
139 * Returns true if the dependency is expired, false otherwise
141 abstract function isExpired();
144 * Hook to perform any expensive pre-serialize loading of dependency values.
146 function loadDependencyValues() {
153 class FileDependency
extends CacheDependency
{
158 * Create a file dependency
160 * @param string $filename The name of the file, preferably fully qualified
161 * @param null|bool|int $timestamp The unix last modified timestamp, or false if the
162 * file does not exist. If omitted, the timestamp will be loaded from
165 * A dependency on a nonexistent file will be triggered when the file is
166 * created. A dependency on an existing file will be triggered when the
169 function __construct( $filename, $timestamp = null ) {
170 $this->filename
= $filename;
171 $this->timestamp
= $timestamp;
178 $this->loadDependencyValues();
180 return [ 'filename', 'timestamp' ];
183 function loadDependencyValues() {
184 if ( is_null( $this->timestamp
) ) {
185 MediaWiki\
suppressWarnings();
186 # Dependency on a non-existent file stores "false"
187 # This is a valid concept!
188 $this->timestamp
= filemtime( $this->filename
);
189 MediaWiki\restoreWarnings
();
196 function isExpired() {
197 MediaWiki\
suppressWarnings();
198 $lastmod = filemtime( $this->filename
);
199 MediaWiki\restoreWarnings
();
200 if ( $lastmod === false ) {
201 if ( $this->timestamp
=== false ) {
206 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
211 if ( $lastmod > $this->timestamp
) {
212 # Modified or created
213 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
227 class GlobalDependency
extends CacheDependency
{
231 function __construct( $name ) {
233 $this->value
= $GLOBALS[$name];
239 function isExpired() {
240 if ( !isset( $GLOBALS[$this->name
] ) ) {
244 return $GLOBALS[$this->name
] != $this->value
;
251 class MainConfigDependency
extends CacheDependency
{
255 function __construct( $name ) {
257 $this->value
= $this->getConfig()->get( $this->name
);
260 private function getConfig() {
261 return MediaWikiServices
::getInstance()->getMainConfig();
267 function isExpired() {
268 if ( !$this->getConfig()->has( $this->name
) ) {
272 return $this->getConfig()->get( $this->name
) != $this->value
;
279 class ConstantDependency
extends CacheDependency
{
283 function __construct( $name ) {
285 $this->value
= constant( $name );
291 function isExpired() {
292 return constant( $this->name
) != $this->value
;