Merge "Fixed "getCachedWork" callback in thumb.php to avoid 404s in the stream method"
[mediawiki.git] / includes / objectcache / MultiWriteBagOStuff.php
blobc656bdd6980dd1f1bb77952865a77a13eb11a89c
1 <?php
2 /**
3 * Wrapper for object caching in different caches.
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.
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.
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
20 * @file
21 * @ingroup Cache
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.
29 * @ingroup Cache
31 class MultiWriteBagOStuff extends BagOStuff {
32 var $caches;
34 /**
35 * Constructor. Parameters are:
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.
41 * @param array $params
42 * @throws MWException
44 public function __construct( $params ) {
45 if ( !isset( $params['caches'] ) ) {
46 throw new MWException( __METHOD__ . ': the caches parameter is required' );
49 $this->caches = array();
50 foreach ( $params['caches'] as $cacheInfo ) {
51 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
55 /**
56 * @param bool $debug
58 public function setDebug( $debug ) {
59 $this->doWrite( 'setDebug', $debug );
62 /**
63 * @param string $key
64 * @param mixed $casToken [optional]
65 * @return bool|mixed
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;
74 return false;
77 /**
78 * @param mixed $casToken
79 * @param string $key
80 * @param mixed $value
81 * @param mixed $exptime
82 * @return bool
84 public function cas( $casToken, $key, $value, $exptime = 0 ) {
85 throw new MWException( "CAS is not implemented in " . __CLASS__ );
88 /**
89 * @param string $key
90 * @param mixed $value
91 * @param int $exptime
92 * @return bool
94 public function set( $key, $value, $exptime = 0 ) {
95 return $this->doWrite( 'set', $key, $value, $exptime );
98 /**
99 * @param string $key
100 * @param int $time
101 * @return bool
103 public function delete( $key, $time = 0 ) {
104 return $this->doWrite( 'delete', $key, $time );
108 * @param string $key
109 * @param mixed $value
110 * @param int $exptime
111 * @return bool
113 public function add( $key, $value, $exptime = 0 ) {
114 return $this->doWrite( 'add', $key, $value, $exptime );
118 * @param string $key
119 * @param mixed $value
120 * @param int $exptime
121 * @return bool
123 public function replace( $key, $value, $exptime = 0 ) {
124 return $this->doWrite( 'replace', $key, $value, $exptime );
128 * @param string $key
129 * @param int $value
130 * @return bool|null
132 public function incr( $key, $value = 1 ) {
133 return $this->doWrite( 'incr', $key, $value );
137 * @param string $key
138 * @param int $value
139 * @return bool
141 public function decr( $key, $value = 1 ) {
142 return $this->doWrite( 'decr', $key, $value );
146 * @param string $key
147 * @param int $timeout
148 * @return bool
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;
160 * @param string $key
161 * @return bool
163 public function unlock( $key ) {
164 if ( isset( $this->caches[0] ) ) {
165 return $this->caches[0]->unlock( $key );
166 } else {
167 return true;
172 * @param string $key
173 * @param Closure $callback Callback method to be executed
174 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
175 * @param int $attempts The amount of times to attempt a merge in case of failure
176 * @return bool Success
178 public function merge( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
179 return $this->doWrite( 'merge', $key, $callback, $exptime );
182 public function getLastError() {
183 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
186 public function clearLastError() {
187 if ( isset( $this->caches[0] ) ) {
188 $this->caches[0]->clearLastError();
193 * @param string $method
194 * @return bool
196 protected function doWrite( $method /*, ... */ ) {
197 $ret = true;
198 $args = func_get_args();
199 array_shift( $args );
201 foreach ( $this->caches as $cache ) {
202 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
203 $ret = false;
206 return $ret;
210 * Delete objects expiring before a certain date.
212 * Succeed if any of the child caches succeed.
213 * @param string $date
214 * @param bool|callable $progressCallback
215 * @return bool
217 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
218 $ret = false;
219 foreach ( $this->caches as $cache ) {
220 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
221 $ret = true;
224 return $ret;