4 * A cache class that replicates all writes to multiple child caches. Reads
5 * are implemented by reading from the caches in the order they are given in
6 * the configuration until a cache gives a positive result.
8 class MultiWriteBagOStuff
extends BagOStuff
{
12 * Constructor. Parameters are:
14 * - caches: This should have a numbered array of cache parameter
15 * structures, in the style required by $wgObjectCaches. See
16 * the documentation of $wgObjectCaches for more detail.
18 * @param $params array
20 public function __construct( $params ) {
21 if ( !isset( $params['caches'] ) ) {
22 throw new MWException( __METHOD__
.': the caches parameter is required' );
25 $this->caches
= array();
26 foreach ( $params['caches'] as $cacheInfo ) {
27 $this->caches
[] = ObjectCache
::newFromParams( $cacheInfo );
31 public function setDebug( $debug ) {
32 $this->doWrite( 'setDebug', $debug );
35 public function get( $key ) {
36 foreach ( $this->caches
as $cache ) {
37 $value = $cache->get( $key );
38 if ( $value !== false ) {
45 public function set( $key, $value, $exptime = 0 ) {
46 return $this->doWrite( 'set', $key, $value, $exptime );
49 public function delete( $key, $time = 0 ) {
50 return $this->doWrite( 'delete', $key, $time );
53 public function add( $key, $value, $exptime = 0 ) {
54 return $this->doWrite( 'add', $key, $value, $exptime );
57 public function replace( $key, $value, $exptime = 0 ) {
58 return $this->doWrite( 'replace', $key, $value, $exptime );
61 public function incr( $key, $value = 1 ) {
62 return $this->doWrite( 'incr', $key, $value );
65 public function decr( $key, $value = 1 ) {
66 return $this->doWrite( 'decr', $key, $value );
69 public function lock( $key, $timeout = 0 ) {
70 // Lock only the first cache, to avoid deadlocks
71 if ( isset( $this->caches
[0] ) ) {
72 return $this->caches
[0]->lock( $key, $timeout );
78 public function unlock( $key ) {
79 if ( isset( $this->caches
[0] ) ) {
80 return $this->caches
[0]->unlock( $key );
86 protected function doWrite( $method /*, ... */ ) {
88 $args = func_get_args();
91 foreach ( $this->caches
as $cache ) {
92 $ret = $ret && call_user_func_array( array( $cache, $method ), $args );