Move up devunt's name to Developers
[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 array BagOStuff[] */
33 protected $caches;
34
35 /**
36 * Constructor. Parameters are:
37 *
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.
41 *
42 * @param array $params
43 * @throws InvalidArgumentException
44 */
45 public function __construct( $params ) {
46 parent::__construct( $params );
47 if ( !isset( $params['caches'] ) ) {
48 throw new InvalidArgumentException( __METHOD__ . ': the caches parameter is required' );
49 }
50
51 $this->caches = array();
52 foreach ( $params['caches'] as $cacheInfo ) {
53 $this->caches[] = ObjectCache::newFromParams( $cacheInfo );
54 }
55 }
56
57 /**
58 * @param bool $debug
59 */
60 public function setDebug( $debug ) {
61 $this->doWrite( 'setDebug', $debug );
62 }
63
64 /**
65 * @param string $key
66 * @param mixed $casToken [optional]
67 * @return bool|mixed
68 */
69 public function get( $key, &$casToken = null ) {
70 foreach ( $this->caches as $cache ) {
71 $value = $cache->get( $key );
72 if ( $value !== false ) {
73 return $value;
74 }
75 }
76 return false;
77 }
78
79 /**
80 * @param string $key
81 * @param mixed $value
82 * @param int $exptime
83 * @return bool
84 */
85 public function set( $key, $value, $exptime = 0 ) {
86 return $this->doWrite( 'set', $key, $value, $exptime );
87 }
88
89 /**
90 * @param string $key
91 * @return bool
92 */
93 public function delete( $key ) {
94 return $this->doWrite( 'delete', $key );
95 }
96
97 /**
98 * @param string $key
99 * @param mixed $value
100 * @param int $exptime
101 * @return bool
102 */
103 public function add( $key, $value, $exptime = 0 ) {
104 return $this->doWrite( 'add', $key, $value, $exptime );
105 }
106
107 /**
108 * @param string $key
109 * @param int $value
110 * @return bool|null
111 */
112 public function incr( $key, $value = 1 ) {
113 return $this->doWrite( 'incr', $key, $value );
114 }
115
116 /**
117 * @param string $key
118 * @param int $value
119 * @return bool
120 */
121 public function decr( $key, $value = 1 ) {
122 return $this->doWrite( 'decr', $key, $value );
123 }
124
125 /**
126 * @param string $key
127 * @param int $timeout
128 * @param int $expiry
129 * @return bool
130 */
131 public function lock( $key, $timeout = 6, $expiry = 6 ) {
132 // Lock only the first cache, to avoid deadlocks
133 if ( isset( $this->caches[0] ) ) {
134 return $this->caches[0]->lock( $key, $timeout, $expiry );
135 } else {
136 return true;
137 }
138 }
139
140 /**
141 * @param string $key
142 * @return bool
143 */
144 public function unlock( $key ) {
145 if ( isset( $this->caches[0] ) ) {
146 return $this->caches[0]->unlock( $key );
147 } else {
148 return true;
149 }
150 }
151
152 /**
153 * @param string $key
154 * @param callable $callback Callback method to be executed
155 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
156 * @param int $attempts The amount of times to attempt a merge in case of failure
157 * @return bool Success
158 */
159 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
160 return $this->doWrite( 'merge', $key, $callback, $exptime );
161 }
162
163 public function getLastError() {
164 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
165 }
166
167 public function clearLastError() {
168 if ( isset( $this->caches[0] ) ) {
169 $this->caches[0]->clearLastError();
170 }
171 }
172
173 /**
174 * @param string $method
175 * @return bool
176 */
177 protected function doWrite( $method /*, ... */ ) {
178 $ret = true;
179 $args = func_get_args();
180 array_shift( $args );
181
182 foreach ( $this->caches as $cache ) {
183 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
184 $ret = false;
185 }
186 }
187 return $ret;
188 }
189
190 /**
191 * Delete objects expiring before a certain date.
192 *
193 * Succeed if any of the child caches succeed.
194 * @param string $date
195 * @param bool|callable $progressCallback
196 * @return bool
197 */
198 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
199 $ret = false;
200 foreach ( $this->caches as $cache ) {
201 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
202 $ret = true;
203 }
204 }
205 return $ret;
206 }
207 }