Remove "related" searches
[mediawiki.git] / includes / objectcache / MultiWriteBagOStuff.php
blob497f75d2d6cfb5e5d8758504c2fee302a61d12f6
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 array BagOStuff[] */
33 protected $caches;
35 /**
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
43 * @throws MWException
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 );
56 /**
57 * @param bool $debug
59 public function setDebug( $debug ) {
60 $this->doWrite( 'setDebug', $debug );
63 /**
64 * @param string $key
65 * @param mixed $casToken [optional]
66 * @return bool|mixed
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;
75 return false;
78 /**
79 * @param mixed $casToken
80 * @param string $key
81 * @param mixed $value
82 * @param mixed $exptime
83 * @return bool
85 public function cas( $casToken, $key, $value, $exptime = 0 ) {
86 throw new MWException( "CAS is not implemented in " . __CLASS__ );
89 /**
90 * @param string $key
91 * @param mixed $value
92 * @param int $exptime
93 * @return bool
95 public function set( $key, $value, $exptime = 0 ) {
96 return $this->doWrite( 'set', $key, $value, $exptime );
99 /**
100 * @param string $key
101 * @param int $time
102 * @return bool
104 public function delete( $key, $time = 0 ) {
105 return $this->doWrite( 'delete', $key, $time );
109 * @param string $key
110 * @param mixed $value
111 * @param int $exptime
112 * @return bool
114 public function add( $key, $value, $exptime = 0 ) {
115 return $this->doWrite( 'add', $key, $value, $exptime );
119 * @param string $key
120 * @param mixed $value
121 * @param int $exptime
122 * @return bool
124 public function replace( $key, $value, $exptime = 0 ) {
125 return $this->doWrite( 'replace', $key, $value, $exptime );
129 * @param string $key
130 * @param int $value
131 * @return bool|null
133 public function incr( $key, $value = 1 ) {
134 return $this->doWrite( 'incr', $key, $value );
138 * @param string $key
139 * @param int $value
140 * @return bool
142 public function decr( $key, $value = 1 ) {
143 return $this->doWrite( 'decr', $key, $value );
147 * @param string $key
148 * @param int $timeout
149 * @return bool
151 public function lock( $key, $timeout = 0 ) {
152 // Lock only the first cache, to avoid deadlocks
153 if ( isset( $this->caches[0] ) ) {
154 return $this->caches[0]->lock( $key, $timeout );
155 } else {
156 return true;
161 * @param string $key
162 * @return bool
164 public function unlock( $key ) {
165 if ( isset( $this->caches[0] ) ) {
166 return $this->caches[0]->unlock( $key );
167 } else {
168 return true;
173 * @param string $key
174 * @param Closure $callback Callback method to be executed
175 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
176 * @param int $attempts The amount of times to attempt a merge in case of failure
177 * @return bool Success
179 public function merge( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
180 return $this->doWrite( 'merge', $key, $callback, $exptime );
183 public function getLastError() {
184 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
187 public function clearLastError() {
188 if ( isset( $this->caches[0] ) ) {
189 $this->caches[0]->clearLastError();
194 * @param string $method
195 * @return bool
197 protected function doWrite( $method /*, ... */ ) {
198 $ret = true;
199 $args = func_get_args();
200 array_shift( $args );
202 foreach ( $this->caches as $cache ) {
203 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
204 $ret = false;
207 return $ret;
211 * Delete objects expiring before a certain date.
213 * Succeed if any of the child caches succeed.
214 * @param string $date
215 * @param bool|callable $progressCallback
216 * @return bool
218 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
219 $ret = false;
220 foreach ( $this->caches as $cache ) {
221 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
222 $ret = true;
225 return $ret;