MediaWiki  REL1_24
MultiWriteBagOStuff.php
Go to the documentation of this file.
00001 <?php
00031 class MultiWriteBagOStuff extends BagOStuff {
00033     protected $caches;
00034 
00045     public function __construct( $params ) {
00046         if ( !isset( $params['caches'] ) ) {
00047             throw new MWException( __METHOD__ . ': the caches parameter is required' );
00048         }
00049 
00050         $this->caches = array();
00051         foreach ( $params['caches'] as $cacheInfo ) {
00052             $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
00053         }
00054     }
00055 
00059     public function setDebug( $debug ) {
00060         $this->doWrite( 'setDebug', $debug );
00061     }
00062 
00068     public function get( $key, &$casToken = null ) {
00069         foreach ( $this->caches as $cache ) {
00070             $value = $cache->get( $key );
00071             if ( $value !== false ) {
00072                 return $value;
00073             }
00074         }
00075         return false;
00076     }
00077 
00085     public function cas( $casToken, $key, $value, $exptime = 0 ) {
00086         throw new MWException( "CAS is not implemented in " . __CLASS__ );
00087     }
00088 
00095     public function set( $key, $value, $exptime = 0 ) {
00096         return $this->doWrite( 'set', $key, $value, $exptime );
00097     }
00098 
00104     public function delete( $key, $time = 0 ) {
00105         return $this->doWrite( 'delete', $key, $time );
00106     }
00107 
00114     public function add( $key, $value, $exptime = 0 ) {
00115         return $this->doWrite( 'add', $key, $value, $exptime );
00116     }
00117 
00123     public function incr( $key, $value = 1 ) {
00124         return $this->doWrite( 'incr', $key, $value );
00125     }
00126 
00132     public function decr( $key, $value = 1 ) {
00133         return $this->doWrite( 'decr', $key, $value );
00134     }
00135 
00141     public function lock( $key, $timeout = 0 ) {
00142         // Lock only the first cache, to avoid deadlocks
00143         if ( isset( $this->caches[0] ) ) {
00144             return $this->caches[0]->lock( $key, $timeout );
00145         } else {
00146             return true;
00147         }
00148     }
00149 
00154     public function unlock( $key ) {
00155         if ( isset( $this->caches[0] ) ) {
00156             return $this->caches[0]->unlock( $key );
00157         } else {
00158             return true;
00159         }
00160     }
00161 
00169     public function merge( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
00170         return $this->doWrite( 'merge', $key, $callback, $exptime );
00171     }
00172 
00173     public function getLastError() {
00174         return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
00175     }
00176 
00177     public function clearLastError() {
00178         if ( isset( $this->caches[0] ) ) {
00179             $this->caches[0]->clearLastError();
00180         }
00181     }
00182 
00187     protected function doWrite( $method /*, ... */ ) {
00188         $ret = true;
00189         $args = func_get_args();
00190         array_shift( $args );
00191 
00192         foreach ( $this->caches as $cache ) {
00193             if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
00194                 $ret = false;
00195             }
00196         }
00197         return $ret;
00198     }
00199 
00208     public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
00209         $ret = false;
00210         foreach ( $this->caches as $cache ) {
00211             if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
00212                 $ret = true;
00213             }
00214         }
00215         return $ret;
00216     }
00217 }