Followup to r86053 - fix special page cases
[mediawiki.git] / includes / cache / CacheDependency.php
blobaa0206643b58a506fe36d593d3ce261dd8c8318d
1 <?php
2 /**
3 * This class stores an arbitrary value along with its dependencies.
4 * Users should typically only use DependencyWrapper::getValueFromCache(),
5 * rather than instantiating one of these objects directly.
6 * @ingroup Cache
7 */
9 class DependencyWrapper {
10 var $value;
11 var $deps;
13 /**
14 * Create an instance.
15 * @param $value Mixed: the user-supplied value
16 * @param $deps Mixed: a dependency or dependency array. All dependencies
17 * must be objects implementing CacheDependency.
19 function __construct( $value = false, $deps = array() ) {
20 $this->value = $value;
22 if ( !is_array( $deps ) ) {
23 $deps = array( $deps );
26 $this->deps = $deps;
29 /**
30 * Returns true if any of the dependencies have expired
32 function isExpired() {
33 foreach ( $this->deps as $dep ) {
34 if ( $dep->isExpired() ) {
35 return true;
39 return false;
42 /**
43 * Initialise dependency values in preparation for storing. This must be
44 * called before serialization.
46 function initialiseDeps() {
47 foreach ( $this->deps as $dep ) {
48 $dep->loadDependencyValues();
52 /**
53 * Get the user-defined value
55 function getValue() {
56 return $this->value;
59 /**
60 * Store the wrapper to a cache
62 * @param $cache BagOStuff
63 * @param $key
64 * @param $expiry
66 function storeToCache( $cache, $key, $expiry = 0 ) {
67 $this->initialiseDeps();
68 $cache->set( $key, $this, $expiry );
71 /**
72 * Attempt to get a value from the cache. If the value is expired or missing,
73 * it will be generated with the callback function (if present), and the newly
74 * calculated value will be stored to the cache in a wrapper.
76 * @param $cache BagOStuff a cache object such as $wgMemc
77 * @param $key String: the cache key
78 * @param $expiry Integer: the expiry timestamp or interval in seconds
79 * @param $callback Mixed: the callback for generating the value, or false
80 * @param $callbackParams Array: the function parameters for the callback
81 * @param $deps Array: the dependencies to store on a cache miss. Note: these
82 * are not the dependencies used on a cache hit! Cache hits use the stored
83 * dependency array.
85 * @return mixed The value, or null if it was not present in the cache and no
86 * callback was defined.
88 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
89 $callbackParams = array(), $deps = array() )
91 $obj = $cache->get( $key );
93 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
94 $value = $obj->value;
95 } elseif ( $callback ) {
96 $value = call_user_func_array( $callback, $callbackParams );
97 # Cache the newly-generated value
98 $wrapper = new DependencyWrapper( $value, $deps );
99 $wrapper->storeToCache( $cache, $key, $expiry );
100 } else {
101 $value = null;
104 return $value;
109 * @ingroup Cache
111 abstract class CacheDependency {
113 * Returns true if the dependency is expired, false otherwise
115 abstract function isExpired();
118 * Hook to perform any expensive pre-serialize loading of dependency values.
120 function loadDependencyValues() { }
124 * @ingroup Cache
126 class FileDependency extends CacheDependency {
127 var $filename, $timestamp;
130 * Create a file dependency
132 * @param $filename String: the name of the file, preferably fully qualified
133 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
134 * file does not exist. If omitted, the timestamp will be loaded from
135 * the file.
137 * A dependency on a nonexistent file will be triggered when the file is
138 * created. A dependency on an existing file will be triggered when the
139 * file is changed.
141 function __construct( $filename, $timestamp = null ) {
142 $this->filename = $filename;
143 $this->timestamp = $timestamp;
146 function __sleep() {
147 $this->loadDependencyValues();
148 return array( 'filename', 'timestamp' );
151 function loadDependencyValues() {
152 if ( is_null( $this->timestamp ) ) {
153 if ( !file_exists( $this->filename ) ) {
154 # Dependency on a non-existent file
155 # This is a valid concept!
156 $this->timestamp = false;
157 } else {
158 $this->timestamp = filemtime( $this->filename );
164 * @return bool
166 function isExpired() {
167 if ( !file_exists( $this->filename ) ) {
168 if ( $this->timestamp === false ) {
169 # Still nonexistent
170 return false;
171 } else {
172 # Deleted
173 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
174 return true;
176 } else {
177 $lastmod = filemtime( $this->filename );
178 if ( $lastmod > $this->timestamp ) {
179 # Modified or created
180 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
181 return true;
182 } else {
183 # Not modified
184 return false;
191 * @ingroup Cache
193 class TitleDependency extends CacheDependency {
194 var $titleObj;
195 var $ns, $dbk;
196 var $touched;
199 * Construct a title dependency
200 * @param $title Title
202 function __construct( Title $title ) {
203 $this->titleObj = $title;
204 $this->ns = $title->getNamespace();
205 $this->dbk = $title->getDBkey();
208 function loadDependencyValues() {
209 $this->touched = $this->getTitle()->getTouched();
213 * Get rid of bulky Title object for sleep
215 * @return array
217 function __sleep() {
218 return array( 'ns', 'dbk', 'touched' );
222 * @return Title
224 function getTitle() {
225 if ( !isset( $this->titleObj ) ) {
226 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
229 return $this->titleObj;
233 * @return bool
235 function isExpired() {
236 $touched = $this->getTitle()->getTouched();
238 if ( $this->touched === false ) {
239 if ( $touched === false ) {
240 # Still missing
241 return false;
242 } else {
243 # Created
244 return true;
246 } elseif ( $touched === false ) {
247 # Deleted
248 return true;
249 } elseif ( $touched > $this->touched ) {
250 # Updated
251 return true;
252 } else {
253 # Unmodified
254 return false;
260 * @ingroup Cache
262 class TitleListDependency extends CacheDependency {
263 var $linkBatch;
264 var $timestamps;
267 * Construct a dependency on a list of titles
269 function __construct( LinkBatch $linkBatch ) {
270 $this->linkBatch = $linkBatch;
273 function calculateTimestamps() {
274 # Initialise values to false
275 $timestamps = array();
277 foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
278 if ( count( $dbks ) > 0 ) {
279 $timestamps[$ns] = array();
281 foreach ( $dbks as $dbk => $value ) {
282 $timestamps[$ns][$dbk] = false;
287 # Do the query
288 if ( count( $timestamps ) ) {
289 $dbr = wfGetDB( DB_SLAVE );
290 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
291 $res = $dbr->select(
292 'page',
293 array( 'page_namespace', 'page_title', 'page_touched' ),
294 $where,
295 __METHOD__
298 foreach ( $res as $row ) {
299 $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
303 return $timestamps;
306 function loadDependencyValues() {
307 $this->timestamps = $this->calculateTimestamps();
311 * @return array
313 function __sleep() {
314 return array( 'timestamps' );
317 function getLinkBatch() {
318 if ( !isset( $this->linkBatch ) ) {
319 $this->linkBatch = new LinkBatch;
320 $this->linkBatch->setArray( $this->timestamps );
322 return $this->linkBatch;
326 * @return bool
328 function isExpired() {
329 $newTimestamps = $this->calculateTimestamps();
331 foreach ( $this->timestamps as $ns => $dbks ) {
332 foreach ( $dbks as $dbk => $oldTimestamp ) {
333 $newTimestamp = $newTimestamps[$ns][$dbk];
335 if ( $oldTimestamp === false ) {
336 if ( $newTimestamp === false ) {
337 # Still missing
338 } else {
339 # Created
340 return true;
342 } elseif ( $newTimestamp === false ) {
343 # Deleted
344 return true;
345 } elseif ( $newTimestamp > $oldTimestamp ) {
346 # Updated
347 return true;
348 } else {
349 # Unmodified
354 return false;
359 * @ingroup Cache
361 class GlobalDependency extends CacheDependency {
362 var $name, $value;
364 function __construct( $name ) {
365 $this->name = $name;
366 $this->value = $GLOBALS[$name];
370 * @return bool
372 function isExpired() {
373 return $GLOBALS[$this->name] != $this->value;
378 * @ingroup Cache
380 class ConstantDependency extends CacheDependency {
381 var $name, $value;
383 function __construct( $name ) {
384 $this->name = $name;
385 $this->value = constant( $name );
389 * @return bool
391 function isExpired() {
392 return constant( $this->name ) != $this->value;