aa0206643b58a506fe36d593d3ce261dd8c8318d
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
32 function isExpired() {
33 foreach ( $this->deps
as $dep ) {
34 if ( $dep->isExpired() ) {
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();
53 * Get the user-defined value
60 * Store the wrapper to a cache
62 * @param $cache BagOStuff
66 function storeToCache( $cache, $key, $expiry = 0 ) {
67 $this->initialiseDeps();
68 $cache->set( $key, $this, $expiry );
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
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() ) {
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 );
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() { }
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
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
141 function __construct( $filename, $timestamp = null ) {
142 $this->filename
= $filename;
143 $this->timestamp
= $timestamp;
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;
158 $this->timestamp
= filemtime( $this->filename
);
166 function isExpired() {
167 if ( !file_exists( $this->filename
) ) {
168 if ( $this->timestamp
=== false ) {
173 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
177 $lastmod = filemtime( $this->filename
);
178 if ( $lastmod > $this->timestamp
) {
179 # Modified or created
180 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
193 class TitleDependency
extends CacheDependency
{
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
218 return array( 'ns', 'dbk', 'touched' );
224 function getTitle() {
225 if ( !isset( $this->titleObj
) ) {
226 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
229 return $this->titleObj
;
235 function isExpired() {
236 $touched = $this->getTitle()->getTouched();
238 if ( $this->touched
=== false ) {
239 if ( $touched === false ) {
246 } elseif ( $touched === false ) {
249 } elseif ( $touched > $this->touched
) {
262 class TitleListDependency
extends CacheDependency
{
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;
288 if ( count( $timestamps ) ) {
289 $dbr = wfGetDB( DB_SLAVE
);
290 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
293 array( 'page_namespace', 'page_title', 'page_touched' ),
298 foreach ( $res as $row ) {
299 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
306 function loadDependencyValues() {
307 $this->timestamps
= $this->calculateTimestamps();
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
;
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 ) {
342 } elseif ( $newTimestamp === false ) {
345 } elseif ( $newTimestamp > $oldTimestamp ) {
361 class GlobalDependency
extends CacheDependency
{
364 function __construct( $name ) {
366 $this->value
= $GLOBALS[$name];
372 function isExpired() {
373 return $GLOBALS[$this->name
] != $this->value
;
380 class ConstantDependency
extends CacheDependency
{
383 function __construct( $name ) {
385 $this->value
= constant( $name );
391 function isExpired() {
392 return constant( $this->name
) != $this->value
;