MediaWiki  REL1_19
MultiWriteBagOStuff.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class MultiWriteBagOStuff extends BagOStuff {
00009         var $caches;
00010 
00020         public function __construct( $params ) {
00021                 if ( !isset( $params['caches'] ) ) {
00022                         throw new MWException( __METHOD__.': the caches parameter is required' );
00023                 }
00024 
00025                 $this->caches = array();
00026                 foreach ( $params['caches'] as $cacheInfo ) {
00027                         $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
00028                 }
00029         }
00030 
00031         public function setDebug( $debug ) {
00032                 $this->doWrite( 'setDebug', $debug );
00033         }
00034 
00035         public function get( $key ) {
00036                 foreach ( $this->caches as $cache ) {
00037                         $value = $cache->get( $key );
00038                         if ( $value !== false ) {
00039                                 return $value;
00040                         }
00041                 }
00042                 return false;
00043         }
00044 
00045         public function set( $key, $value, $exptime = 0 ) {
00046                 return $this->doWrite( 'set', $key, $value, $exptime );
00047         }
00048 
00049         public function delete( $key, $time = 0 ) {
00050                 return $this->doWrite( 'delete', $key, $time );
00051         }
00052 
00053         public function add( $key, $value, $exptime = 0 ) {
00054                 return $this->doWrite( 'add', $key, $value, $exptime );
00055         }
00056 
00057         public function replace( $key, $value, $exptime = 0 ) {
00058                 return $this->doWrite( 'replace', $key, $value, $exptime );
00059         }
00060 
00061         public function incr( $key, $value = 1 ) {
00062                 return $this->doWrite( 'incr', $key, $value );
00063         }
00064 
00065         public function decr( $key, $value = 1 ) {
00066                 return $this->doWrite( 'decr', $key, $value );
00067         }       
00068 
00069         public function lock( $key, $timeout = 0 ) {
00070                 // Lock only the first cache, to avoid deadlocks
00071                 if ( isset( $this->caches[0] ) ) {
00072                         return $this->caches[0]->lock( $key, $timeout );
00073                 } else {
00074                         return true;
00075                 }
00076         }
00077 
00078         public function unlock( $key ) {
00079                 if ( isset( $this->caches[0] ) ) {
00080                         return $this->caches[0]->unlock( $key );
00081                 } else {
00082                         return true;
00083                 }
00084         }
00085 
00086         protected function doWrite( $method /*, ... */ ) {
00087                 $ret = true;
00088                 $args = func_get_args();
00089                 array_shift( $args );
00090 
00091                 foreach ( $this->caches as $cache ) {
00092                         if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
00093                                 $ret = false;
00094                         }
00095                 }
00096                 return $ret;
00097         }
00098 
00104         public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
00105                 $ret = false;
00106                 foreach ( $this->caches as $cache ) {
00107                         if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
00108                                 $ret = true;
00109                         }
00110                 }
00111                 return $ret;
00112         }
00113 }