merged master
[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 * @throws MWException
43 */
44 public function __construct( $params ) {
45 if ( !isset( $params['caches'] ) ) {
46 throw new MWException( __METHOD__.': the caches parameter is required' );
47 }
48
49 $this->caches = array();
50 foreach ( $params['caches'] as $cacheInfo ) {
51 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
52 }
53 }
54
55 /**
56 * @param $debug bool
57 */
58 public function setDebug( $debug ) {
59 $this->doWrite( 'setDebug', $debug );
60 }
61
62 /**
63 * @param $key string
64 * @return bool|mixed
65 */
66 public function get( $key ) {
67 foreach ( $this->caches as $cache ) {
68 $value = $cache->get( $key );
69 if ( $value !== false ) {
70 return $value;
71 }
72 }
73 return false;
74 }
75
76 /**
77 * @param $key string
78 * @param $value mixed
79 * @param $exptime int
80 * @return bool
81 */
82 public function set( $key, $value, $exptime = 0 ) {
83 return $this->doWrite( 'set', $key, $value, $exptime );
84 }
85
86 /**
87 * @param $key string
88 * @param $time int
89 * @return bool
90 */
91 public function delete( $key, $time = 0 ) {
92 return $this->doWrite( 'delete', $key, $time );
93 }
94
95 /**
96 * @param $key string
97 * @param $value mixed
98 * @param $exptime int
99 * @return bool
100 */
101 public function add( $key, $value, $exptime = 0 ) {
102 return $this->doWrite( 'add', $key, $value, $exptime );
103 }
104
105 /**
106 * @param $key string
107 * @param $value mixed
108 * @param $exptime int
109 * @return bool
110 */
111 public function replace( $key, $value, $exptime = 0 ) {
112 return $this->doWrite( 'replace', $key, $value, $exptime );
113 }
114
115 /**
116 * @param $key string
117 * @param $value int
118 * @return bool|null
119 */
120 public function incr( $key, $value = 1 ) {
121 return $this->doWrite( 'incr', $key, $value );
122 }
123
124 /**
125 * @param $key string
126 * @param $value int
127 * @return bool
128 */
129 public function decr( $key, $value = 1 ) {
130 return $this->doWrite( 'decr', $key, $value );
131 }
132
133 /**
134 * @param $key string
135 * @param $timeout int
136 * @return bool
137 */
138 public function lock( $key, $timeout = 0 ) {
139 // Lock only the first cache, to avoid deadlocks
140 if ( isset( $this->caches[0] ) ) {
141 return $this->caches[0]->lock( $key, $timeout );
142 } else {
143 return true;
144 }
145 }
146
147 /**
148 * @param $key string
149 * @return bool
150 */
151 public function unlock( $key ) {
152 if ( isset( $this->caches[0] ) ) {
153 return $this->caches[0]->unlock( $key );
154 } else {
155 return true;
156 }
157 }
158
159 /**
160 * @param $method string
161 * @return bool
162 */
163 protected function doWrite( $method /*, ... */ ) {
164 $ret = true;
165 $args = func_get_args();
166 array_shift( $args );
167
168 foreach ( $this->caches as $cache ) {
169 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
170 $ret = false;
171 }
172 }
173 return $ret;
174 }
175
176 /**
177 * Delete objects expiring before a certain date.
178 *
179 * Succeed if any of the child caches succeed.
180 * @param $date string
181 * @param $progressCallback bool|callback
182 * @return bool
183 */
184 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
185 $ret = false;
186 foreach ( $this->caches as $cache ) {
187 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
188 $ret = true;
189 }
190 }
191 return $ret;
192 }
193 }