API * Optimized backlinking query (still needs an index change)
[mediawiki.git] / includes / CacheDependency.php
blob4bb3d328689559a4705fdc5327ba8aec2953159b
1 <?php
3 /**
4 * This class stores an arbitrary value along with its dependencies.
5 * Users should typically only use DependencyWrapper::getFromCache(), rather
6 * than instantiating one of these objects directly.
7 */
8 class DependencyWrapper {
9 var $value;
10 var $deps;
12 /**
13 * Create an instance.
14 * @param mixed $value The user-supplied value
15 * @param mixed $deps A dependency or dependency array. All dependencies
16 * must be objects implementing CacheDependency.
18 function __construct( $value = false, $deps = array() ) {
19 $this->value = $value;
20 if ( !is_array( $deps ) ) {
21 $deps = array( $deps );
23 $this->deps = $deps;
26 /**
27 * Returns true if any of the dependencies have expired
29 function isExpired() {
30 foreach ( $this->deps as $dep ) {
31 if ( $dep->isExpired() ) {
32 return true;
35 return false;
38 /**
39 * Initialise dependency values in preparation for storing. This must be
40 * called before serialization.
42 function initialiseDeps() {
43 foreach ( $this->deps as $dep ) {
44 $dep->loadDependencyValues();
48 /**
49 * Get the user-defined value
51 function getValue() {
52 return $this->value;
55 /**
56 * Store the wrapper to a cache
58 function storeToCache( $cache, $key, $expiry = 0 ) {
59 $this->initialiseDeps();
60 $cache->set( $key, $this, $expiry );
63 /**
64 * Attempt to get a value from the cache. If the value is expired or missing,
65 * it will be generated with the callback function (if present), and the newly
66 * calculated value will be stored to the cache in a wrapper.
68 * @param object $cache A cache object such as $wgMemc
69 * @param string $key The cache key
70 * @param integer $expiry The expiry timestamp or interval in seconds
71 * @param mixed $callback The callback for generating the value, or false
72 * @param array $callbackParams The function parameters for the callback
73 * @param array $deps The dependencies to store on a cache miss. Note: these
74 * are not the dependencies used on a cache hit! Cache hits use the stored
75 * dependency array.
77 * @return mixed The value, or null if it was not present in the cache and no
78 * callback was defined.
80 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
81 $callbackParams = array(), $deps = array() )
83 $obj = $cache->get( $key );
84 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
85 $value = $obj->value;
86 } elseif ( $callback ) {
87 $value = call_user_func_array( $callback, $callbackParams );
88 # Cache the newly-generated value
89 $wrapper = new DependencyWrapper( $value, $deps );
90 $wrapper->storeToCache( $cache, $key, $expiry );
91 } else {
92 $value = null;
94 return $value;
98 abstract class CacheDependency {
99 /**
100 * Returns true if the dependency is expired, false otherwise
102 abstract function isExpired();
105 * Hook to perform any expensive pre-serialize loading of dependency values.
107 function loadDependencyValues() {}
110 class FileDependency extends CacheDependency {
111 var $filename, $timestamp;
114 * Create a file dependency
116 * @param string $filename The name of the file, preferably fully qualified
117 * @param mixed $timestamp The unix last modified timestamp, or false if the
118 * file does not exist. If omitted, the timestamp will be loaded from
119 * the file.
121 * A dependency on a nonexistent file will be triggered when the file is
122 * created. A dependency on an existing file will be triggered when the
123 * file is changed.
125 function __construct( $filename, $timestamp = null ) {
126 $this->filename = $filename;
127 $this->timestamp = $timestamp;
130 function loadDependencyValues() {
131 if ( is_null( $this->timestamp ) ) {
132 if ( !file_exists( $this->filename ) ) {
133 # Dependency on a non-existent file
134 # This is a valid concept!
135 $this->timestamp = false;
136 } else {
137 $this->timestamp = filemtime( $this->filename );
142 function isExpired() {
143 if ( !file_exists( $this->filename ) ) {
144 if ( $this->timestamp === false ) {
145 # Still nonexistent
146 return false;
147 } else {
148 # Deleted
149 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
150 return true;
152 } else {
153 $lastmod = filemtime( $this->filename );
154 if ( $lastmod > $this->timestamp ) {
155 # Modified or created
156 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
157 return true;
158 } else {
159 # Not modified
160 return false;
166 class TitleDependency extends CacheDependency {
167 var $titleObj;
168 var $ns, $dbk;
169 var $touched;
172 * Construct a title dependency
173 * @param Title $title
175 function __construct( Title $title ) {
176 $this->titleObj = $title;
177 $this->ns = $title->getNamespace();
178 $this->dbk = $title->getDBkey();
181 function loadDependencyValues() {
182 $this->touched = $this->getTitle()->getTouched();
186 * Get rid of bulky Title object for sleep
188 function __sleep() {
189 return array( 'ns', 'dbk', 'touched' );
192 function getTitle() {
193 if ( !isset( $this->titleObj ) ) {
194 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
196 return $this->titleObj;
199 function isExpired() {
200 $touched = $this->getTitle()->getTouched();
201 if ( $this->touched === false ) {
202 if ( $touched === false ) {
203 # Still missing
204 return false;
205 } else {
206 # Created
207 return true;
209 } elseif ( $touched === false ) {
210 # Deleted
211 return true;
212 } elseif ( $touched > $this->touched ) {
213 # Updated
214 return true;
215 } else {
216 # Unmodified
217 return false;
222 class TitleListDependency extends CacheDependency {
223 var $linkBatch;
224 var $timestamps;
227 * Construct a dependency on a list of titles
229 function __construct( LinkBatch $linkBatch ) {
230 $this->linkBatch = $linkBatch;
233 function calculateTimestamps() {
234 # Initialise values to false
235 $timestamps = array();
236 foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
237 if ( count( $dbks ) > 0 ) {
238 $timestamps[$ns] = array();
239 foreach ( $dbks as $dbk => $value ) {
240 $timestamps[$ns][$dbk] = false;
245 # Do the query
246 if ( count( $timestamps ) ) {
247 $dbr =& wfGetDB( DB_SLAVE );
248 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
249 $res = $dbr->select( 'page',
250 array( 'page_namespace', 'page_title', 'page_touched' ),
251 $where, __METHOD__ );
252 while ( $row = $dbr->fetchObject( $res ) ) {
253 $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
256 return $timestamps;
259 function loadDependencyValues() {
260 $this->timestamps = $this->calculateTimestamps();
263 function __sleep() {
264 return array( 'timestamps' );
267 function getLinkBatch() {
268 if ( !isset( $this->linkBatch ) ){
269 $this->linkBatch = new LinkBatch;
270 $this->linkBatch->setArray( $this->timestamps );
272 return $this->linkBatch;
275 function isExpired() {
276 $newTimestamps = $this->calculateTimestamps();
277 foreach ( $this->timestamps as $ns => $dbks ) {
278 foreach ( $dbks as $dbk => $oldTimestamp ) {
279 $newTimestamp = $newTimestamps[$ns][$dbk];
280 if ( $oldTimestamp === false ) {
281 if ( $newTimestamp === false ) {
282 # Still missing
283 } else {
284 # Created
285 return true;
287 } elseif ( $newTimestamp === false ) {
288 # Deleted
289 return true;
290 } elseif ( $newTimestamp > $oldTimestamp ) {
291 # Updated
292 return true;
293 } else {
294 # Unmodified
298 return false;
302 class GlobalDependency extends CacheDependency {
303 var $name, $value;
305 function __construct( $name ) {
306 $this->name = $name;
307 $this->value = $GLOBALS[$name];
310 function isExpired() {
311 return $GLOBALS[$this->name] != $this->value;
315 class ConstantDependency extends CacheDependency {
316 var $name, $value;
318 function __construct( $name ) {
319 $this->name = $name;
320 $this->value = constant( $name );
323 function isExpired() {
324 return constant( $this->name ) != $this->value;