Simplify $assoc check
[mediawiki.git] / includes / objectcache / MultiWriteBagOStuff.php
blob0d95a846d66b5a91155b2f9b922658034c3d7292
1 <?php
3 /**
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.
7 */
8 class MultiWriteBagOStuff extends BagOStuff {
9 var $caches;
11 /**
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 ) {
39 return $value;
42 return 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 );
73 } else {
74 return true;
78 public function unlock( $key ) {
79 if ( isset( $this->caches[0] ) ) {
80 return $this->caches[0]->unlock( $key );
81 } else {
82 return true;
86 protected function doWrite( $method /*, ... */ ) {
87 $ret = true;
88 $args = func_get_args();
89 array_shift( $args );
91 foreach ( $this->caches as $cache ) {
92 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
93 $ret = false;
96 return $ret;
99 /**
100 * Delete objects expiring before a certain date.
102 * Succeed if any of the child caches succeed.
104 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
105 $ret = false;
106 foreach ( $this->caches as $cache ) {
107 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
108 $ret = true;
111 return $ret;