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
{
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 $params array
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 );
58 public function setDebug( $debug ) {
59 $this->doWrite( 'setDebug', $debug );
66 public function get( $key ) {
67 foreach ( $this->caches
as $cache ) {
68 $value = $cache->get( $key );
69 if ( $value !== false ) {
82 public function set( $key, $value, $exptime = 0 ) {
83 return $this->doWrite( 'set', $key, $value, $exptime );
91 public function delete( $key, $time = 0 ) {
92 return $this->doWrite( 'delete', $key, $time );
101 public function add( $key, $value, $exptime = 0 ) {
102 return $this->doWrite( 'add', $key, $value, $exptime );
107 * @param $value mixed
108 * @param $exptime int
111 public function replace( $key, $value, $exptime = 0 ) {
112 return $this->doWrite( 'replace', $key, $value, $exptime );
120 public function incr( $key, $value = 1 ) {
121 return $this->doWrite( 'incr', $key, $value );
129 public function decr( $key, $value = 1 ) {
130 return $this->doWrite( 'decr', $key, $value );
135 * @param $timeout int
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 );
151 public function unlock( $key ) {
152 if ( isset( $this->caches
[0] ) ) {
153 return $this->caches
[0]->unlock( $key );
160 * @param $method string
163 protected function doWrite( $method /*, ... */ ) {
165 $args = func_get_args();
166 array_shift( $args );
168 foreach ( $this->caches
as $cache ) {
169 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
177 * Delete objects expiring before a certain date.
179 * Succeed if any of the child caches succeed.
180 * @param $date string
181 * @param $progressCallback bool|callback
184 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
186 foreach ( $this->caches
as $cache ) {
187 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {