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.
9 class DependencyWrapper
{
15 * @param mixed $value The user-supplied value
16 * @param mixed $deps A dependency or dependency array. All dependencies
17 * must be objects implementing CacheDependency.
19 function __construct( $value = false, $deps = array() ) {
20 $this->value
= $value;
21 if ( !is_array( $deps ) ) {
22 $deps = array( $deps );
28 * Returns true if any of the dependencies have expired
30 function isExpired() {
31 foreach ( $this->deps
as $dep ) {
32 if ( $dep->isExpired() ) {
40 * Initialise dependency values in preparation for storing. This must be
41 * called before serialization.
43 function initialiseDeps() {
44 foreach ( $this->deps
as $dep ) {
45 $dep->loadDependencyValues();
50 * Get the user-defined value
57 * Store the wrapper to a cache
59 function storeToCache( $cache, $key, $expiry = 0 ) {
60 $this->initialiseDeps();
61 $cache->set( $key, $this, $expiry );
65 * Attempt to get a value from the cache. If the value is expired or missing,
66 * it will be generated with the callback function (if present), and the newly
67 * calculated value will be stored to the cache in a wrapper.
69 * @param object $cache A cache object such as $wgMemc
70 * @param string $key The cache key
71 * @param integer $expiry The expiry timestamp or interval in seconds
72 * @param mixed $callback The callback for generating the value, or false
73 * @param array $callbackParams The function parameters for the callback
74 * @param array $deps The dependencies to store on a cache miss. Note: these
75 * are not the dependencies used on a cache hit! Cache hits use the stored
78 * @return mixed The value, or null if it was not present in the cache and no
79 * callback was defined.
81 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
82 $callbackParams = array(), $deps = array() )
84 $obj = $cache->get( $key );
85 if ( is_object( $obj ) && $obj instanceof DependencyWrapper
&& !$obj->isExpired() ) {
87 } elseif ( $callback ) {
88 $value = call_user_func_array( $callback, $callbackParams );
89 # Cache the newly-generated value
90 $wrapper = new DependencyWrapper( $value, $deps );
91 $wrapper->storeToCache( $cache, $key, $expiry );
102 abstract class CacheDependency
{
104 * Returns true if the dependency is expired, false otherwise
106 abstract function isExpired();
109 * Hook to perform any expensive pre-serialize loading of dependency values.
111 function loadDependencyValues() {}
117 class FileDependency
extends CacheDependency
{
118 var $filename, $timestamp;
121 * Create a file dependency
123 * @param string $filename The name of the file, preferably fully qualified
124 * @param mixed $timestamp The unix last modified timestamp, or false if the
125 * file does not exist. If omitted, the timestamp will be loaded from
128 * A dependency on a nonexistent file will be triggered when the file is
129 * created. A dependency on an existing file will be triggered when the
132 function __construct( $filename, $timestamp = null ) {
133 $this->filename
= $filename;
134 $this->timestamp
= $timestamp;
138 $this->loadDependencyValues();
139 return array( 'filename', 'timestamp' );
142 function loadDependencyValues() {
143 if ( is_null( $this->timestamp
) ) {
144 if ( !file_exists( $this->filename
) ) {
145 # Dependency on a non-existent file
146 # This is a valid concept!
147 $this->timestamp
= false;
149 $this->timestamp
= filemtime( $this->filename
);
154 function isExpired() {
155 if ( !file_exists( $this->filename
) ) {
156 if ( $this->timestamp
=== false ) {
161 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
165 $lastmod = filemtime( $this->filename
);
166 if ( $lastmod > $this->timestamp
) {
167 # Modified or created
168 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
181 class TitleDependency
extends CacheDependency
{
187 * Construct a title dependency
188 * @param Title $title
190 function __construct( Title
$title ) {
191 $this->titleObj
= $title;
192 $this->ns
= $title->getNamespace();
193 $this->dbk
= $title->getDBkey();
196 function loadDependencyValues() {
197 $this->touched
= $this->getTitle()->getTouched();
201 * Get rid of bulky Title object for sleep
204 return array( 'ns', 'dbk', 'touched' );
207 function getTitle() {
208 if ( !isset( $this->titleObj
) ) {
209 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
211 return $this->titleObj
;
214 function isExpired() {
215 $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();
254 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
255 if ( count( $dbks ) > 0 ) {
256 $timestamps[$ns] = array();
257 foreach ( $dbks as $dbk => $value ) {
258 $timestamps[$ns][$dbk] = false;
264 if ( count( $timestamps ) ) {
265 $dbr = wfGetDB( DB_SLAVE
);
266 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
267 $res = $dbr->select( 'page',
268 array( 'page_namespace', 'page_title', 'page_touched' ),
269 $where, __METHOD__
);
270 while ( $row = $dbr->fetchObject( $res ) ) {
271 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
277 function loadDependencyValues() {
278 $this->timestamps
= $this->calculateTimestamps();
282 return array( 'timestamps' );
285 function getLinkBatch() {
286 if ( !isset( $this->linkBatch
) ){
287 $this->linkBatch
= new LinkBatch
;
288 $this->linkBatch
->setArray( $this->timestamps
);
290 return $this->linkBatch
;
293 function isExpired() {
294 $newTimestamps = $this->calculateTimestamps();
295 foreach ( $this->timestamps
as $ns => $dbks ) {
296 foreach ( $dbks as $dbk => $oldTimestamp ) {
297 $newTimestamp = $newTimestamps[$ns][$dbk];
298 if ( $oldTimestamp === false ) {
299 if ( $newTimestamp === false ) {
305 } elseif ( $newTimestamp === false ) {
308 } elseif ( $newTimestamp > $oldTimestamp ) {
323 class GlobalDependency
extends CacheDependency
{
326 function __construct( $name ) {
328 $this->value
= $GLOBALS[$name];
331 function isExpired() {
332 return $GLOBALS[$this->name
] != $this->value
;
339 class ConstantDependency
extends CacheDependency
{
342 function __construct( $name ) {
344 $this->value
= constant( $name );
347 function isExpired() {
348 return constant( $this->name
) != $this->value
;