MediaWiki  REL1_20
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 
00066         public function get( $key ) {
00067                 foreach ( $this->caches as $cache ) {
00068                         $value = $cache->get( $key );
00069                         if ( $value !== false ) {
00070                                 return $value;
00071                         }
00072                 }
00073                 return false;
00074         }
00075 
00082         public function set( $key, $value, $exptime = 0 ) {
00083                 return $this->doWrite( 'set', $key, $value, $exptime );
00084         }
00085 
00091         public function delete( $key, $time = 0 ) {
00092                 return $this->doWrite( 'delete', $key, $time );
00093         }
00094 
00101         public function add( $key, $value, $exptime = 0 ) {
00102                 return $this->doWrite( 'add', $key, $value, $exptime );
00103         }
00104 
00111         public function replace( $key, $value, $exptime = 0 ) {
00112                 return $this->doWrite( 'replace', $key, $value, $exptime );
00113         }
00114 
00120         public function incr( $key, $value = 1 ) {
00121                 return $this->doWrite( 'incr', $key, $value );
00122         }
00123 
00129         public function decr( $key, $value = 1 ) {
00130                 return $this->doWrite( 'decr', $key, $value );
00131         }
00132 
00138         public function lock( $key, $timeout = 0 ) {
00139                 // Lock only the first cache, to avoid deadlocks
00140                 if ( isset( $this->caches[0] ) ) {
00141                         return $this->caches[0]->lock( $key, $timeout );
00142                 } else {
00143                         return true;
00144                 }
00145         }
00146 
00151         public function unlock( $key ) {
00152                 if ( isset( $this->caches[0] ) ) {
00153                         return $this->caches[0]->unlock( $key );
00154                 } else {
00155                         return true;
00156                 }
00157         }
00158 
00163         protected function doWrite( $method /*, ... */ ) {
00164                 $ret = true;
00165                 $args = func_get_args();
00166                 array_shift( $args );
00167 
00168                 foreach ( $this->caches as $cache ) {
00169                         if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
00170                                 $ret = false;
00171                         }
00172                 }
00173                 return $ret;
00174         }
00175 
00184         public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
00185                 $ret = false;
00186                 foreach ( $this->caches as $cache ) {
00187                         if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
00188                                 $ret = true;
00189                         }
00190                 }
00191                 return $ret;
00192         }
00193 }