MediaWiki  REL1_21
MultiWriteBagOStuff.php
Go to the documentation of this file.
00001 <?php
00031 class MultiWriteBagOStuff extends BagOStuff {
00032         var $caches;
00033 
00044         public function __construct( $params ) {
00045                 if ( !isset( $params['caches'] ) ) {
00046                         throw new MWException( __METHOD__.': the caches parameter is required' );
00047                 }
00048 
00049                 $this->caches = array();
00050                 foreach ( $params['caches'] as $cacheInfo ) {
00051                         $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
00052                 }
00053         }
00054 
00058         public function setDebug( $debug ) {
00059                 $this->doWrite( 'setDebug', $debug );
00060         }
00061 
00067         public function get( $key, &$casToken = null ) {
00068                 foreach ( $this->caches as $cache ) {
00069                         $value = $cache->get( $key );
00070                         if ( $value !== false ) {
00071                                 return $value;
00072                         }
00073                 }
00074                 return false;
00075         }
00076 
00084         public function cas( $casToken, $key, $value, $exptime = 0 ) {
00085                 throw new MWException( "CAS is not implemented in " . __CLASS__ );
00086         }
00087 
00094         public function set( $key, $value, $exptime = 0 ) {
00095                 return $this->doWrite( 'set', $key, $value, $exptime );
00096         }
00097 
00103         public function delete( $key, $time = 0 ) {
00104                 return $this->doWrite( 'delete', $key, $time );
00105         }
00106 
00113         public function add( $key, $value, $exptime = 0 ) {
00114                 return $this->doWrite( 'add', $key, $value, $exptime );
00115         }
00116 
00123         public function replace( $key, $value, $exptime = 0 ) {
00124                 return $this->doWrite( 'replace', $key, $value, $exptime );
00125         }
00126 
00132         public function incr( $key, $value = 1 ) {
00133                 return $this->doWrite( 'incr', $key, $value );
00134         }
00135 
00141         public function decr( $key, $value = 1 ) {
00142                 return $this->doWrite( 'decr', $key, $value );
00143         }
00144 
00150         public function lock( $key, $timeout = 0 ) {
00151                 // Lock only the first cache, to avoid deadlocks
00152                 if ( isset( $this->caches[0] ) ) {
00153                         return $this->caches[0]->lock( $key, $timeout );
00154                 } else {
00155                         return true;
00156                 }
00157         }
00158 
00163         public function unlock( $key ) {
00164                 if ( isset( $this->caches[0] ) ) {
00165                         return $this->caches[0]->unlock( $key );
00166                 } else {
00167                         return true;
00168                 }
00169         }
00170 
00178         public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
00179                 return $this->doWrite( 'merge', $key, $callback, $exptime );
00180         }
00181 
00186         protected function doWrite( $method /*, ... */ ) {
00187                 $ret = true;
00188                 $args = func_get_args();
00189                 array_shift( $args );
00190 
00191                 foreach ( $this->caches as $cache ) {
00192                         if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
00193                                 $ret = false;
00194                         }
00195                 }
00196                 return $ret;
00197         }
00198 
00207         public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
00208                 $ret = false;
00209                 foreach ( $this->caches as $cache ) {
00210                         if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
00211                                 $ret = true;
00212                         }
00213                 }
00214                 return $ret;
00215         }
00216 }