Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / objectcache / BagOStuff.php
blob857943ee9018eaf55e8b00d321215341c38d79bf
1 <?php
2 /**
3 * Classes to cache objects in PHP accelerators, SQL database or DBA files
5 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Cache
27 /**
28 * @defgroup Cache Cache
31 /**
32 * interface is intended to be more or less compatible with
33 * the PHP memcached client.
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>
41 * @ingroup Cache
43 abstract class BagOStuff {
44 private $debugMode = false;
46 /**
47 * @param $bool bool
49 public function setDebug( $bool ) {
50 $this->debugMode = $bool;
53 /* *** THE GUTS OF THE OPERATION *** */
54 /* Override these with functional things in subclasses */
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
62 abstract public function get( $key, &$casToken = null );
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
71 abstract public function set( $key, $value, $exptime = 0 );
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
81 abstract public function cas( $casToken, $key, $value, $exptime = 0 );
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
89 abstract public function delete( $key, $time = 0 );
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).
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
102 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
103 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
107 * @see BagOStuff::merge()
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
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
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 );
130 } while ( !$success && --$attempts );
132 return $success;
136 * @see BagOStuff::merge()
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
144 protected function mergeViaLock( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
145 if ( !$this->lock( $key, 60 ) ) {
146 return false;
149 $currentValue = $this->get( $key ); // get the old value
150 $value = $callback( $this, $key, $currentValue ); // derive the new value
152 if ( $value === false ) {
153 $success = true; // do nothing
154 } else {
155 $success = $this->set( $key, $value, $exptime ); // set the new value
158 if ( !$this->unlock( $key ) ) {
159 // this should never happen
160 trigger_error( "Could not release lock for key '$key'." );
163 return $success;
167 * @param $key string
168 * @param $timeout integer [optional]
169 * @return bool success
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;
177 $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
178 $sleep = 2 * $uRTT; // rough time to do get()+set()
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;
188 usleep( $sleep ); // back off
189 $locked = $this->add( "{$key}:lock", 1, $timeout );
190 } while ( !$locked );
192 return $locked;
196 * @param $key string
197 * @return bool success
199 public function unlock( $key ) {
200 return $this->delete( "{$key}:lock" );
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.
210 * @return bool on success, false if unimplemented
212 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
213 // stub
214 return false;
217 /* *** Emulated functions *** */
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
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;
232 return $res;
236 * @param $key string
237 * @param $value mixed
238 * @param $exptime integer
239 * @return bool success
241 public function add( $key, $value, $exptime = 0 ) {
242 if ( $this->get( $key ) === false ) {
243 return $this->set( $key, $value, $exptime );
245 return false; // key already set
249 * @param $key string
250 * @param $value mixed
251 * @param $exptime int
252 * @return bool success
254 public function replace( $key, $value, $exptime = 0 ) {
255 if ( $this->get( $key ) !== false ) {
256 return $this->set( $key, $value, $exptime );
258 return false; // key not already set
262 * Increase stored value of $key by $value while preserving its TTL
263 * @param string $key Key to increase
264 * @param $value Integer: Value to add to $key (Default 1)
265 * @return integer|bool New value or false on failure
267 public function incr( $key, $value = 1 ) {
268 if ( !$this->lock( $key ) ) {
269 return false;
271 $n = $this->get( $key );
272 if ( $this->isInteger( $n ) ) { // key exists?
273 $n += intval( $value );
274 $this->set( $key, max( 0, $n ) ); // exptime?
275 } else {
276 $n = false;
278 $this->unlock( $key );
280 return $n;
284 * Decrease stored value of $key by $value while preserving its TTL
285 * @param $key String
286 * @param $value Integer
287 * @return integer
289 public function decr( $key, $value = 1 ) {
290 return $this->incr( $key, - $value );
294 * @param $text string
296 public function debug( $text ) {
297 if ( $this->debugMode ) {
298 $class = get_class( $this );
299 wfDebug( "$class debug: $text\n" );
304 * Convert an optionally relative time to an absolute time
305 * @param $exptime integer
306 * @return int
308 protected function convertExpiry( $exptime ) {
309 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
310 return time() + $exptime;
311 } else {
312 return $exptime;
317 * Convert an optionally absolute expiry time to a relative time. If an
318 * absolute time is specified which is in the past, use a short expiry time.
320 * @param $exptime integer
321 * @return integer
323 protected function convertToRelative( $exptime ) {
324 if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
325 $exptime -= time();
326 if ( $exptime <= 0 ) {
327 $exptime = 1;
329 return $exptime;
330 } else {
331 return $exptime;
336 * Check if a value is an integer
338 * @param $value mixed
339 * @return bool
341 protected function isInteger( $value ) {
342 return ( is_int( $value ) || ctype_digit( $value ) );