Merge "Revert "CategoryView modified to use css columns""
[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 MWException
44 */
45 public function __construct( $params ) {
46 parent::__construct( $params );
47 if ( !isset( $params['caches'] ) ) {
48 throw new MWException( __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 mixed $casToken
81 * @param string $key
82 * @param mixed $value
83 * @param mixed $exptime
84 * @return bool
85 * @throws MWException
86 */
87 public function cas( $casToken, $key, $value, $exptime = 0 ) {
88 throw new MWException( "CAS is not implemented in " . __CLASS__ );
89 }
90
91 /**
92 * @param string $key
93 * @param mixed $value
94 * @param int $exptime
95 * @return bool
96 */
97 public function set( $key, $value, $exptime = 0 ) {
98 return $this->doWrite( 'set', $key, $value, $exptime );
99 }
100
101 /**
102 * @param string $key
103 * @param int $time
104 * @return bool
105 */
106 public function delete( $key, $time = 0 ) {
107 return $this->doWrite( 'delete', $key, $time );
108 }
109
110 /**
111 * @param string $key
112 * @param mixed $value
113 * @param int $exptime
114 * @return bool
115 */
116 public function add( $key, $value, $exptime = 0 ) {
117 return $this->doWrite( 'add', $key, $value, $exptime );
118 }
119
120 /**
121 * @param string $key
122 * @param int $value
123 * @return bool|null
124 */
125 public function incr( $key, $value = 1 ) {
126 return $this->doWrite( 'incr', $key, $value );
127 }
128
129 /**
130 * @param string $key
131 * @param int $value
132 * @return bool
133 */
134 public function decr( $key, $value = 1 ) {
135 return $this->doWrite( 'decr', $key, $value );
136 }
137
138 /**
139 * @param string $key
140 * @param int $timeout
141 * @param int $expiry
142 * @return bool
143 */
144 public function lock( $key, $timeout = 6, $expiry = 6 ) {
145 // Lock only the first cache, to avoid deadlocks
146 if ( isset( $this->caches[0] ) ) {
147 return $this->caches[0]->lock( $key, $timeout, $expiry );
148 } else {
149 return true;
150 }
151 }
152
153 /**
154 * @param string $key
155 * @return bool
156 */
157 public function unlock( $key ) {
158 if ( isset( $this->caches[0] ) ) {
159 return $this->caches[0]->unlock( $key );
160 } else {
161 return true;
162 }
163 }
164
165 /**
166 * @param string $key
167 * @param Closure $callback Callback method to be executed
168 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
169 * @param int $attempts The amount of times to attempt a merge in case of failure
170 * @return bool Success
171 */
172 public function merge( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
173 return $this->doWrite( 'merge', $key, $callback, $exptime );
174 }
175
176 public function getLastError() {
177 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
178 }
179
180 public function clearLastError() {
181 if ( isset( $this->caches[0] ) ) {
182 $this->caches[0]->clearLastError();
183 }
184 }
185
186 /**
187 * @param string $method
188 * @return bool
189 */
190 protected function doWrite( $method /*, ... */ ) {
191 $ret = true;
192 $args = func_get_args();
193 array_shift( $args );
194
195 foreach ( $this->caches as $cache ) {
196 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
197 $ret = false;
198 }
199 }
200 return $ret;
201 }
202
203 /**
204 * Delete objects expiring before a certain date.
205 *
206 * Succeed if any of the child caches succeed.
207 * @param string $date
208 * @param bool|callable $progressCallback
209 * @return bool
210 */
211 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
212 $ret = false;
213 foreach ( $this->caches as $cache ) {
214 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
215 $ret = true;
216 }
217 }
218 return $ret;
219 }
220 }