f85722ba69708373244ca1a6d7e9c6eb145c6f27
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 var $debugMode = false;
46 public function set_debug( $bool ) {
47 $this->debugMode
= $bool;
50 /* *** THE GUTS OF THE OPERATION *** */
51 /* Override these with functional things in subclasses */
54 * Get an item with the given key. Returns false if it does not exist.
57 abstract public function get( $key );
63 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
65 abstract public function set( $key, $value, $exptime = 0 );
70 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
72 abstract public function delete( $key, $time = 0 );
74 public function lock( $key, $timeout = 0 ) {
79 public function unlock( $key ) {
84 public function keys() {
89 /* *** Emulated functions *** */
90 /* Better performance can likely be got with custom written versions */
91 public function get_multi( $keys ) {
94 foreach ( $keys as $key ) {
95 $out[$key] = $this->get( $key );
101 public function set_multi( $hash, $exptime = 0 ) {
102 foreach ( $hash as $key => $value ) {
103 $this->set( $key, $value, $exptime );
107 public function add( $key, $value, $exptime = 0 ) {
108 if ( !$this->get( $key ) ) {
109 $this->set( $key, $value, $exptime );
115 public function add_multi( $hash, $exptime = 0 ) {
116 foreach ( $hash as $key => $value ) {
117 $this->add( $key, $value, $exptime );
121 public function delete_multi( $keys, $time = 0 ) {
122 foreach ( $keys as $key ) {
123 $this->delete( $key, $time );
127 public function replace( $key, $value, $exptime = 0 ) {
128 if ( $this->get( $key ) !== false ) {
129 $this->set( $key, $value, $exptime );
134 * @param $key String: Key to increase
135 * @param $value Integer: Value to add to $key (Default 1)
136 * @return null if lock is not possible else $key value increased by $value
138 public function incr( $key, $value = 1 ) {
139 if ( !$this->lock( $key ) ) {
143 $value = intval( $value );
145 if ( ( $n = $this->get( $key ) ) !== false ) {
147 $this->set( $key, $n ); // exptime?
149 $this->unlock( $key );
154 public function decr( $key, $value = 1 ) {
155 return $this->incr( $key, - $value );
158 public function debug( $text ) {
159 if ( $this->debugMode
) {
160 wfDebug( "BagOStuff debug: $text\n" );
165 * Convert an optionally relative time to an absolute time
167 protected function convertExpiry( $exptime ) {
168 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
169 return time() +
$exptime;