Merge "mediawiki.action.edit.stash: set timeout to 4 seconds"
[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 array BagOStuff[] */
33 protected $caches;
34
35 /**
36 * Constructor. Parameters are:
37 *
38 * - caches: This should have a numbered array of cache parameter
39 * structures, in the style required by $wgObjectCaches. See
40 * the documentation of $wgObjectCaches for more detail.
41 *
42 * @param array $params
43 * @throws MWException
44 */
45 public function __construct( $params ) {
46 if ( !isset( $params['caches'] ) ) {
47 throw new MWException( __METHOD__ . ': the caches parameter is required' );
48 }
49
50 $this->caches = array();
51 foreach ( $params['caches'] as $cacheInfo ) {
52 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
53 }
54 }
55
56 /**
57 * @param bool $debug
58 */
59 public function setDebug( $debug ) {
60 $this->doWrite( 'setDebug', $debug );
61 }
62
63 /**
64 * @param string $key
65 * @param mixed $casToken [optional]
66 * @return bool|mixed
67 */
68 public function get( $key, &$casToken = null ) {
69 foreach ( $this->caches as $cache ) {
70 $value = $cache->get( $key );
71 if ( $value !== false ) {
72 return $value;
73 }
74 }
75 return false;
76 }
77
78 /**
79 * @param mixed $casToken
80 * @param string $key
81 * @param mixed $value
82 * @param mixed $exptime
83 * @return bool
84 * @throws MWException
85 */
86 public function cas( $casToken, $key, $value, $exptime = 0 ) {
87 throw new MWException( "CAS is not implemented in " . __CLASS__ );
88 }
89
90 /**
91 * @param string $key
92 * @param mixed $value
93 * @param int $exptime
94 * @return bool
95 */
96 public function set( $key, $value, $exptime = 0 ) {
97 return $this->doWrite( 'set', $key, $value, $exptime );
98 }
99
100 /**
101 * @param string $key
102 * @param int $time
103 * @return bool
104 */
105 public function delete( $key, $time = 0 ) {
106 return $this->doWrite( 'delete', $key, $time );
107 }
108
109 /**
110 * @param string $key
111 * @param mixed $value
112 * @param int $exptime
113 * @return bool
114 */
115 public function add( $key, $value, $exptime = 0 ) {
116 return $this->doWrite( 'add', $key, $value, $exptime );
117 }
118
119 /**
120 * @param string $key
121 * @param int $value
122 * @return bool|null
123 */
124 public function incr( $key, $value = 1 ) {
125 return $this->doWrite( 'incr', $key, $value );
126 }
127
128 /**
129 * @param string $key
130 * @param int $value
131 * @return bool
132 */
133 public function decr( $key, $value = 1 ) {
134 return $this->doWrite( 'decr', $key, $value );
135 }
136
137 /**
138 * @param string $key
139 * @param int $timeout
140 * @param int $expiry
141 * @return bool
142 */
143 public function lock( $key, $timeout = 6, $expiry = 6 ) {
144 // Lock only the first cache, to avoid deadlocks
145 if ( isset( $this->caches[0] ) ) {
146 return $this->caches[0]->lock( $key, $timeout, $expiry );
147 } else {
148 return true;
149 }
150 }
151
152 /**
153 * @param string $key
154 * @return bool
155 */
156 public function unlock( $key ) {
157 if ( isset( $this->caches[0] ) ) {
158 return $this->caches[0]->unlock( $key );
159 } else {
160 return true;
161 }
162 }
163
164 /**
165 * @param string $key
166 * @param Closure $callback Callback method to be executed
167 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
168 * @param int $attempts The amount of times to attempt a merge in case of failure
169 * @return bool Success
170 */
171 public function merge( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
172 return $this->doWrite( 'merge', $key, $callback, $exptime );
173 }
174
175 public function getLastError() {
176 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
177 }
178
179 public function clearLastError() {
180 if ( isset( $this->caches[0] ) ) {
181 $this->caches[0]->clearLastError();
182 }
183 }
184
185 /**
186 * @param string $method
187 * @return bool
188 */
189 protected function doWrite( $method /*, ... */ ) {
190 $ret = true;
191 $args = func_get_args();
192 array_shift( $args );
193
194 foreach ( $this->caches as $cache ) {
195 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
196 $ret = false;
197 }
198 }
199 return $ret;
200 }
201
202 /**
203 * Delete objects expiring before a certain date.
204 *
205 * Succeed if any of the child caches succeed.
206 * @param string $date
207 * @param bool|callable $progressCallback
208 * @return bool
209 */
210 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
211 $ret = false;
212 foreach ( $this->caches as $cache ) {
213 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
214 $ret = true;
215 }
216 }
217 return $ret;
218 }
219 }