Merge "Done a bit of deglobalisation."
[lhc/web/wiklou.git] / includes / objectcache / MultiWriteBagOStuff.php
1 <?php
2 /**
3 * Wrapper for object caching in different caches.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * A cache class that replicates all writes to multiple child caches. Reads
26 * are implemented by reading from the caches in the order they are given in
27 * the configuration until a cache gives a positive result.
28 *
29 * @ingroup Cache
30 */
31 class MultiWriteBagOStuff extends BagOStuff {
32 var $caches;
33
34 /**
35 * Constructor. Parameters are:
36 *
37 * - caches: This should have a numbered array of cache parameter
38 * structures, in the style required by $wgObjectCaches. See
39 * the documentation of $wgObjectCaches for more detail.
40 *
41 * @param $params array
42 */
43 public function __construct( $params ) {
44 if ( !isset( $params['caches'] ) ) {
45 throw new MWException( __METHOD__.': the caches parameter is required' );
46 }
47
48 $this->caches = array();
49 foreach ( $params['caches'] as $cacheInfo ) {
50 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
51 }
52 }
53
54 public function setDebug( $debug ) {
55 $this->doWrite( 'setDebug', $debug );
56 }
57
58 public function get( $key ) {
59 foreach ( $this->caches as $cache ) {
60 $value = $cache->get( $key );
61 if ( $value !== false ) {
62 return $value;
63 }
64 }
65 return false;
66 }
67
68 public function set( $key, $value, $exptime = 0 ) {
69 return $this->doWrite( 'set', $key, $value, $exptime );
70 }
71
72 public function delete( $key, $time = 0 ) {
73 return $this->doWrite( 'delete', $key, $time );
74 }
75
76 public function add( $key, $value, $exptime = 0 ) {
77 return $this->doWrite( 'add', $key, $value, $exptime );
78 }
79
80 public function replace( $key, $value, $exptime = 0 ) {
81 return $this->doWrite( 'replace', $key, $value, $exptime );
82 }
83
84 public function incr( $key, $value = 1 ) {
85 return $this->doWrite( 'incr', $key, $value );
86 }
87
88 public function decr( $key, $value = 1 ) {
89 return $this->doWrite( 'decr', $key, $value );
90 }
91
92 public function lock( $key, $timeout = 0 ) {
93 // Lock only the first cache, to avoid deadlocks
94 if ( isset( $this->caches[0] ) ) {
95 return $this->caches[0]->lock( $key, $timeout );
96 } else {
97 return true;
98 }
99 }
100
101 public function unlock( $key ) {
102 if ( isset( $this->caches[0] ) ) {
103 return $this->caches[0]->unlock( $key );
104 } else {
105 return true;
106 }
107 }
108
109 protected function doWrite( $method /*, ... */ ) {
110 $ret = true;
111 $args = func_get_args();
112 array_shift( $args );
113
114 foreach ( $this->caches as $cache ) {
115 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
116 $ret = false;
117 }
118 }
119 return $ret;
120 }
121
122 /**
123 * Delete objects expiring before a certain date.
124 *
125 * Succeed if any of the child caches succeed.
126 * @return bool
127 */
128 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
129 $ret = false;
130 foreach ( $this->caches as $cache ) {
131 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
132 $ret = true;
133 }
134 }
135 return $ret;
136 }
137 }