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
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.
31 class MultiWriteBagOStuff
extends BagOStuff
{
32 /** @var array BagOStuff[] */
36 * Constructor. Parameters are:
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.
42 * @param array $params
45 public function __construct( $params ) {
46 if ( !isset( $params['caches'] ) ) {
47 throw new MWException( __METHOD__
. ': the caches parameter is required' );
50 $this->caches
= array();
51 foreach ( $params['caches'] as $cacheInfo ) {
52 $this->caches
[] = ObjectCache
::newFromParams( $cacheInfo );
59 public function setDebug( $debug ) {
60 $this->doWrite( 'setDebug', $debug );
65 * @param mixed $casToken [optional]
68 public function get( $key, &$casToken = null ) {
69 foreach ( $this->caches
as $cache ) {
70 $value = $cache->get( $key );
71 if ( $value !== false ) {
79 * @param mixed $casToken
82 * @param mixed $exptime
85 public function cas( $casToken, $key, $value, $exptime = 0 ) {
86 throw new MWException( "CAS is not implemented in " . __CLASS__
);
95 public function set( $key, $value, $exptime = 0 ) {
96 return $this->doWrite( 'set', $key, $value, $exptime );
104 public function delete( $key, $time = 0 ) {
105 return $this->doWrite( 'delete', $key, $time );
110 * @param mixed $value
111 * @param int $exptime
114 public function add( $key, $value, $exptime = 0 ) {
115 return $this->doWrite( 'add', $key, $value, $exptime );
123 public function incr( $key, $value = 1 ) {
124 return $this->doWrite( 'incr', $key, $value );
132 public function decr( $key, $value = 1 ) {
133 return $this->doWrite( 'decr', $key, $value );
138 * @param int $timeout
142 public function lock( $key, $timeout = 6, $expiry = 6 ) {
143 // Lock only the first cache, to avoid deadlocks
144 if ( isset( $this->caches
[0] ) ) {
145 return $this->caches
[0]->lock( $key, $timeout, $expiry );
155 public function unlock( $key ) {
156 if ( isset( $this->caches
[0] ) ) {
157 return $this->caches
[0]->unlock( $key );
165 * @param Closure $callback Callback method to be executed
166 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
167 * @param int $attempts The amount of times to attempt a merge in case of failure
168 * @return bool Success
170 public function merge( $key, Closure
$callback, $exptime = 0, $attempts = 10 ) {
171 return $this->doWrite( 'merge', $key, $callback, $exptime );
174 public function getLastError() {
175 return isset( $this->caches
[0] ) ?
$this->caches
[0]->getLastError() : self
::ERR_NONE
;
178 public function clearLastError() {
179 if ( isset( $this->caches
[0] ) ) {
180 $this->caches
[0]->clearLastError();
185 * @param string $method
188 protected function doWrite( $method /*, ... */ ) {
190 $args = func_get_args();
191 array_shift( $args );
193 foreach ( $this->caches
as $cache ) {
194 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
202 * Delete objects expiring before a certain date.
204 * Succeed if any of the child caches succeed.
205 * @param string $date
206 * @param bool|callable $progressCallback
209 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
211 foreach ( $this->caches
as $cache ) {
212 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {