Merge "(bug 19195) Make user IDs more readily available with the API"
[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 * Get an associative array containing the item for each of the given keys.
65 * Each item will be false if it does not exist.
66 * @param $keys Array List of strings
67 * @return Array
68 */
69 public function getBatch( array $keys ) {
70 $res = array();
71 foreach ( $keys as $key ) {
72 $res[$key] = $this->get( $key );
73 }
74 return $res;
75 }
76
77 /**
78 * Set an item.
79 * @param $key string
80 * @param $value mixed
81 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
82 * @return bool success
83 */
84 abstract public function set( $key, $value, $exptime = 0 );
85
86 /**
87 * Delete an item.
88 * @param $key string
89 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
90 * @return bool success
91 */
92 abstract public function delete( $key, $time = 0 );
93
94 /**
95 * @param $key string
96 * @param $timeout integer
97 * @return bool success
98 */
99 public function lock( $key, $timeout = 0 ) {
100 /* stub */
101 return true;
102 }
103
104 /**
105 * @param $key string
106 * @return bool success
107 */
108 public function unlock( $key ) {
109 /* stub */
110 return true;
111 }
112
113 /**
114 * @todo: what is this?
115 * @return Array
116 */
117 public function keys() {
118 /* stub */
119 return array();
120 }
121
122 /**
123 * Delete all objects expiring before a certain date.
124 * @param $date string The reference date in MW format
125 * @param $progressCallback callback|bool Optional, a function which will be called
126 * regularly during long-running operations with the percentage progress
127 * as the first parameter.
128 *
129 * @return bool on success, false if unimplemented
130 */
131 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
132 // stub
133 return false;
134 }
135
136 /* *** Emulated functions *** */
137
138 /**
139 * @param $key string
140 * @param $value mixed
141 * @param $exptime integer
142 * @return bool success
143 */
144 public function add( $key, $value, $exptime = 0 ) {
145 if ( !$this->get( $key ) ) {
146 return $this->set( $key, $value, $exptime );
147 }
148 return true;
149 }
150
151 /**
152 * @param $key string
153 * @param $value mixed
154 * @return bool success
155 */
156 public function replace( $key, $value, $exptime = 0 ) {
157 if ( $this->get( $key ) !== false ) {
158 return $this->set( $key, $value, $exptime );
159 }
160 return true;
161 }
162
163 /**
164 * @param $key String: Key to increase
165 * @param $value Integer: Value to add to $key (Default 1)
166 * @return null if lock is not possible else $key value increased by $value
167 * @return success
168 */
169 public function incr( $key, $value = 1 ) {
170 if ( !$this->lock( $key ) ) {
171 return null;
172 }
173
174 $value = intval( $value );
175
176 if ( ( $n = $this->get( $key ) ) !== false ) {
177 $n += $value;
178 $this->set( $key, $n ); // exptime?
179 }
180 $this->unlock( $key );
181
182 return $n;
183 }
184
185 /**
186 * @param $key String
187 * @param $value Integer
188 * @return bool success
189 */
190 public function decr( $key, $value = 1 ) {
191 return $this->incr( $key, - $value );
192 }
193
194 /**
195 * @param $text string
196 */
197 public function debug( $text ) {
198 if ( $this->debugMode ) {
199 $class = get_class( $this );
200 wfDebug( "$class debug: $text\n" );
201 }
202 }
203
204 /**
205 * Convert an optionally relative time to an absolute time
206 * @param $exptime integer
207 * @return int
208 */
209 protected function convertExpiry( $exptime ) {
210 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
211 return time() + $exptime;
212 } else {
213 return $exptime;
214 }
215 }
216 }