MediaWiki  REL1_24
CacheDependency.php
Go to the documentation of this file.
00001 <?php
00030 class DependencyWrapper {
00031     private $value;
00033     private $deps;
00034 
00041     function __construct( $value = false, $deps = array() ) {
00042         $this->value = $value;
00043 
00044         if ( !is_array( $deps ) ) {
00045             $deps = array( $deps );
00046         }
00047 
00048         $this->deps = $deps;
00049     }
00050 
00056     function isExpired() {
00057         foreach ( $this->deps as $dep ) {
00058             if ( $dep->isExpired() ) {
00059                 return true;
00060             }
00061         }
00062 
00063         return false;
00064     }
00065 
00070     function initialiseDeps() {
00071         foreach ( $this->deps as $dep ) {
00072             $dep->loadDependencyValues();
00073         }
00074     }
00075 
00080     function getValue() {
00081         return $this->value;
00082     }
00083 
00091     function storeToCache( $cache, $key, $expiry = 0 ) {
00092         $this->initialiseDeps();
00093         $cache->set( $key, $this, $expiry );
00094     }
00095 
00113     static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
00114         $callbackParams = array(), $deps = array()
00115     ) {
00116         $obj = $cache->get( $key );
00117 
00118         if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
00119             $value = $obj->value;
00120         } elseif ( $callback ) {
00121             $value = call_user_func_array( $callback, $callbackParams );
00122             # Cache the newly-generated value
00123             $wrapper = new DependencyWrapper( $value, $deps );
00124             $wrapper->storeToCache( $cache, $key, $expiry );
00125         } else {
00126             $value = null;
00127         }
00128 
00129         return $value;
00130     }
00131 }
00132 
00136 abstract class CacheDependency {
00140     abstract function isExpired();
00141 
00145     function loadDependencyValues() {
00146     }
00147 }
00148 
00152 class FileDependency extends CacheDependency {
00153     private $filename;
00154     private $timestamp;
00155 
00168     function __construct( $filename, $timestamp = null ) {
00169         $this->filename = $filename;
00170         $this->timestamp = $timestamp;
00171     }
00172 
00176     function __sleep() {
00177         $this->loadDependencyValues();
00178 
00179         return array( 'filename', 'timestamp' );
00180     }
00181 
00182     function loadDependencyValues() {
00183         if ( is_null( $this->timestamp ) ) {
00184             if ( !file_exists( $this->filename ) ) {
00185                 # Dependency on a non-existent file
00186                 # This is a valid concept!
00187                 $this->timestamp = false;
00188             } else {
00189                 $this->timestamp = filemtime( $this->filename );
00190             }
00191         }
00192     }
00193 
00197     function isExpired() {
00198         if ( !file_exists( $this->filename ) ) {
00199             if ( $this->timestamp === false ) {
00200                 # Still nonexistent
00201                 return false;
00202             } else {
00203                 # Deleted
00204                 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
00205 
00206                 return true;
00207             }
00208         } else {
00209             $lastmod = filemtime( $this->filename );
00210             if ( $lastmod > $this->timestamp ) {
00211                 # Modified or created
00212                 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
00213 
00214                 return true;
00215             } else {
00216                 # Not modified
00217                 return false;
00218             }
00219         }
00220     }
00221 }
00222 
00226 class GlobalDependency extends CacheDependency {
00227     private $name;
00228     private $value;
00229 
00230     function __construct( $name ) {
00231         $this->name = $name;
00232         $this->value = $GLOBALS[$name];
00233     }
00234 
00238     function isExpired() {
00239         if ( !isset( $GLOBALS[$this->name] ) ) {
00240             return true;
00241         }
00242 
00243         return $GLOBALS[$this->name] != $this->value;
00244     }
00245 }
00246 
00250 class ConstantDependency extends CacheDependency {
00251     private $name;
00252     private $value;
00253 
00254     function __construct( $name ) {
00255         $this->name = $name;
00256         $this->value = constant( $name );
00257     }
00258 
00262     function isExpired() {
00263         return constant( $this->name ) != $this->value;
00264     }
00265 }