merging latest master
[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 * @return mixed Returns false on failure
60 */
61 abstract public function get( $key );
62
63 /**
64 * Set an item.
65 * @param $key string
66 * @param $value mixed
67 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
68 * @return bool success
69 */
70 abstract public function set( $key, $value, $exptime = 0 );
71
72 /**
73 * Delete an item.
74 * @param $key string
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
77 */
78 abstract public function delete( $key, $time = 0 );
79
80 /**
81 * @param $key string
82 * @param $timeout integer
83 * @return bool success
84 */
85 public function lock( $key, $timeout = 0 ) {
86 /* stub */
87 return true;
88 }
89
90 /**
91 * @param $key string
92 * @return bool success
93 */
94 public function unlock( $key ) {
95 /* stub */
96 return true;
97 }
98
99 /**
100 * @todo: what is this?
101 * @return Array
102 */
103 public function keys() {
104 /* stub */
105 return array();
106 }
107
108 /**
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.
114 *
115 * @return bool on success, false if unimplemented
116 */
117 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
118 // stub
119 return false;
120 }
121
122 /* *** Emulated functions *** */
123
124 /**
125 * Get an associative array containing the item for each of the keys that have items.
126 * @param $keys Array List of strings
127 * @return Array
128 */
129 public function getMulti( array $keys ) {
130 $res = array();
131 foreach ( $keys as $key ) {
132 $val = $this->get( $key );
133 if ( $val !== false ) {
134 $res[$key] = $val;
135 }
136 }
137 return $res;
138 }
139
140 /**
141 * @param $key string
142 * @param $value mixed
143 * @param $exptime integer
144 * @return bool success
145 */
146 public function add( $key, $value, $exptime = 0 ) {
147 if ( $this->get( $key ) === false ) {
148 return $this->set( $key, $value, $exptime );
149 }
150 return false; // key already set
151 }
152
153 /**
154 * @param $key string
155 * @param $value mixed
156 * @param $exptime int
157 * @return bool success
158 */
159 public function replace( $key, $value, $exptime = 0 ) {
160 if ( $this->get( $key ) !== false ) {
161 return $this->set( $key, $value, $exptime );
162 }
163 return false; // key not already set
164 }
165
166 /**
167 * @param $key String: Key to increase
168 * @param $value Integer: Value to add to $key (Default 1)
169 * @return null if lock is not possible else $key value increased by $value
170 * @return bool success
171 */
172 public function incr( $key, $value = 1 ) {
173 if ( !$this->lock( $key ) ) {
174 return null;
175 }
176
177 $value = intval( $value );
178
179 if ( ( $n = $this->get( $key ) ) !== false ) {
180 $n += $value;
181 $this->set( $key, $n ); // exptime?
182 }
183 $this->unlock( $key );
184
185 return $n;
186 }
187
188 /**
189 * @param $key String
190 * @param $value Integer
191 * @return bool success
192 */
193 public function decr( $key, $value = 1 ) {
194 return $this->incr( $key, - $value );
195 }
196
197 /**
198 * @param $text string
199 */
200 public function debug( $text ) {
201 if ( $this->debugMode ) {
202 $class = get_class( $this );
203 wfDebug( "$class debug: $text\n" );
204 }
205 }
206
207 /**
208 * Convert an optionally relative time to an absolute time
209 * @param $exptime integer
210 * @return int
211 */
212 protected function convertExpiry( $exptime ) {
213 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
214 return time() + $exptime;
215 } else {
216 return $exptime;
217 }
218 }
219
220 /**
221 * Convert an optionally absolute expiry time to a relative time. If an
222 * absolute time is specified which is in the past, use a short expiry time.
223 *
224 * @param $exptime integer
225 * @return integer
226 */
227 protected function convertToRelative( $exptime ) {
228 if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
229 $exptime -= time();
230 if ( $exptime <= 0 ) {
231 $exptime = 1;
232 }
233 return $exptime;
234 } else {
235 return $exptime;
236 }
237 }
238 }