f6fe243cd1b422c17b09db034374f5c98a76e41f
[lhc/web/wiklou.git] / includes / objectcache / BagOStuff.php
1 <?php
2 /**
3 * Classes to cache objects in PHP accelerators, SQL database or DBA files
4 *
5 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Cache
25 */
26
27 /**
28 * @defgroup Cache Cache
29 */
30
31 /**
32 * interface is intended to be more or less compatible with
33 * the PHP memcached client.
34 *
35 * backends for local hash array and SQL table included:
36 * <code>
37 * $bag = new HashBagOStuff();
38 * $bag = new SqlBagOStuff(); # connect to db first
39 * </code>
40 *
41 * @ingroup Cache
42 */
43 abstract class BagOStuff {
44 private $debugMode = false;
45
46 /**
47 * @param $bool bool
48 */
49 public function setDebug( $bool ) {
50 $this->debugMode = $bool;
51 }
52
53 /* *** THE GUTS OF THE OPERATION *** */
54 /* Override these with functional things in subclasses */
55
56 /**
57 * Get an item with the given key. Returns false if it does not exist.
58 * @param $key string
59 * @param $casToken[optional] mixed
60 * @return mixed Returns false on failure
61 */
62 abstract public function get( $key, &$casToken = null );
63
64 /**
65 * Set an item.
66 * @param $key string
67 * @param $value mixed
68 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
69 * @return bool success
70 */
71 abstract public function set( $key, $value, $exptime = 0 );
72
73 /**
74 * Check and set an item.
75 * @param $casToken mixed
76 * @param $key string
77 * @param $value mixed
78 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
79 * @return bool success
80 */
81 abstract public function cas( $casToken, $key, $value, $exptime = 0 );
82
83 /**
84 * Delete an item.
85 * @param $key string
86 * @param int $time Amount of time to delay the operation (mostly memcached-specific)
87 * @return bool True if the item was deleted or not found, false on failure
88 */
89 abstract public function delete( $key, $time = 0 );
90
91 /**
92 * Merge changes into the existing cache value (possibly creating a new one).
93 * The callback function returns the new value given the current value (possibly false),
94 * and takes the arguments: (this BagOStuff object, cache key, current value).
95 *
96 * @param $key string
97 * @param $callback closure Callback method to be executed
98 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
99 * @param int $attempts The amount of times to attempt a merge in case of failure
100 * @return bool success
101 */
102 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
103 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
104 }
105
106 /**
107 * @see BagOStuff::merge()
108 *
109 * @param $key string
110 * @param $callback closure Callback method to be executed
111 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
112 * @param int $attempts The amount of times to attempt a merge in case of failure
113 * @return bool success
114 */
115 protected function mergeViaCas( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
116 do {
117 $casToken = null; // passed by reference
118 $currentValue = $this->get( $key, $casToken ); // get the old value
119 $value = $callback( $this, $key, $currentValue ); // derive the new value
120
121 if ( $value === false ) {
122 $success = true; // do nothing
123 } elseif ( $currentValue === false ) {
124 // Try to create the key, failing if it gets created in the meantime
125 $success = $this->add( $key, $value, $exptime );
126 } else {
127 // Try to update the key, failing if it gets changed in the meantime
128 $success = $this->cas( $casToken, $key, $value, $exptime );
129 }
130 } while ( !$success && --$attempts );
131
132 return $success;
133 }
134
135 /**
136 * @see BagOStuff::merge()
137 *
138 * @param $key string
139 * @param $callback closure Callback method to be executed
140 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
141 * @param int $attempts The amount of times to attempt a merge in case of failure
142 * @return bool success
143 */
144 protected function mergeViaLock( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
145 if ( !$this->lock( $key, 60 ) ) {
146 return false;
147 }
148
149 $currentValue = $this->get( $key ); // get the old value
150 $value = $callback( $this, $key, $currentValue ); // derive the new value
151
152 if ( $value === false ) {
153 $success = true; // do nothing
154 } else {
155 $success = $this->set( $key, $value, $exptime ); // set the new value
156 }
157
158 if ( !$this->unlock( $key ) ) {
159 // this should never happen
160 trigger_error( "Could not release lock for key '$key'." );
161 }
162
163 return $success;
164 }
165
166 /**
167 * @param $key string
168 * @param $timeout integer [optional]
169 * @return bool success
170 */
171 public function lock( $key, $timeout = 60 ) {
172 $timestamp = microtime( true ); // starting UNIX timestamp
173 if ( $this->add( "{$key}:lock", 1, $timeout ) ) {
174 return true;
175 }
176
177 $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
178 $sleep = 2 * $uRTT; // rough time to do get()+set()
179
180 $locked = false; // lock acquired
181 $attempts = 0; // failed attempts
182 do {
183 if ( ++$attempts >= 3 && $sleep <= 1e6 ) {
184 // Exponentially back off after failed attempts to avoid network spam.
185 // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
186 $sleep *= 2;
187 }
188 usleep( $sleep ); // back off
189 $locked = $this->add( "{$key}:lock", 1, $timeout );
190 } while ( !$locked );
191
192 return $locked;
193 }
194
195 /**
196 * @param $key string
197 * @return bool success
198 */
199 public function unlock( $key ) {
200 return $this->delete( "{$key}:lock" );
201 }
202
203 /**
204 * Delete all objects expiring before a certain date.
205 * @param string $date The reference date in MW format
206 * @param $progressCallback callback|bool Optional, a function which will be called
207 * regularly during long-running operations with the percentage progress
208 * as the first parameter.
209 *
210 * @return bool on success, false if unimplemented
211 */
212 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
213 // stub
214 return false;
215 }
216
217 /* *** Emulated functions *** */
218
219 /**
220 * Get an associative array containing the item for each of the keys that have items.
221 * @param array $keys List of strings
222 * @return Array
223 */
224 public function getMulti( array $keys ) {
225 $res = array();
226 foreach ( $keys as $key ) {
227 $val = $this->get( $key );
228 if ( $val !== false ) {
229 $res[$key] = $val;
230 }
231 }
232 return $res;
233 }
234
235 /**
236 * @param $key string
237 * @param $value mixed
238 * @param $exptime integer
239 * @return bool success
240 */
241 public function add( $key, $value, $exptime = 0 ) {
242 if ( $this->get( $key ) === false ) {
243 return $this->set( $key, $value, $exptime );
244 }
245 return false; // key already set
246 }
247
248 /**
249 * @param $key string
250 * @param $value mixed
251 * @param $exptime int
252 * @return bool success
253 * @deprecated 1.23
254 */
255 public function replace( $key, $value, $exptime = 0 ) {
256 wfDeprecated( __METHOD__, '1.23' );
257 if ( $this->get( $key ) !== false ) {
258 return $this->set( $key, $value, $exptime );
259 }
260 return false; // key not already set
261 }
262
263 /**
264 * Increase stored value of $key by $value while preserving its TTL
265 * @param string $key Key to increase
266 * @param $value Integer: Value to add to $key (Default 1)
267 * @return integer|bool New value or false on failure
268 */
269 public function incr( $key, $value = 1 ) {
270 if ( !$this->lock( $key ) ) {
271 return false;
272 }
273 $n = $this->get( $key );
274 if ( $this->isInteger( $n ) ) { // key exists?
275 $n += intval( $value );
276 $this->set( $key, max( 0, $n ) ); // exptime?
277 } else {
278 $n = false;
279 }
280 $this->unlock( $key );
281
282 return $n;
283 }
284
285 /**
286 * Decrease stored value of $key by $value while preserving its TTL
287 * @param $key String
288 * @param $value Integer
289 * @return integer
290 */
291 public function decr( $key, $value = 1 ) {
292 return $this->incr( $key, - $value );
293 }
294
295 /**
296 * @param $text string
297 */
298 public function debug( $text ) {
299 if ( $this->debugMode ) {
300 $class = get_class( $this );
301 wfDebug( "$class debug: $text\n" );
302 }
303 }
304
305 /**
306 * Convert an optionally relative time to an absolute time
307 * @param $exptime integer
308 * @return int
309 */
310 protected function convertExpiry( $exptime ) {
311 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
312 return time() + $exptime;
313 } else {
314 return $exptime;
315 }
316 }
317
318 /**
319 * Convert an optionally absolute expiry time to a relative time. If an
320 * absolute time is specified which is in the past, use a short expiry time.
321 *
322 * @param $exptime integer
323 * @return integer
324 */
325 protected function convertToRelative( $exptime ) {
326 if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
327 $exptime -= time();
328 if ( $exptime <= 0 ) {
329 $exptime = 1;
330 }
331 return $exptime;
332 } else {
333 return $exptime;
334 }
335 }
336
337 /**
338 * Check if a value is an integer
339 *
340 * @param $value mixed
341 * @return bool
342 */
343 protected function isInteger( $value ) {
344 return ( is_int( $value ) || ctype_digit( $value ) );
345 }
346 }