three new hooks in SpecialUndelete.php from Wikia codebase so that extensions, such...
[lhc/web/wiklou.git] / includes / objectcache / MultiWriteBagOStuff.php
1 <?php
2
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;
10
11 /**
12 * Constructor. Parameters are:
13 *
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.
17 */
18 public function __construct( $params ) {
19 if ( !isset( $params['caches'] ) ) {
20 throw new MWException( __METHOD__.': the caches parameter is required' );
21 }
22
23 $this->caches = array();
24 foreach ( $params['caches'] as $cacheInfo ) {
25 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
26 }
27 }
28
29 public function setDebug( $debug ) {
30 $this->doWrite( 'setDebug', $debug );
31 }
32
33 public function get( $key ) {
34 foreach ( $this->caches as $cache ) {
35 $value = $cache->get( $key );
36 if ( $value !== false ) {
37 return $value;
38 }
39 }
40 return false;
41 }
42
43 public function set( $key, $value, $exptime = 0 ) {
44 return $this->doWrite( 'set', $key, $value, $exptime );
45 }
46
47 public function delete( $key, $time = 0 ) {
48 return $this->doWrite( 'delete', $key, $time );
49 }
50
51 public function add( $key, $value, $exptime = 0 ) {
52 return $this->doWrite( 'add', $key, $value, $exptime );
53 }
54
55 public function replace( $key, $value, $exptime = 0 ) {
56 return $this->doWrite( 'replace', $key, $value, $exptime );
57 }
58
59 public function incr( $key, $value = 1 ) {
60 return $this->doWrite( 'incr', $key, $value );
61 }
62
63 public function decr( $key, $value = 1 ) {
64 return $this->doWrite( 'decr', $key, $value );
65 }
66
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 );
71 } else {
72 return true;
73 }
74 }
75
76 public function unlock( $key ) {
77 if ( isset( $this->caches[0] ) ) {
78 return $this->caches[0]->unlock( $key );
79 } else {
80 return true;
81 }
82 }
83
84 protected function doWrite( $method /*, ... */ ) {
85 $ret = true;
86 $args = func_get_args();
87 array_shift( $args );
88
89 foreach ( $this->caches as $cache ) {
90 $ret = $ret && call_user_func_array( array( $cache, $method ), $args );
91 }
92 return $ret;
93 }
94
95 }