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
28 * @defgroup Cache Cache
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:
37 * $bag = new HashBagOStuff();
38 * $bag = new SqlBagOStuff(); # connect to db first
43 abstract class BagOStuff
{
44 private $debugMode = false;
49 public function setDebug( $bool ) {
50 $this->debugMode
= $bool;
53 /* *** THE GUTS OF THE OPERATION *** */
54 /* Override these with functional things in subclasses */
57 * Get an item with the given key. Returns false if it does not exist.
59 * @return mixed Returns false on failure
61 abstract public function get( $key );
67 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
68 * @return bool success
70 abstract public function set( $key, $value, $exptime = 0 );
75 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
76 * @return bool True if the item was deleted or not found, false on failure
78 abstract public function delete( $key, $time = 0 );
82 * @param $timeout integer
83 * @return bool success
85 public function lock( $key, $timeout = 0 ) {
92 * @return bool success
94 public function unlock( $key ) {
100 * @todo: what is this?
103 public function keys() {
109 * Delete all objects expiring before a certain date.
110 * @param $date string The reference date in MW format
111 * @param $progressCallback callback|bool Optional, a function which will be called
112 * regularly during long-running operations with the percentage progress
113 * as the first parameter.
115 * @return bool on success, false if unimplemented
117 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
122 /* *** Emulated functions *** */
125 * Get an associative array containing the item for each of the keys that have items.
126 * @param $keys Array List of strings
129 public function getMulti( array $keys ) {
131 foreach ( $keys as $key ) {
132 $val = $this->get( $key );
133 if ( $val !== false ) {
142 * @param $value mixed
143 * @param $exptime integer
144 * @return bool success
146 public function add( $key, $value, $exptime = 0 ) {
147 if ( $this->get( $key ) === false ) {
148 return $this->set( $key, $value, $exptime );
150 return false; // key already set
155 * @param $value mixed
156 * @param $exptime int
157 * @return bool success
159 public function replace( $key, $value, $exptime = 0 ) {
160 if ( $this->get( $key ) !== false ) {
161 return $this->set( $key, $value, $exptime );
163 return false; // key not already set
167 * Increase stored value of $key by $value while preserving its TTL
168 * @param $key String: Key to increase
169 * @param $value Integer: Value to add to $key (Default 1)
170 * @return integer|bool New value or false on failure
172 public function incr( $key, $value = 1 ) {
173 if ( !$this->lock( $key ) ) {
176 $n = $this->get( $key );
177 if ( $this->isInteger( $n ) ) { // key exists?
178 $n +
= intval( $value );
179 $this->set( $key, max( 0, $n ) ); // exptime?
183 $this->unlock( $key );
189 * Decrease stored value of $key by $value while preserving its TTL
191 * @param $value Integer
194 public function decr( $key, $value = 1 ) {
195 return $this->incr( $key, - $value );
199 * @param $text string
201 public function debug( $text ) {
202 if ( $this->debugMode
) {
203 $class = get_class( $this );
204 wfDebug( "$class debug: $text\n" );
209 * Convert an optionally relative time to an absolute time
210 * @param $exptime integer
213 protected function convertExpiry( $exptime ) {
214 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
215 return time() +
$exptime;
222 * Convert an optionally absolute expiry time to a relative time. If an
223 * absolute time is specified which is in the past, use a short expiry time.
225 * @param $exptime integer
228 protected function convertToRelative( $exptime ) {
229 if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
231 if ( $exptime <= 0 ) {
241 * Check if a value is an integer
243 * @param $value mixed
246 protected function isInteger( $value ) {
247 return ( is_int( $value ) ||
ctype_digit( $value ) );