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 $asyncWrites = false;
39 * - caches: This should have a numbered array of cache parameter
40 * structures, in the style required by $wgObjectCaches. See
41 * the documentation of $wgObjectCaches for more detail.
42 * BagOStuff objects can also be used as values.
43 * The first cache is the primary one, being the first to
44 * be read in the fallback chain. Writes happen to all stores
45 * in the order they are defined. However, lock()/unlock() calls
46 * only use the primary store.
47 * - replication: Either 'sync' or 'async'. This controls whether writes to
48 * secondary stores are deferred when possible. Async writes
49 * require the HHVM register_postsend_function() function.
50 * Async writes can increase the chance of some race conditions
51 * or cause keys to expire seconds later than expected. It is
52 * safe to use for modules when cached values: are immutable,
53 * invalidation uses logical TTLs, invalidation uses etag/timestamp
54 * validation against the DB, or merge() is used to handle races.
56 * @param array $params
57 * @throws InvalidArgumentException
59 public function __construct( $params ) {
60 parent
::__construct( $params );
62 if ( empty( $params['caches'] ) ||
!is_array( $params['caches'] ) ) {
63 throw new InvalidArgumentException(
64 __METHOD__
. ': "caches" parameter must be an array of caches'
68 $this->caches
= array();
69 foreach ( $params['caches'] as $cacheInfo ) {
70 $this->caches
[] = ( $cacheInfo instanceof BagOStuff
)
72 : ObjectCache
::newFromParams( $cacheInfo );
75 $this->asyncWrites
= isset( $params['replication'] ) && $params['replication'] === 'async';
81 public function setDebug( $debug ) {
82 $this->doWrite( 'setDebug', $debug );
85 public function get( $key, &$casToken = null, $flags = 0 ) {
86 foreach ( $this->caches
as $cache ) {
87 $value = $cache->get( $key, $casToken, $flags );
88 if ( $value !== false ) {
101 public function set( $key, $value, $exptime = 0 ) {
102 return $this->doWrite( 'set', $key, $value, $exptime );
109 public function delete( $key ) {
110 return $this->doWrite( 'delete', $key );
115 * @param mixed $value
116 * @param int $exptime
119 public function add( $key, $value, $exptime = 0 ) {
120 return $this->doWrite( 'add', $key, $value, $exptime );
128 public function incr( $key, $value = 1 ) {
129 return $this->doWrite( 'incr', $key, $value );
137 public function decr( $key, $value = 1 ) {
138 return $this->doWrite( 'decr', $key, $value );
143 * @param int $timeout
145 * @param string $rclass
148 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
149 // Lock only the first cache, to avoid deadlocks
150 return $this->caches
[0]->lock( $key, $timeout, $expiry, $rclass );
157 public function unlock( $key ) {
158 return $this->caches
[0]->unlock( $key );
163 * @param callable $callback Callback method to be executed
164 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
165 * @param int $attempts The amount of times to attempt a merge in case of failure
166 * @return bool Success
168 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
169 return $this->doWrite( 'merge', $key, $callback, $exptime );
172 public function getLastError() {
173 return $this->caches
[0]->getLastError();
176 public function clearLastError() {
177 $this->caches
[0]->clearLastError();
181 * @param string $method
184 protected function doWrite( $method /*, ... */ ) {
186 $args = func_get_args();
187 array_shift( $args );
189 foreach ( $this->caches
as $i => $cache ) {
190 if ( $i == 0 ||
!$this->asyncWrites
) {
191 // First store or in sync mode: write now and get result
192 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
196 // Secondary write in async mode: do not block this HTTP request
197 $logger = $this->logger
;
198 DeferredUpdates
::addCallableUpdate(
199 function () use ( $cache, $method, $args, $logger ) {
200 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
201 $logger->warning( "Async $method op failed" );
212 * Delete objects expiring before a certain date.
214 * Succeed if any of the child caches succeed.
215 * @param string $date
216 * @param bool|callable $progressCallback
219 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
221 foreach ( $this->caches
as $cache ) {
222 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {