Use compression in the objectcache table if zlib support is available.
[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 *
23 * @package MediaWiki
24 */
25
26 /**
27 * Simple generic object store
28 *
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
31 *
32 * backends for local hash array and SQL table included:
33 * $bag = new HashBagOStuff();
34 * $bag = new MysqlBagOStuff($tablename); # connect to db first
35 *
36 * @package MediaWiki
37 * @abstract
38 */
39 class BagOStuff {
40 var $debugmode;
41
42 function BagOStuff() {
43 $this->set_debug( false );
44 }
45
46 function set_debug($bool) {
47 $this->debugmode = $bool;
48 }
49
50 /* *** THE GUTS OF THE OPERATION *** */
51 /* Override these with functional things in subclasses */
52
53 function get($key) {
54 /* stub */
55 return false;
56 }
57
58 function set($key, $value, $exptime=0) {
59 /* stub */
60 return false;
61 }
62
63 function delete($key, $time=0) {
64 /* stub */
65 return false;
66 }
67
68 function lock($key, $timeout = 0) {
69 /* stub */
70 return true;
71 }
72
73 function unlock($key) {
74 /* stub */
75 return true;
76 }
77
78 /* *** Emulated functions *** */
79 /* Better performance can likely be got with custom written versions */
80 function get_multi($keys) {
81 $out = array();
82 foreach($keys as $key)
83 $out[$key] = $this->get($key);
84 return $out;
85 }
86
87 function set_multi($hash, $exptime=0) {
88 foreach($hash as $key => $value)
89 $this->set($key, $value, $exptime);
90 }
91
92 function add($key, $value, $exptime=0) {
93 if( $this->get($key) == false ) {
94 $this->set($key, $value, $exptime);
95 return true;
96 }
97 }
98
99 function add_multi($hash, $exptime=0) {
100 foreach($hash as $key => $value)
101 $this->add($key, $value, $exptime);
102 }
103
104 function delete_multi($keys, $time=0) {
105 foreach($keys as $key)
106 $this->delete($key, $time);
107 }
108
109 function replace($key, $value, $exptime=0) {
110 if( $this->get($key) !== false )
111 $this->set($key, $value, $exptime);
112 }
113
114 function incr($key, $value=1) {
115 if ( !$this->lock($key) ) {
116 return false;
117 }
118 $value = intval($value);
119 if($value < 0) $value = 0;
120
121 $n = false;
122 if( ($n = $this->get($key)) !== false ) {
123 $n += $value;
124 $this->set($key, $n); // exptime?
125 }
126 $this->unlock($key);
127 return $n;
128 }
129
130 function decr($key, $value=1) {
131 if ( !$this->lock($key) ) {
132 return false;
133 }
134 $value = intval($value);
135 if($value < 0) $value = 0;
136
137 $m = false;
138 if( ($n = $this->get($key)) !== false ) {
139 $m = $n - $value;
140 if($m < 0) $m = 0;
141 $this->set($key, $m); // exptime?
142 }
143 $this->unlock($key);
144 return $m;
145 }
146
147 function _debug($text) {
148 if($this->debugmode)
149 wfDebug("BagOStuff debug: $text\n");
150 }
151 }
152
153
154 /**
155 * Functional versions!
156 * @todo document
157 * @package MediaWiki
158 */
159 class HashBagOStuff extends BagOStuff {
160 /*
161 This is a test of the interface, mainly. It stores
162 things in an associative array, which is not going to
163 persist between program runs.
164 */
165 var $bag;
166
167 function HashBagOStuff() {
168 $this->bag = array();
169 }
170
171 function _expire($key) {
172 $et = $this->bag[$key][1];
173 if(($et == 0) || ($et > time()))
174 return false;
175 $this->delete($key);
176 return true;
177 }
178
179 function get($key) {
180 if(!$this->bag[$key])
181 return false;
182 if($this->_expire($key))
183 return false;
184 return $this->bag[$key][0];
185 }
186
187 function set($key,$value,$exptime=0) {
188 if(($exptime != 0) && ($exptime < 3600*24*30))
189 $exptime = time() + $exptime;
190 $this->bag[$key] = array( $value, $exptime );
191 }
192
193 function delete($key,$time=0) {
194 if(!$this->bag[$key])
195 return false;
196 unset($this->bag[$key]);
197 return true;
198 }
199 }
200
201 /*
202 CREATE TABLE objectcache (
203 keyname char(255) binary not null default '',
204 value mediumblob,
205 exptime datetime,
206 unique key (keyname),
207 key (exptime)
208 );
209 */
210
211 /**
212 * @todo document
213 * @abstract
214 * @package MediaWiki
215 */
216 class SqlBagOStuff extends BagOStuff {
217 var $table;
218
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table = $tablename;
221 }
222
223 function get($key) {
224 /* expire old entries if any */
225 $this->expireall();
226
227 $res = $this->_query(
228 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
229 if(!$res) {
230 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
231 return false;
232 }
233 if($row=$this->_fetchobject($res)) {
234 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
235 return $this->_unserialize($row->value);
236 } else {
237 $this->_debug('get: no matching rows');
238 }
239 return false;
240 }
241
242 function set($key,$value,$exptime=0) {
243 $exptime = intval($exptime);
244 if($exptime < 0) $exptime = 0;
245 if($exptime == 0) {
246 $exp = $this->_maxdatetime();
247 } else {
248 if($exptime < 3600*24*30)
249 $exptime += time();
250 $exp = $this->_fromunixtime($exptime);
251 }
252 $this->delete( $key );
253 $this->_query(
254 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
255 $key, $this->_serialize($value));
256 return true; /* ? */
257 }
258
259 function delete($key,$time=0) {
260 $this->_query(
261 "DELETE FROM $0 WHERE keyname='$1'", $key );
262 return true; /* ? */
263 }
264
265 function getTableName() {
266 return $this->table;
267 }
268
269 function _query($sql) {
270 $reps = func_get_args();
271 $reps[0] = $this->getTableName();
272 // ewwww
273 for($i=0;$i<count($reps);$i++) {
274 $sql = str_replace(
275 '$' . $i,
276 $this->_strencode($reps[$i]),
277 $sql);
278 }
279 $res = $this->_doquery($sql);
280 if($res == false) {
281 $this->_debug('query failed: ' . $this->_dberror($res));
282 }
283 return $res;
284 }
285
286 function _strencode($str) {
287 /* Protect strings in SQL */
288 return str_replace( "'", "''", $str );
289 }
290
291 function _doquery($sql) {
292 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
293 }
294
295 function _fetchrow($res) {
296 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
297 }
298
299 function _freeresult($result) {
300 /* stub */
301 return false;
302 }
303
304 function _dberror($result) {
305 /* stub */
306 return 'unknown error';
307 }
308
309 function _maxdatetime() {
310 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
311 }
312
313 function _fromunixtime() {
314 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
315 }
316
317 function expireall() {
318 /* Remove any items that have expired */
319 $now=$this->_fromunixtime(time());
320 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
321 }
322
323 function deleteall() {
324 /* Clear *all* items from cache table */
325 $this->_query( "DELETE FROM $0" );
326 }
327
328 /**
329 * Serialize an object and, if possible, compress the representation.
330 * On typical message and page data, this can provide a 3X decrease
331 * in storage requirements.
332 *
333 * @param mixed $data
334 * @return string
335 */
336 function _serialize( &$data ) {
337 $serial = serialize( $data );
338 if( function_exists( 'gzdeflate' ) ) {
339 return gzdeflate( $serial );
340 } else {
341 return $serial;
342 }
343 }
344
345 /**
346 * Unserialize and, if necessary, decompress an object.
347 * @param string $serial
348 * @return mixed
349 */
350 function &_unserialize( $serial ) {
351 if( function_exists( 'gzinflate' ) ) {
352 $decomp = @gzinflate( $serial );
353 if( false !== $decomp ) {
354 $serial = $decomp;
355 }
356 }
357 return unserialize( $serial );
358 }
359 }
360
361 /**
362 * @todo document
363 * @package MediaWiki
364 */
365 class MediaWikiBagOStuff extends SqlBagOStuff {
366 var $tableInitialised = false;
367
368 function _doquery($sql) {
369 $dbw =& wfGetDB( DB_MASTER );
370 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
371 }
372 function _fetchobject($result) {
373 $dbw =& wfGetDB( DB_MASTER );
374 return $dbw->fetchObject($result);
375 }
376 function _freeresult($result) {
377 $dbw =& wfGetDB( DB_MASTER );
378 return $dbw->freeResult($result);
379 }
380 function _dberror($result) {
381 $dbw =& wfGetDB( DB_MASTER );
382 return $dbw->lastError();
383 }
384 function _maxdatetime() {
385 return '9999-12-31 12:59:59';
386 }
387 function _fromunixtime($ts) {
388 return gmdate( 'Y-m-d H:i:s', $ts );
389 }
390 function _strencode($s) {
391 $dbw =& wfGetDB( DB_MASTER );
392 return $dbw->strencode($s);
393 }
394 function getTableName() {
395 if ( !$this->tableInitialised ) {
396 $dbw =& wfGetDB( DB_MASTER );
397 $this->table = $dbw->tableName( $this->table );
398 $this->tableInitialised = true;
399 }
400 return $this->table;
401 }
402 }
403
404 /**
405 * This is a wrapper for Turck MMCache's shared memory functions.
406 *
407 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
408 * to use a weird custom serializer that randomly segfaults. So we wrap calls
409 * with serialize()/unserialize().
410 *
411 * The thing I noticed about the Turck serialized data was that unlike ordinary
412 * serialize(), it contained the names of methods, and judging by the amount of
413 * binary data, perhaps even the bytecode of the methods themselves. It may be
414 * that Turck's serializer is faster, so a possible future extension would be
415 * to use it for arrays but not for objects.
416 *
417 * @package MediaWiki
418 */
419 class TurckBagOStuff extends BagOStuff {
420 function get($key) {
421 $val = mmcache_get( $key );
422 if ( is_string( $val ) ) {
423 $val = unserialize( $val );
424 }
425 return $val;
426 }
427
428 function set($key, $value, $exptime=0) {
429 mmcache_put( $key, serialize( $value ), $exptime );
430 return true;
431 }
432
433 function delete($key, $time=0) {
434 mmcache_rm( $key );
435 return true;
436 }
437
438 function lock($key, $waitTimeout = 0 ) {
439 mmcache_lock( $key );
440 return true;
441 }
442
443 function unlock($key) {
444 mmcache_unlock( $key );
445 return true;
446 }
447 }
448 ?>