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 BagOStuff[] */
34 /** @var bool Use async secondary writes */
35 protected $asyncReplication = false;
37 protected $asyncWrites = array();
41 * - caches: This should have a numbered array of cache parameter
42 * structures, in the style required by $wgObjectCaches. See
43 * the documentation of $wgObjectCaches for more detail.
44 * BagOStuff objects can also be used as values.
45 * The first cache is the primary one, being the first to
46 * be read in the fallback chain. Writes happen to all stores
47 * in the order they are defined. However, lock()/unlock() calls
48 * only use the primary store.
49 * - replication: Either 'sync' or 'async'. This controls whether writes to
50 * secondary stores are deferred when possible. Async writes
51 * require the HHVM register_postsend_function() function.
52 * Async writes can increase the chance of some race conditions
53 * or cause keys to expire seconds later than expected. It is
54 * safe to use for modules when cached values: are immutable,
55 * invalidation uses logical TTLs, invalidation uses etag/timestamp
56 * validation against the DB, or merge() is used to handle races.
58 * @param array $params
59 * @throws InvalidArgumentException
61 public function __construct( $params ) {
62 parent
::__construct( $params );
64 if ( !isset( $params['caches'] ) ) {
65 throw new InvalidArgumentException( __METHOD__
. ': "caches" parameter required' );
68 $this->caches
= array();
69 foreach ( $params['caches'] as $cacheInfo ) {
70 $this->caches
[] = ( $cacheInfo instanceof BagOStuff
)
72 : ObjectCache
::newFromParams( $cacheInfo );
75 if ( isset( $params['replication'] ) && $params['replication'] === 'async' ) {
76 $this->asyncReplication
= true;
83 public function setDebug( $debug ) {
84 $this->doWrite( 'setDebug', $debug );
87 public function get( $key, &$casToken = null, $flags = 0 ) {
88 foreach ( $this->caches
as $cache ) {
89 $value = $cache->get( $key, $casToken, $flags );
90 if ( $value !== false ) {
100 * @param int $exptime
103 public function set( $key, $value, $exptime = 0 ) {
104 return $this->doWrite( 'set', $key, $value, $exptime );
111 public function delete( $key ) {
112 return $this->doWrite( 'delete', $key );
117 * @param mixed $value
118 * @param int $exptime
121 public function add( $key, $value, $exptime = 0 ) {
122 return $this->doWrite( 'add', $key, $value, $exptime );
130 public function incr( $key, $value = 1 ) {
131 return $this->doWrite( 'incr', $key, $value );
139 public function decr( $key, $value = 1 ) {
140 return $this->doWrite( 'decr', $key, $value );
145 * @param int $timeout
147 * @param string $rclass
150 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
151 // Lock only the first cache, to avoid deadlocks
152 if ( isset( $this->caches
[0] ) ) {
153 return $this->caches
[0]->lock( $key, $timeout, $expiry, $rclass );
163 public function unlock( $key ) {
164 if ( isset( $this->caches
[0] ) ) {
165 return $this->caches
[0]->unlock( $key );
173 * @param callable $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, $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
196 protected function doWrite( $method /*, ... */ ) {
198 $args = func_get_args();
199 array_shift( $args );
201 foreach ( $this->caches
as $i => $cache ) {
202 if ( $i == 0 ||
!$this->asyncReplication
) {
203 // First store or in sync mode: write now and get result
204 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
208 // Secondary write in async mode: do not block this HTTP request
209 $logger = $this->logger
;
210 DeferredUpdates
::addCallableUpdate(
211 function() use ( $cache, $method, $args, $logger ) {
212 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
213 $logger->warning( "Async $method op failed" );
224 * Delete objects expiring before a certain date.
226 * Succeed if any of the child caches succeed.
227 * @param string $date
228 * @param bool|callable $progressCallback
231 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
233 foreach ( $this->caches
as $cache ) {
234 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {