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.
9 class DependencyWrapper
{
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 );
30 * Returns true if any of the dependencies have expired
34 function isExpired() {
35 foreach ( $this->deps
as $dep ) {
36 if ( $dep->isExpired() ) {
45 * Initialise dependency values in preparation for storing. This must be
46 * called before serialization.
48 function initialiseDeps() {
49 foreach ( $this->deps
as $dep ) {
50 $dep->loadDependencyValues();
55 * Get the user-defined value
63 * Store the wrapper to a cache
65 * @param $cache BagOStuff
69 function storeToCache( $cache, $key, $expiry = 0 ) {
70 $this->initialiseDeps();
71 $cache->set( $key, $this, $expiry );
75 * Attempt to get a value from the cache. If the value is expired or missing,
76 * it will be generated with the callback function (if present), and the newly
77 * calculated value will be stored to the cache in a wrapper.
79 * @param $cache BagOStuff a cache object such as $wgMemc
80 * @param $key String: the cache key
81 * @param $expiry Integer: the expiry timestamp or interval in seconds
82 * @param $callback Mixed: the callback for generating the value, or false
83 * @param $callbackParams Array: the function parameters for the callback
84 * @param $deps Array: the dependencies to store on a cache miss. Note: these
85 * are not the dependencies used on a cache hit! Cache hits use the stored
88 * @return mixed The value, or null if it was not present in the cache and no
89 * callback was defined.
91 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
92 $callbackParams = array(), $deps = array() )
94 $obj = $cache->get( $key );
96 if ( is_object( $obj ) && $obj instanceof DependencyWrapper
&& !$obj->isExpired() ) {
98 } elseif ( $callback ) {
99 $value = call_user_func_array( $callback, $callbackParams );
100 # Cache the newly-generated value
101 $wrapper = new DependencyWrapper( $value, $deps );
102 $wrapper->storeToCache( $cache, $key, $expiry );
114 abstract class CacheDependency
{
116 * Returns true if the dependency is expired, false otherwise
118 abstract function isExpired();
121 * Hook to perform any expensive pre-serialize loading of dependency values.
123 function loadDependencyValues() { }
129 class FileDependency
extends CacheDependency
{
130 var $filename, $timestamp;
133 * Create a file dependency
135 * @param $filename String: the name of the file, preferably fully qualified
136 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
137 * file does not exist. If omitted, the timestamp will be loaded from
140 * A dependency on a nonexistent file will be triggered when the file is
141 * created. A dependency on an existing file will be triggered when the
144 function __construct( $filename, $timestamp = null ) {
145 $this->filename
= $filename;
146 $this->timestamp
= $timestamp;
153 $this->loadDependencyValues();
154 return array( 'filename', 'timestamp' );
157 function loadDependencyValues() {
158 if ( is_null( $this->timestamp
) ) {
159 if ( !file_exists( $this->filename
) ) {
160 # Dependency on a non-existent file
161 # This is a valid concept!
162 $this->timestamp
= false;
164 $this->timestamp
= filemtime( $this->filename
);
172 function isExpired() {
173 if ( !file_exists( $this->filename
) ) {
174 if ( $this->timestamp
=== false ) {
179 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
183 $lastmod = filemtime( $this->filename
);
184 if ( $lastmod > $this->timestamp
) {
185 # Modified or created
186 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
199 class TitleDependency
extends CacheDependency
{
205 * Construct a title dependency
206 * @param $title Title
208 function __construct( Title
$title ) {
209 $this->titleObj
= $title;
210 $this->ns
= $title->getNamespace();
211 $this->dbk
= $title->getDBkey();
214 function loadDependencyValues() {
215 $this->touched
= $this->getTitle()->getTouched();
219 * Get rid of bulky Title object for sleep
224 return array( 'ns', 'dbk', 'touched' );
230 function getTitle() {
231 if ( !isset( $this->titleObj
) ) {
232 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
235 return $this->titleObj
;
241 function isExpired() {
242 $touched = $this->getTitle()->getTouched();
244 if ( $this->touched
=== false ) {
245 if ( $touched === false ) {
252 } elseif ( $touched === false ) {
255 } elseif ( $touched > $this->touched
) {
268 class TitleListDependency
extends CacheDependency
{
273 * Construct a dependency on a list of titles
274 * @param $linkBatch LinkBatch
276 function __construct( LinkBatch
$linkBatch ) {
277 $this->linkBatch
= $linkBatch;
283 function calculateTimestamps() {
284 # Initialise values to false
285 $timestamps = array();
287 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
288 if ( count( $dbks ) > 0 ) {
289 $timestamps[$ns] = array();
291 foreach ( $dbks as $dbk => $value ) {
292 $timestamps[$ns][$dbk] = false;
298 if ( count( $timestamps ) ) {
299 $dbr = wfGetDB( DB_SLAVE
);
300 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
303 array( 'page_namespace', 'page_title', 'page_touched' ),
308 foreach ( $res as $row ) {
309 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
316 function loadDependencyValues() {
317 $this->timestamps
= $this->calculateTimestamps();
324 return array( 'timestamps' );
330 function getLinkBatch() {
331 if ( !isset( $this->linkBatch
) ) {
332 $this->linkBatch
= new LinkBatch
;
333 $this->linkBatch
->setArray( $this->timestamps
);
335 return $this->linkBatch
;
341 function isExpired() {
342 $newTimestamps = $this->calculateTimestamps();
344 foreach ( $this->timestamps
as $ns => $dbks ) {
345 foreach ( $dbks as $dbk => $oldTimestamp ) {
346 $newTimestamp = $newTimestamps[$ns][$dbk];
348 if ( $oldTimestamp === false ) {
349 if ( $newTimestamp === false ) {
355 } elseif ( $newTimestamp === false ) {
358 } elseif ( $newTimestamp > $oldTimestamp ) {
374 class GlobalDependency
extends CacheDependency
{
377 function __construct( $name ) {
379 $this->value
= $GLOBALS[$name];
385 function isExpired() {
386 if( !isset($GLOBALS[$this->name
]) ) {
389 return $GLOBALS[$this->name
] != $this->value
;
396 class ConstantDependency
extends CacheDependency
{
399 function __construct( $name ) {
401 $this->value
= constant( $name );
407 function isExpired() {
408 return constant( $this->name
) != $this->value
;