3 * This class stores an arbitrary value along with its dependencies.
4 * Users should typically only use DependencyWrapper::getFromCache(), rather
5 * than instantiating one of these objects directly.
8 class DependencyWrapper
{
14 * @param $value Mixed: the user-supplied value
15 * @param $deps Mixed: 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 );
27 * Returns true if any of the dependencies have expired
29 function isExpired() {
30 foreach ( $this->deps
as $dep ) {
31 if ( $dep->isExpired() ) {
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();
49 * Get the user-defined value
56 * Store the wrapper to a cache
58 function storeToCache( $cache, $key, $expiry = 0 ) {
59 $this->initialiseDeps();
60 $cache->set( $key, $this, $expiry );
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 $cache Object: a cache object such as $wgMemc
69 * @param $key String: the cache key
70 * @param $expiry Integer: the expiry timestamp or interval in seconds
71 * @param $callback Mixed: the callback for generating the value, or false
72 * @param $callbackParams Array: the function parameters for the callback
73 * @param $deps Array: 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
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() ) {
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 );
101 abstract class CacheDependency
{
103 * Returns true if the dependency is expired, false otherwise
105 abstract function isExpired();
108 * Hook to perform any expensive pre-serialize loading of dependency values.
110 function loadDependencyValues() { }
116 class FileDependency
extends CacheDependency
{
117 var $filename, $timestamp;
120 * Create a file dependency
122 * @param $filename String: the name of the file, preferably fully qualified
123 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
124 * file does not exist. If omitted, the timestamp will be loaded from
127 * A dependency on a nonexistent file will be triggered when the file is
128 * created. A dependency on an existing file will be triggered when the
131 function __construct( $filename, $timestamp = null ) {
132 $this->filename
= $filename;
133 $this->timestamp
= $timestamp;
137 $this->loadDependencyValues();
138 return array( 'filename', 'timestamp' );
141 function loadDependencyValues() {
142 if ( is_null( $this->timestamp
) ) {
143 if ( !file_exists( $this->filename
) ) {
144 # Dependency on a non-existent file
145 # This is a valid concept!
146 $this->timestamp
= false;
148 $this->timestamp
= filemtime( $this->filename
);
153 function isExpired() {
154 if ( !file_exists( $this->filename
) ) {
155 if ( $this->timestamp
=== false ) {
160 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
164 $lastmod = filemtime( $this->filename
);
165 if ( $lastmod > $this->timestamp
) {
166 # Modified or created
167 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
180 class TitleDependency
extends CacheDependency
{
186 * Construct a title dependency
187 * @param $title Title
189 function __construct( Title
$title ) {
190 $this->titleObj
= $title;
191 $this->ns
= $title->getNamespace();
192 $this->dbk
= $title->getDBkey();
195 function loadDependencyValues() {
196 $this->touched
= $this->getTitle()->getTouched();
200 * Get rid of bulky Title object for sleep
203 return array( 'ns', 'dbk', 'touched' );
206 function getTitle() {
207 if ( !isset( $this->titleObj
) ) {
208 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
210 return $this->titleObj
;
213 function isExpired() {
214 $touched = $this->getTitle()->getTouched();
216 if ( $this->touched
=== false ) {
217 if ( $touched === false ) {
224 } elseif ( $touched === false ) {
227 } elseif ( $touched > $this->touched
) {
240 class TitleListDependency
extends CacheDependency
{
245 * Construct a dependency on a list of titles
247 function __construct( LinkBatch
$linkBatch ) {
248 $this->linkBatch
= $linkBatch;
251 function calculateTimestamps() {
252 # Initialise values to false
253 $timestamps = array();
255 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
256 if ( count( $dbks ) > 0 ) {
257 $timestamps[$ns] = array();
258 foreach ( $dbks as $dbk => $value ) {
259 $timestamps[$ns][$dbk] = false;
265 if ( count( $timestamps ) ) {
266 $dbr = wfGetDB( DB_SLAVE
);
267 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
270 array( 'page_namespace', 'page_title', 'page_touched' ),
275 while ( $row = $dbr->fetchObject( $res ) ) {
276 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
282 function loadDependencyValues() {
283 $this->timestamps
= $this->calculateTimestamps();
287 return array( 'timestamps' );
290 function getLinkBatch() {
291 if ( !isset( $this->linkBatch
) ) {
292 $this->linkBatch
= new LinkBatch
;
293 $this->linkBatch
->setArray( $this->timestamps
);
295 return $this->linkBatch
;
298 function isExpired() {
299 $newTimestamps = $this->calculateTimestamps();
300 foreach ( $this->timestamps
as $ns => $dbks ) {
301 foreach ( $dbks as $dbk => $oldTimestamp ) {
302 $newTimestamp = $newTimestamps[$ns][$dbk];
304 if ( $oldTimestamp === false ) {
305 if ( $newTimestamp === false ) {
311 } elseif ( $newTimestamp === false ) {
314 } elseif ( $newTimestamp > $oldTimestamp ) {
329 class GlobalDependency
extends CacheDependency
{
332 function __construct( $name ) {
334 $this->value
= $GLOBALS[$name];
337 function isExpired() {
338 return $GLOBALS[$this->name
] != $this->value
;
345 class ConstantDependency
extends CacheDependency
{
348 function __construct( $name ) {
350 $this->value
= constant( $name );
353 function isExpired() {
354 return constant( $this->name
) != $this->value
;