Rename RESTBase1/Parsoid3 `bodyOnly` parameter to `body_only`
[lhc/web/wiklou.git] / includes / objectcache / MultiWriteBagOStuff.php
1 <?php
2 /**
3 * Wrapper for object caching in different caches.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
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.
28 *
29 * @ingroup Cache
30 */
31 class MultiWriteBagOStuff extends BagOStuff {
32 /** @var BagOStuff[] */
33 protected $caches;
34 /** @var bool Use async secondary writes */
35 protected $asyncWrites = false;
36
37 /**
38 * $params include:
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.
55 *
56 * @param array $params
57 * @throws InvalidArgumentException
58 */
59 public function __construct( $params ) {
60 parent::__construct( $params );
61
62 if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
63 throw new InvalidArgumentException(
64 __METHOD__ . ': "caches" parameter must be an array of caches'
65 );
66 }
67
68 $this->caches = array();
69 foreach ( $params['caches'] as $cacheInfo ) {
70 $this->caches[] = ( $cacheInfo instanceof BagOStuff )
71 ? $cacheInfo
72 : ObjectCache::newFromParams( $cacheInfo );
73 }
74
75 $this->asyncWrites = isset( $params['replication'] ) && $params['replication'] === 'async';
76 }
77
78 /**
79 * @param bool $debug
80 */
81 public function setDebug( $debug ) {
82 $this->doWrite( 'setDebug', $debug );
83 }
84
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 ) {
89 return $value;
90 }
91 }
92 return false;
93 }
94
95 /**
96 * @param string $key
97 * @param mixed $value
98 * @param int $exptime
99 * @return bool
100 */
101 public function set( $key, $value, $exptime = 0 ) {
102 return $this->doWrite( 'set', $key, $value, $exptime );
103 }
104
105 /**
106 * @param string $key
107 * @return bool
108 */
109 public function delete( $key ) {
110 return $this->doWrite( 'delete', $key );
111 }
112
113 /**
114 * @param string $key
115 * @param mixed $value
116 * @param int $exptime
117 * @return bool
118 */
119 public function add( $key, $value, $exptime = 0 ) {
120 return $this->doWrite( 'add', $key, $value, $exptime );
121 }
122
123 /**
124 * @param string $key
125 * @param int $value
126 * @return bool|null
127 */
128 public function incr( $key, $value = 1 ) {
129 return $this->doWrite( 'incr', $key, $value );
130 }
131
132 /**
133 * @param string $key
134 * @param int $value
135 * @return bool
136 */
137 public function decr( $key, $value = 1 ) {
138 return $this->doWrite( 'decr', $key, $value );
139 }
140
141 /**
142 * @param string $key
143 * @param int $timeout
144 * @param int $expiry
145 * @param string $rclass
146 * @return bool
147 */
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 );
151 }
152
153 /**
154 * @param string $key
155 * @return bool
156 */
157 public function unlock( $key ) {
158 return $this->caches[0]->unlock( $key );
159 }
160
161 /**
162 * @param string $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
167 */
168 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
169 return $this->doWrite( 'merge', $key, $callback, $exptime );
170 }
171
172 public function getLastError() {
173 return $this->caches[0]->getLastError();
174 }
175
176 public function clearLastError() {
177 $this->caches[0]->clearLastError();
178 }
179
180 /**
181 * @param string $method
182 * @return bool
183 */
184 protected function doWrite( $method /*, ... */ ) {
185 $ret = true;
186 $args = func_get_args();
187 array_shift( $args );
188
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 ) ) {
193 $ret = false;
194 }
195 } else {
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" );
202 }
203 }
204 );
205 }
206 }
207
208 return $ret;
209 }
210
211 /**
212 * Delete objects expiring before a certain date.
213 *
214 * Succeed if any of the child caches succeed.
215 * @param string $date
216 * @param bool|callable $progressCallback
217 * @return bool
218 */
219 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
220 $ret = false;
221 foreach ( $this->caches as $cache ) {
222 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
223 $ret = true;
224 }
225 }
226
227 return $ret;
228 }
229 }