Merge "Cleaned up and split up Swift header parsing methods a bit"
[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 public function get( $key, &$casToken = null, $flags = 0 ) {
65 foreach ( $this->caches as $cache ) {
66 $value = $cache->get( $key, null, $flags = 0 );
67 if ( $value !== false ) {
68 return $value;
69 }
70 }
71 return false;
72 }
73
74 /**
75 * @param string $key
76 * @param mixed $value
77 * @param int $exptime
78 * @return bool
79 */
80 public function set( $key, $value, $exptime = 0 ) {
81 return $this->doWrite( 'set', $key, $value, $exptime );
82 }
83
84 /**
85 * @param string $key
86 * @return bool
87 */
88 public function delete( $key ) {
89 return $this->doWrite( 'delete', $key );
90 }
91
92 /**
93 * @param string $key
94 * @param mixed $value
95 * @param int $exptime
96 * @return bool
97 */
98 public function add( $key, $value, $exptime = 0 ) {
99 return $this->doWrite( 'add', $key, $value, $exptime );
100 }
101
102 /**
103 * @param string $key
104 * @param int $value
105 * @return bool|null
106 */
107 public function incr( $key, $value = 1 ) {
108 return $this->doWrite( 'incr', $key, $value );
109 }
110
111 /**
112 * @param string $key
113 * @param int $value
114 * @return bool
115 */
116 public function decr( $key, $value = 1 ) {
117 return $this->doWrite( 'decr', $key, $value );
118 }
119
120 /**
121 * @param string $key
122 * @param int $timeout
123 * @param int $expiry
124 * @return bool
125 */
126 public function lock( $key, $timeout = 6, $expiry = 6 ) {
127 // Lock only the first cache, to avoid deadlocks
128 if ( isset( $this->caches[0] ) ) {
129 return $this->caches[0]->lock( $key, $timeout, $expiry );
130 } else {
131 return true;
132 }
133 }
134
135 /**
136 * @param string $key
137 * @return bool
138 */
139 public function unlock( $key ) {
140 if ( isset( $this->caches[0] ) ) {
141 return $this->caches[0]->unlock( $key );
142 } else {
143 return true;
144 }
145 }
146
147 /**
148 * @param string $key
149 * @param callable $callback Callback method to be executed
150 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
151 * @param int $attempts The amount of times to attempt a merge in case of failure
152 * @return bool Success
153 */
154 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
155 return $this->doWrite( 'merge', $key, $callback, $exptime );
156 }
157
158 public function getLastError() {
159 return isset( $this->caches[0] ) ? $this->caches[0]->getLastError() : self::ERR_NONE;
160 }
161
162 public function clearLastError() {
163 if ( isset( $this->caches[0] ) ) {
164 $this->caches[0]->clearLastError();
165 }
166 }
167
168 /**
169 * @param string $method
170 * @return bool
171 */
172 protected function doWrite( $method /*, ... */ ) {
173 $ret = true;
174 $args = func_get_args();
175 array_shift( $args );
176
177 foreach ( $this->caches as $cache ) {
178 if ( !call_user_func_array( array( $cache, $method ), $args ) ) {
179 $ret = false;
180 }
181 }
182 return $ret;
183 }
184
185 /**
186 * Delete objects expiring before a certain date.
187 *
188 * Succeed if any of the child caches succeed.
189 * @param string $date
190 * @param bool|callable $progressCallback
191 * @return bool
192 */
193 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
194 $ret = false;
195 foreach ( $this->caches as $cache ) {
196 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
197 $ret = true;
198 }
199 }
200 return $ret;
201 }
202 }