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.
8 class DependencyWrapper
{
14 * @param mixed $value The user-supplied value
15 * @param mixed $deps 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 object $cache A cache object such as $wgMemc
69 * @param string $key The cache key
70 * @param integer $expiry The expiry timestamp or interval in seconds
71 * @param mixed $callback The callback for generating the value, or false
72 * @param array $callbackParams The function parameters for the callback
73 * @param array $deps 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 );
98 abstract class CacheDependency
{
100 * Returns true if the dependency is expired, false otherwise
102 abstract function isExpired();
105 * Hook to perform any expensive pre-serialize loading of dependency values.
107 function loadDependencyValues() {}
110 class FileDependency
extends CacheDependency
{
111 var $filename, $timestamp;
114 * Create a file dependency
116 * @param string $filename The name of the file, preferably fully qualified
117 * @param mixed $timestamp The unix last modified timestamp, or false if the
118 * file does not exist. If omitted, the timestamp will be loaded from
121 * A dependency on a nonexistent file will be triggered when the file is
122 * created. A dependency on an existing file will be triggered when the
125 function __construct( $filename, $timestamp = null ) {
126 $this->filename
= $filename;
127 $this->timestamp
= $timestamp;
130 function loadDependencyValues() {
131 if ( is_null( $this->timestamp
) ) {
132 if ( !file_exists( $this->filename
) ) {
133 # Dependency on a non-existent file
134 # This is a valid concept!
135 $this->timestamp
= false;
137 $this->timestamp
= filemtime( $this->filename
);
142 function isExpired() {
143 if ( !file_exists( $this->filename
) ) {
144 if ( $this->timestamp
=== false ) {
149 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
153 $lastmod = filemtime( $this->filename
);
154 if ( $lastmod > $this->timestamp
) {
155 # Modified or created
156 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
166 class TitleDependency
extends CacheDependency
{
172 * Construct a title dependency
173 * @param Title $title
175 function __construct( Title
$title ) {
176 $this->titleObj
= $title;
177 $this->ns
= $title->getNamespace();
178 $this->dbk
= $title->getDBkey();
181 function loadDependencyValues() {
182 $this->touched
= $this->getTitle()->getTouched();
186 * Get rid of bulky Title object for sleep
189 return array( 'ns', 'dbk', 'touched' );
192 function getTitle() {
193 if ( !isset( $this->titleObj
) ) {
194 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
196 return $this->titleObj
;
199 function isExpired() {
200 $touched = $this->getTitle()->getTouched();
201 if ( $this->touched
=== false ) {
202 if ( $touched === false ) {
209 } elseif ( $touched === false ) {
212 } elseif ( $touched > $this->touched
) {
222 class TitleListDependency
extends CacheDependency
{
227 * Construct a dependency on a list of titles
229 function __construct( LinkBatch
$linkBatch ) {
230 $this->linkBatch
= $linkBatch;
233 function calculateTimestamps() {
234 # Initialise values to false
235 $timestamps = array();
236 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
237 if ( count( $dbks ) > 0 ) {
238 $timestamps[$ns] = array();
239 foreach ( $dbks as $dbk => $value ) {
240 $timestamps[$ns][$dbk] = false;
246 if ( count( $timestamps ) ) {
247 $dbr =& wfGetDB( DB_SLAVE
);
248 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
249 $res = $dbr->select( 'page',
250 array( 'page_namespace', 'page_title', 'page_touched' ),
251 $where, __METHOD__
);
252 while ( $row = $dbr->fetchObject( $res ) ) {
253 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
259 function loadDependencyValues() {
260 $this->timestamps
= $this->calculateTimestamps();
264 return array( 'timestamps' );
267 function getLinkBatch() {
268 if ( !isset( $this->linkBatch
) ){
269 $this->linkBatch
= new LinkBatch
;
270 $this->linkBatch
->setArray( $this->timestamps
);
272 return $this->linkBatch
;
275 function isExpired() {
276 $newTimestamps = $this->calculateTimestamps();
277 foreach ( $this->timestamps
as $ns => $dbks ) {
278 foreach ( $dbks as $dbk => $oldTimestamp ) {
279 $newTimestamp = $newTimestamps[$ns][$dbk];
280 if ( $oldTimestamp === false ) {
281 if ( $newTimestamp === false ) {
287 } elseif ( $newTimestamp === false ) {
290 } elseif ( $newTimestamp > $oldTimestamp ) {
302 class GlobalDependency
extends CacheDependency
{
305 function __construct( $name ) {
307 $this->value
= $GLOBALS[$name];
310 function isExpired() {
311 return $GLOBALS[$this->name
] != $this->value
;
315 class ConstantDependency
extends CacheDependency
{
318 function __construct( $name ) {
320 $this->value
= constant( $name );
323 function isExpired() {
324 return constant( $this->name
) != $this->value
;