093f6716c25998b23ac6ce21fb94996e2f07981f
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 # $Id$
3 #
4 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # Simple generic object store
23 # interface is intended to be more or less compatible with
24 # the PHP memcached client.
25 #
26 # backends for local hash array and SQL table included:
27 # $bag = new HashBagOStuff();
28 # $bag = new MysqlBagOStuff($tablename); # connect to db first
29
30 class /* abstract */ BagOStuff {
31 var $debugmode;
32
33 function BagOStuff() {
34 $this->set_debug( false );
35 }
36
37 function set_debug($bool) {
38 $this->debugmode = $bool;
39 }
40
41 /* *** THE GUTS OF THE OPERATION *** */
42 /* Override these with functional things in subclasses */
43
44 function get($key) {
45 /* stub */
46 return false;
47 }
48
49 function set($key, $value, $exptime=0) {
50 /* stub */
51 return false;
52 }
53
54 function delete($key, $time=0) {
55 /* stub */
56 return false;
57 }
58
59 function lock($key, $timeout = 0) {
60 /* stub */
61 return true;
62 }
63
64 function unlock($key) {
65 /* stub */
66 return true;
67 }
68
69 /* *** Emulated functions *** */
70 /* Better performance can likely be got with custom written versions */
71 function get_multi($keys) {
72 $out = array();
73 foreach($keys as $key)
74 $out[$key] = $this->get($key);
75 return $out;
76 }
77
78 function set_multi($hash, $exptime=0) {
79 foreach($hash as $key => $value)
80 $this->set($key, $value, $exptime);
81 }
82
83 function add($key, $value, $exptime=0) {
84 if( $this->get($key) == false ) {
85 $this->set($key, $value, $exptime);
86 return true;
87 }
88 }
89
90 function add_multi($hash, $exptime=0) {
91 foreach($hash as $key => $value)
92 $this->add($key, $value, $exptime);
93 }
94
95 function delete_multi($keys, $time=0) {
96 foreach($keys as $key)
97 $this->delete($key, $time);
98 }
99
100 function replace($key, $value, $exptime=0) {
101 if( $this->get($key) !== false )
102 $this->set($key, $value, $exptime);
103 }
104
105 function incr($key, $value=1) {
106 if ( !$this->lock($key) ) {
107 return false;
108 }
109 $value = intval($value);
110 if($value < 0) $value = 0;
111
112 $n = false;
113 if( ($n = $this->get($key)) !== false ) {
114 $n += $value;
115 $this->set($key, $n); // exptime?
116 }
117 $this->unlock($key);
118 return $n;
119 }
120
121 function decr($key, $value=1) {
122 if ( !$this->lock($key) ) {
123 return false;
124 }
125 $value = intval($value);
126 if($value < 0) $value = 0;
127
128 $m = false;
129 if( ($n = $this->get($key)) !== false ) {
130 $m = $n - $value;
131 if($m < 0) $m = 0;
132 $this->set($key, $m); // exptime?
133 }
134 $this->unlock($key);
135 return $m;
136 }
137
138 function _debug($text) {
139 if($this->debugmode)
140 wfDebug("BagOStuff debug: $text\n");
141 }
142 }
143
144
145 /* Functional versions! */
146 class HashBagOStuff extends BagOStuff {
147 /*
148 This is a test of the interface, mainly. It stores
149 things in an associative array, which is not going to
150 persist between program runs.
151 */
152 var $bag;
153
154 function HashBagOStuff() {
155 $this->bag = array();
156 }
157
158 function _expire($key) {
159 $et = $this->bag[$key][1];
160 if(($et == 0) || ($et > time()))
161 return false;
162 $this->delete($key);
163 return true;
164 }
165
166 function get($key) {
167 if(!$this->bag[$key])
168 return false;
169 if($this->_expire($key))
170 return false;
171 return $this->bag[$key][0];
172 }
173
174 function set($key,$value,$exptime=0) {
175 if(($exptime != 0) && ($exptime < 3600*24*30))
176 $exptime = time() + $exptime;
177 $this->bag[$key] = array( $value, $exptime );
178 }
179
180 function delete($key,$time=0) {
181 if(!$this->bag[$key])
182 return false;
183 unset($this->bag[$key]);
184 return true;
185 }
186 }
187
188 /*
189 CREATE TABLE objectcache (
190 keyname char(255) binary not null default '',
191 value mediumblob,
192 exptime datetime,
193 unique key (keyname),
194 key (exptime)
195 );
196 */
197 class /* abstract */ SqlBagOStuff extends BagOStuff {
198 var $table;
199 function SqlBagOStuff($tablename = "objectcache") {
200 $this->table = $tablename;
201 }
202
203 function get($key) {
204 /* expire old entries if any */
205 $this->expireall();
206
207 $res = $this->_query(
208 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
209 if(!$res) {
210 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
211 return false;
212 }
213 if($row=$this->_fetchobject($res)) {
214 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
215 return unserialize($row->value);
216 } else {
217 $this->_debug("get: no matching rows");
218 }
219 return false;
220 }
221
222 function set($key,$value,$exptime=0) {
223 $exptime = intval($exptime);
224 if($exptime < 0) $exptime = 0;
225 if($exptime == 0) {
226 $exp = $this->_maxdatetime();
227 } else {
228 if($exptime < 3600*24*30)
229 $exptime += time();
230 $exp = $this->_fromunixtime($exptime);
231 }
232 $this->delete( $key );
233 $this->_query(
234 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
235 $key, serialize($value));
236 return true; /* ? */
237 }
238
239 function delete($key,$time=0) {
240 $this->_query(
241 "DELETE FROM $0 WHERE keyname='$1'", $key );
242 return true; /* ? */
243 }
244
245 function _query($sql) {
246 $reps = func_get_args();
247 $reps[0] = $this->table;
248 // ewwww
249 for($i=0;$i<count($reps);$i++) {
250 $sql = str_replace(
251 "$" . $i,
252 $this->_strencode($reps[$i]),
253 $sql);
254 }
255 $res = $this->_doquery($sql);
256 if($res == false) {
257 $this->_debug("query failed: " . $this->_dberror($res));
258 }
259 return $res;
260 }
261
262 function _strencode($str) {
263 /* Protect strings in SQL */
264 return str_replace( "'", "''", $str );
265 }
266
267 function _doquery($sql) {
268 die( "abstract function SqlBagOStuff::_doquery() must be defined" );
269 }
270
271 function _fetchrow($res) {
272 die( "abstract function SqlBagOStuff::_fetchrow() must be defined" );
273 }
274
275 function _freeresult($result) {
276 /* stub */
277 return false;
278 }
279
280 function _dberror($result) {
281 /* stub */
282 return "unknown error";
283 }
284
285 function _maxdatetime() {
286 die( "abstract function SqlBagOStuff::_maxdatetime() must be defined" );
287 }
288
289 function _fromunixtime() {
290 die( "abstract function SqlBagOStuff::_fromunixtime() must be defined" );
291 }
292
293 function expireall() {
294 /* Remove any items that have expired */
295 $now=$this->_fromunixtime(time());
296 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
297 }
298
299 function deleteall() {
300 /* Clear *all* items from cache table */
301 $this->_query( "DELETE FROM $0" );
302 }
303 }
304
305 class MediaWikiBagOStuff extends SqlBagOStuff {
306 function _doquery($sql) {
307 $dbw =& wfGetDB( DB_MASTER );
308 return $dbw->query($sql, "MediaWikiBagOStuff:_doquery");
309 }
310 function _fetchobject($result) {
311 $dbw =& wfGetDB( DB_MASTER );
312 return $dbw->fetchObject($result);
313 }
314 function _freeresult($result) {
315 $dbw =& wfGetDB( DB_MASTER );
316 return $dbw->freeResult($result);
317 }
318 function _dberror($result) {
319 $dbw =& wfGetDB( DB_MASTER );
320 return $dbw->lastError();
321 }
322 function _maxdatetime() {
323 return "9999-12-31 12:59:59";
324 }
325 function _fromunixtime($ts) {
326 return gmdate( "Y-m-d H:i:s", $ts );
327 }
328 function _strencode($s) {
329 $dbw =& wfGetDB( DB_MASTER );
330 return $dbw->strencode($s);
331 }
332 }
333
334 class TurckBagOStuff extends BagOStuff {
335 function get($key) {
336 return mmcache_get( $key );
337 }
338
339 function set($key, $value, $exptime=0) {
340 mmcache_put( $key, $value, $exptime );
341 return true;
342 }
343
344 function delete($key, $time=0) {
345 mmcache_rm( $key );
346 return true;
347 }
348
349 function lock($key, $waitTimeout = 0 ) {
350 mmcache_lock( $key );
351 return true;
352 }
353
354 function unlock($key) {
355 mmcache_unlock( $key );
356 return true;
357 }
358 }
359 ?>