(bug 19195) Make user IDs more readily available with the API
[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 * @param $params array
19 */
20 public function __construct( $params ) {
21 if ( !isset( $params['caches'] ) ) {
22 throw new MWException( __METHOD__.': the caches parameter is required' );
23 }
24
25 $this->caches = array();
26 foreach ( $params['caches'] as $cacheInfo ) {
27 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
28 }
29 }
30
31 public function setDebug( $debug ) {
32 $this->doWrite( 'setDebug', $debug );
33 }
34
35 public function get( $key ) {
36 foreach ( $this->caches as $cache ) {
37 $value = $cache->get( $key );
38 if ( $value !== false ) {
39 return $value;
40 }
41 }
42 return false;
43 }
44
45 public function set( $key, $value, $exptime = 0 ) {
46 return $this->doWrite( 'set', $key, $value, $exptime );
47 }
48
49 public function delete( $key, $time = 0 ) {
50 return $this->doWrite( 'delete', $key, $time );
51 }
52
53 public function add( $key, $value, $exptime = 0 ) {
54 return $this->doWrite( 'add', $key, $value, $exptime );
55 }
56
57 public function replace( $key, $value, $exptime = 0 ) {
58 return $this->doWrite( 'replace', $key, $value, $exptime );
59 }
60
61 public function incr( $key, $value = 1 ) {
62 return $this->doWrite( 'incr', $key, $value );
63 }
64
65 public function decr( $key, $value = 1 ) {
66 return $this->doWrite( 'decr', $key, $value );
67 }
68
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;
75 }
76 }
77
78 public function unlock( $key ) {
79 if ( isset( $this->caches[0] ) ) {
80 return $this->caches[0]->unlock( $key );
81 } else {
82 return true;
83 }
84 }
85
86 protected function doWrite( $method /*, ... */ ) {
87 $ret = true;
88 $args = func_get_args();
89 array_shift( $args );
90
91 foreach ( $this->caches as $cache ) {
92 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
93 $ret = false;
94 }
95 }
96 return $ret;
97 }
98
99 /**
100 * Delete objects expiring before a certain date.
101 *
102 * Succeed if any of the child caches succeed.
103 * @return bool
104 */
105 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
106 $ret = false;
107 foreach ( $this->caches as $cache ) {
108 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
109 $ret = true;
110 }
111 }
112 return $ret;
113 }
114 }