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 public function __construct( $params ) {
19 if ( !isset( $params['caches'] ) ) {
20 throw new MWException( __METHOD__
.': the caches parameter is required' );
23 $this->caches
= array();
24 foreach ( $params['caches'] as $cacheInfo ) {
25 $this->caches
[] = ObjectCache
::newFromParams( $cacheInfo );
29 public function setDebug( $debug ) {
30 $this->doWrite( 'setDebug', $debug );
33 public function get( $key ) {
34 foreach ( $this->caches
as $cache ) {
35 $value = $cache->get( $key );
36 if ( $value !== false ) {
43 public function set( $key, $value, $exptime = 0 ) {
44 return $this->doWrite( 'set', $key, $value, $exptime );
47 public function delete( $key, $time = 0 ) {
48 return $this->doWrite( 'delete', $key, $time );
51 public function add( $key, $value, $exptime = 0 ) {
52 return $this->doWrite( 'add', $key, $value, $exptime );
55 public function replace( $key, $value, $exptime = 0 ) {
56 return $this->doWrite( 'replace', $key, $value, $exptime );
59 public function incr( $key, $value = 1 ) {
60 return $this->doWrite( 'incr', $key, $value );
63 public function decr( $key, $value = 1 ) {
64 return $this->doWrite( 'decr', $key, $value );
67 public function lock( $key, $timeout = 0 ) {
68 // Lock only the first cache, to avoid deadlocks
69 if ( isset( $this->caches
[0] ) ) {
70 return $this->caches
[0]->lock( $key, $timeout );
76 public function unlock( $key ) {
77 if ( isset( $this->caches
[0] ) ) {
78 return $this->caches
[0]->unlock( $key );
84 protected function doWrite( $method /*, ... */ ) {
86 $args = func_get_args();
89 foreach ( $this->caches
as $cache ) {
90 $ret = $ret && call_user_func_array( array( $cache, $method ), $args );