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 function storeToCache( $cache, $key, $expiry = 0 ) {
63 $this->initialiseDeps();
64 $cache->set( $key, $this, $expiry );
68 * Attempt to get a value from the cache. If the value is expired or missing,
69 * it will be generated with the callback function (if present), and the newly
70 * calculated value will be stored to the cache in a wrapper.
72 * @param $cache Object: a cache object such as $wgMemc
73 * @param $key String: the cache key
74 * @param $expiry Integer: the expiry timestamp or interval in seconds
75 * @param $callback Mixed: the callback for generating the value, or false
76 * @param $callbackParams Array: the function parameters for the callback
77 * @param $deps Array: the dependencies to store on a cache miss. Note: these
78 * are not the dependencies used on a cache hit! Cache hits use the stored
81 * @return mixed The value, or null if it was not present in the cache and no
82 * callback was defined.
84 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
85 $callbackParams = array(), $deps = array() )
87 $obj = $cache->get( $key );
89 if ( is_object( $obj ) && $obj instanceof DependencyWrapper
&& !$obj->isExpired() ) {
91 } elseif ( $callback ) {
92 $value = call_user_func_array( $callback, $callbackParams );
93 # Cache the newly-generated value
94 $wrapper = new DependencyWrapper( $value, $deps );
95 $wrapper->storeToCache( $cache, $key, $expiry );
107 abstract class CacheDependency
{
109 * Returns true if the dependency is expired, false otherwise
111 abstract function isExpired();
114 * Hook to perform any expensive pre-serialize loading of dependency values.
116 function loadDependencyValues() { }
122 class FileDependency
extends CacheDependency
{
123 var $filename, $timestamp;
126 * Create a file dependency
128 * @param $filename String: the name of the file, preferably fully qualified
129 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
130 * file does not exist. If omitted, the timestamp will be loaded from
133 * A dependency on a nonexistent file will be triggered when the file is
134 * created. A dependency on an existing file will be triggered when the
137 function __construct( $filename, $timestamp = null ) {
138 $this->filename
= $filename;
139 $this->timestamp
= $timestamp;
143 $this->loadDependencyValues();
144 return array( 'filename', 'timestamp' );
147 function loadDependencyValues() {
148 if ( is_null( $this->timestamp
) ) {
149 if ( !file_exists( $this->filename
) ) {
150 # Dependency on a non-existent file
151 # This is a valid concept!
152 $this->timestamp
= false;
154 $this->timestamp
= filemtime( $this->filename
);
159 function isExpired() {
160 if ( !file_exists( $this->filename
) ) {
161 if ( $this->timestamp
=== false ) {
166 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
170 $lastmod = filemtime( $this->filename
);
171 if ( $lastmod > $this->timestamp
) {
172 # Modified or created
173 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
186 class TitleDependency
extends CacheDependency
{
192 * Construct a title dependency
193 * @param $title Title
195 function __construct( Title
$title ) {
196 $this->titleObj
= $title;
197 $this->ns
= $title->getNamespace();
198 $this->dbk
= $title->getDBkey();
201 function loadDependencyValues() {
202 $this->touched
= $this->getTitle()->getTouched();
206 * Get rid of bulky Title object for sleep
209 return array( 'ns', 'dbk', 'touched' );
212 function getTitle() {
213 if ( !isset( $this->titleObj
) ) {
214 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
217 return $this->titleObj
;
220 function isExpired() {
221 $touched = $this->getTitle()->getTouched();
223 if ( $this->touched
=== false ) {
224 if ( $touched === false ) {
231 } elseif ( $touched === false ) {
234 } elseif ( $touched > $this->touched
) {
247 class TitleListDependency
extends CacheDependency
{
252 * Construct a dependency on a list of titles
254 function __construct( LinkBatch
$linkBatch ) {
255 $this->linkBatch
= $linkBatch;
258 function calculateTimestamps() {
259 # Initialise values to false
260 $timestamps = array();
262 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
263 if ( count( $dbks ) > 0 ) {
264 $timestamps[$ns] = array();
266 foreach ( $dbks as $dbk => $value ) {
267 $timestamps[$ns][$dbk] = false;
273 if ( count( $timestamps ) ) {
274 $dbr = wfGetDB( DB_SLAVE
);
275 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
278 array( 'page_namespace', 'page_title', 'page_touched' ),
283 foreach ( $res as $row ) {
284 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
291 function loadDependencyValues() {
292 $this->timestamps
= $this->calculateTimestamps();
296 return array( 'timestamps' );
299 function getLinkBatch() {
300 if ( !isset( $this->linkBatch
) ) {
301 $this->linkBatch
= new LinkBatch
;
302 $this->linkBatch
->setArray( $this->timestamps
);
304 return $this->linkBatch
;
307 function isExpired() {
308 $newTimestamps = $this->calculateTimestamps();
310 foreach ( $this->timestamps
as $ns => $dbks ) {
311 foreach ( $dbks as $dbk => $oldTimestamp ) {
312 $newTimestamp = $newTimestamps[$ns][$dbk];
314 if ( $oldTimestamp === false ) {
315 if ( $newTimestamp === false ) {
321 } elseif ( $newTimestamp === false ) {
324 } elseif ( $newTimestamp > $oldTimestamp ) {
340 class GlobalDependency
extends CacheDependency
{
343 function __construct( $name ) {
345 $this->value
= $GLOBALS[$name];
348 function isExpired() {
349 return $GLOBALS[$this->name
] != $this->value
;
356 class ConstantDependency
extends CacheDependency
{
359 function __construct( $name ) {
361 $this->value
= constant( $name );
364 function isExpired() {
365 return constant( $this->name
) != $this->value
;