Merge "Preserve caller expectations for behaviour of sslVerifyHost"
[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 * @param $casToken[optional] mixed
65 * @return bool|mixed
66 */
67 public function get( $key, &$casToken = null ) {
68 foreach ( $this->caches as $cache ) {
69 $value = $cache->get( $key );
70 if ( $value !== false ) {
71 return $value;
72 }
73 }
74 return false;
75 }
76
77 /**
78 * @param $casToken mixed
79 * @param $key string
80 * @param $value mixed
81 * @param $exptime int
82 * @return bool
83 */
84 public function cas( $casToken, $key, $value, $exptime = 0 ) {
85 throw new MWException( "CAS is not implemented in " . __CLASS__ );
86 }
87
88 /**
89 * @param $key string
90 * @param $value mixed
91 * @param $exptime int
92 * @return bool
93 */
94 public function set( $key, $value, $exptime = 0 ) {
95 return $this->doWrite( 'set', $key, $value, $exptime );
96 }
97
98 /**
99 * @param $key string
100 * @param $time int
101 * @return bool
102 */
103 public function delete( $key, $time = 0 ) {
104 return $this->doWrite( 'delete', $key, $time );
105 }
106
107 /**
108 * @param $key string
109 * @param $value mixed
110 * @param $exptime int
111 * @return bool
112 */
113 public function add( $key, $value, $exptime = 0 ) {
114 return $this->doWrite( 'add', $key, $value, $exptime );
115 }
116
117 /**
118 * @param $key string
119 * @param $value mixed
120 * @param $exptime int
121 * @return bool
122 */
123 public function replace( $key, $value, $exptime = 0 ) {
124 return $this->doWrite( 'replace', $key, $value, $exptime );
125 }
126
127 /**
128 * @param $key string
129 * @param $value int
130 * @return bool|null
131 */
132 public function incr( $key, $value = 1 ) {
133 return $this->doWrite( 'incr', $key, $value );
134 }
135
136 /**
137 * @param $key string
138 * @param $value int
139 * @return bool
140 */
141 public function decr( $key, $value = 1 ) {
142 return $this->doWrite( 'decr', $key, $value );
143 }
144
145 /**
146 * @param $key string
147 * @param $timeout int
148 * @return bool
149 */
150 public function lock( $key, $timeout = 0 ) {
151 // Lock only the first cache, to avoid deadlocks
152 if ( isset( $this->caches[0] ) ) {
153 return $this->caches[0]->lock( $key, $timeout );
154 } else {
155 return true;
156 }
157 }
158
159 /**
160 * @param $key string
161 * @return bool
162 */
163 public function unlock( $key ) {
164 if ( isset( $this->caches[0] ) ) {
165 return $this->caches[0]->unlock( $key );
166 } else {
167 return true;
168 }
169 }
170
171 /**
172 * @param $key string
173 * @param $callback closure Callback method to be executed
174 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
175 * @param $attempts int The amount of times to attempt a merge in case of failure
176 * @return bool success
177 */
178 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
179 return $this->doWrite( 'merge', $key, $callback, $exptime );
180 }
181
182 /**
183 * @param $method string
184 * @return bool
185 */
186 protected function doWrite( $method /*, ... */ ) {
187 $ret = true;
188 $args = func_get_args();
189 array_shift( $args );
190
191 foreach ( $this->caches as $cache ) {
192 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
193 $ret = false;
194 }
195 }
196 return $ret;
197 }
198
199 /**
200 * Delete objects expiring before a certain date.
201 *
202 * Succeed if any of the child caches succeed.
203 * @param $date string
204 * @param $progressCallback bool|callback
205 * @return bool
206 */
207 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
208 $ret = false;
209 foreach ( $this->caches as $cache ) {
210 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
211 $ret = true;
212 }
213 }
214 return $ret;
215 }
216 }