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;
137 function loadDependencyValues() {
138 if ( is_null( $this->timestamp
) ) {
139 if ( !file_exists( $this->filename
) ) {
140 # Dependency on a non-existent file
141 # This is a valid concept!
142 $this->timestamp
= false;
144 $this->timestamp
= filemtime( $this->filename
);
149 function isExpired() {
150 if ( !file_exists( $this->filename
) ) {
151 if ( $this->timestamp
=== false ) {
156 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
160 $lastmod = filemtime( $this->filename
);
161 if ( $lastmod > $this->timestamp
) {
162 # Modified or created
163 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
176 class TitleDependency
extends CacheDependency
{
182 * Construct a title dependency
183 * @param Title $title
185 function __construct( Title
$title ) {
186 $this->titleObj
= $title;
187 $this->ns
= $title->getNamespace();
188 $this->dbk
= $title->getDBkey();
191 function loadDependencyValues() {
192 $this->touched
= $this->getTitle()->getTouched();
196 * Get rid of bulky Title object for sleep
199 return array( 'ns', 'dbk', 'touched' );
202 function getTitle() {
203 if ( !isset( $this->titleObj
) ) {
204 $this->titleObj
= Title
::makeTitle( $this->ns
, $this->dbk
);
206 return $this->titleObj
;
209 function isExpired() {
210 $touched = $this->getTitle()->getTouched();
211 if ( $this->touched
=== false ) {
212 if ( $touched === false ) {
219 } elseif ( $touched === false ) {
222 } elseif ( $touched > $this->touched
) {
235 class TitleListDependency
extends CacheDependency
{
240 * Construct a dependency on a list of titles
242 function __construct( LinkBatch
$linkBatch ) {
243 $this->linkBatch
= $linkBatch;
246 function calculateTimestamps() {
247 # Initialise values to false
248 $timestamps = array();
249 foreach ( $this->getLinkBatch()->data
as $ns => $dbks ) {
250 if ( count( $dbks ) > 0 ) {
251 $timestamps[$ns] = array();
252 foreach ( $dbks as $dbk => $value ) {
253 $timestamps[$ns][$dbk] = false;
259 if ( count( $timestamps ) ) {
260 $dbr = wfGetDB( DB_SLAVE
);
261 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
262 $res = $dbr->select( 'page',
263 array( 'page_namespace', 'page_title', 'page_touched' ),
264 $where, __METHOD__
);
265 while ( $row = $dbr->fetchObject( $res ) ) {
266 $timestamps[$row->page_namespace
][$row->page_title
] = $row->page_touched
;
272 function loadDependencyValues() {
273 $this->timestamps
= $this->calculateTimestamps();
277 return array( 'timestamps' );
280 function getLinkBatch() {
281 if ( !isset( $this->linkBatch
) ){
282 $this->linkBatch
= new LinkBatch
;
283 $this->linkBatch
->setArray( $this->timestamps
);
285 return $this->linkBatch
;
288 function isExpired() {
289 $newTimestamps = $this->calculateTimestamps();
290 foreach ( $this->timestamps
as $ns => $dbks ) {
291 foreach ( $dbks as $dbk => $oldTimestamp ) {
292 $newTimestamp = $newTimestamps[$ns][$dbk];
293 if ( $oldTimestamp === false ) {
294 if ( $newTimestamp === false ) {
300 } elseif ( $newTimestamp === false ) {
303 } elseif ( $newTimestamp > $oldTimestamp ) {
318 class GlobalDependency
extends CacheDependency
{
321 function __construct( $name ) {
323 $this->value
= $GLOBALS[$name];
326 function isExpired() {
327 return $GLOBALS[$this->name
] != $this->value
;
334 class ConstantDependency
extends CacheDependency
{
337 function __construct( $name ) {
339 $this->value
= constant( $name );
342 function isExpired() {
343 return constant( $this->name
) != $this->value
;