[FileRepo] Avoid performance degeration due to thumbnail requests with a read-only...
[mediawiki.git] / includes / objectcache / BagOStuff.php
bloba4545ef83b40ab2448fa0b68111fedf85889e84f
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
60 * @return bool|Object
62 abstract public function get( $key );
64 /**
65 * Set an item.
66 * @param $key string
67 * @param $value mixed
68 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
70 abstract public function set( $key, $value, $exptime = 0 );
72 /**
73 * Delete an item.
74 * @param $key string
75 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
77 abstract public function delete( $key, $time = 0 );
79 public function lock( $key, $timeout = 0 ) {
80 /* stub */
81 return true;
84 public function unlock( $key ) {
85 /* stub */
86 return true;
89 public function keys() {
90 /* stub */
91 return array();
94 /**
95 * Delete all objects expiring before a certain date.
96 * @param $date string The reference date in MW format
97 * @param $progressCallback callback|bool Optional, a function which will be called
98 * regularly during long-running operations with the percentage progress
99 * as the first parameter.
101 * @return bool on success, false if unimplemented
103 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
104 // stub
105 return false;
108 /* *** Emulated functions *** */
110 public function add( $key, $value, $exptime = 0 ) {
111 if ( !$this->get( $key ) ) {
112 $this->set( $key, $value, $exptime );
114 return true;
118 public function replace( $key, $value, $exptime = 0 ) {
119 if ( $this->get( $key ) !== false ) {
120 $this->set( $key, $value, $exptime );
125 * @param $key String: Key to increase
126 * @param $value Integer: Value to add to $key (Default 1)
127 * @return null if lock is not possible else $key value increased by $value
129 public function incr( $key, $value = 1 ) {
130 if ( !$this->lock( $key ) ) {
131 return null;
134 $value = intval( $value );
136 if ( ( $n = $this->get( $key ) ) !== false ) {
137 $n += $value;
138 $this->set( $key, $n ); // exptime?
140 $this->unlock( $key );
142 return $n;
145 public function decr( $key, $value = 1 ) {
146 return $this->incr( $key, - $value );
149 public function debug( $text ) {
150 if ( $this->debugMode ) {
151 $class = get_class( $this );
152 wfDebug( "$class debug: $text\n" );
157 * Convert an optionally relative time to an absolute time
158 * @return int
160 protected function convertExpiry( $exptime ) {
161 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
162 return time() + $exptime;
163 } else {
164 return $exptime;