In DBA caches: fixed return value when the key is missing
[lhc/web/wiklou.git] / includes / BagOStuff.php
1 <?php
2 #
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20 /**
21 *
22 * @package MediaWiki
23 */
24
25 /**
26 * Simple generic object store
27 *
28 * interface is intended to be more or less compatible with
29 * the PHP memcached client.
30 *
31 * backends for local hash array and SQL table included:
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
34 *
35 * @package MediaWiki
36 */
37 class BagOStuff {
38 var $debugmode;
39
40 function BagOStuff() {
41 $this->set_debug( false );
42 }
43
44 function set_debug($bool) {
45 $this->debugmode = $bool;
46 }
47
48 /* *** THE GUTS OF THE OPERATION *** */
49 /* Override these with functional things in subclasses */
50
51 function get($key) {
52 /* stub */
53 return false;
54 }
55
56 function set($key, $value, $exptime=0) {
57 /* stub */
58 return false;
59 }
60
61 function delete($key, $time=0) {
62 /* stub */
63 return false;
64 }
65
66 function lock($key, $timeout = 0) {
67 /* stub */
68 return true;
69 }
70
71 function unlock($key) {
72 /* stub */
73 return true;
74 }
75
76 /* *** Emulated functions *** */
77 /* Better performance can likely be got with custom written versions */
78 function get_multi($keys) {
79 $out = array();
80 foreach($keys as $key)
81 $out[$key] = $this->get($key);
82 return $out;
83 }
84
85 function set_multi($hash, $exptime=0) {
86 foreach($hash as $key => $value)
87 $this->set($key, $value, $exptime);
88 }
89
90 function add($key, $value, $exptime=0) {
91 if( $this->get($key) == false ) {
92 $this->set($key, $value, $exptime);
93 return true;
94 }
95 }
96
97 function add_multi($hash, $exptime=0) {
98 foreach($hash as $key => $value)
99 $this->add($key, $value, $exptime);
100 }
101
102 function delete_multi($keys, $time=0) {
103 foreach($keys as $key)
104 $this->delete($key, $time);
105 }
106
107 function replace($key, $value, $exptime=0) {
108 if( $this->get($key) !== false )
109 $this->set($key, $value, $exptime);
110 }
111
112 function incr($key, $value=1) {
113 if ( !$this->lock($key) ) {
114 return false;
115 }
116 $value = intval($value);
117 if($value < 0) $value = 0;
118
119 $n = false;
120 if( ($n = $this->get($key)) !== false ) {
121 $n += $value;
122 $this->set($key, $n); // exptime?
123 }
124 $this->unlock($key);
125 return $n;
126 }
127
128 function decr($key, $value=1) {
129 if ( !$this->lock($key) ) {
130 return false;
131 }
132 $value = intval($value);
133 if($value < 0) $value = 0;
134
135 $m = false;
136 if( ($n = $this->get($key)) !== false ) {
137 $m = $n - $value;
138 if($m < 0) $m = 0;
139 $this->set($key, $m); // exptime?
140 }
141 $this->unlock($key);
142 return $m;
143 }
144
145 function _debug($text) {
146 if($this->debugmode)
147 wfDebug("BagOStuff debug: $text\n");
148 }
149
150 /**
151 * Convert an optionally relative time to an absolute time
152 */
153 static function convertExpiry( $exptime ) {
154 if(($exptime != 0) && ($exptime < 3600*24*30)) {
155 return time() + $exptime;
156 } else {
157 return $exptime;
158 }
159 }
160 }
161
162
163 /**
164 * Functional versions!
165 * @todo document
166 * @package MediaWiki
167 */
168 class HashBagOStuff extends BagOStuff {
169 /*
170 This is a test of the interface, mainly. It stores
171 things in an associative array, which is not going to
172 persist between program runs.
173 */
174 var $bag;
175
176 function HashBagOStuff() {
177 $this->bag = array();
178 }
179
180 function _expire($key) {
181 $et = $this->bag[$key][1];
182 if(($et == 0) || ($et > time()))
183 return false;
184 $this->delete($key);
185 return true;
186 }
187
188 function get($key) {
189 if(!$this->bag[$key])
190 return false;
191 if($this->_expire($key))
192 return false;
193 return $this->bag[$key][0];
194 }
195
196 function set($key,$value,$exptime=0) {
197 $this->bag[$key] = array( $value, BagOStuff::convertExpiry( $exptime ) );
198 }
199
200 function delete($key,$time=0) {
201 if(!$this->bag[$key])
202 return false;
203 unset($this->bag[$key]);
204 return true;
205 }
206 }
207
208 /*
209 CREATE TABLE objectcache (
210 keyname char(255) binary not null default '',
211 value mediumblob,
212 exptime datetime,
213 unique key (keyname),
214 key (exptime)
215 );
216 */
217
218 /**
219 * @todo document
220 * @abstract
221 * @package MediaWiki
222 */
223 abstract class SqlBagOStuff extends BagOStuff {
224 var $table;
225 var $lastexpireall = 0;
226
227 function SqlBagOStuff($tablename = 'objectcache') {
228 $this->table = $tablename;
229 }
230
231 function get($key) {
232 /* expire old entries if any */
233 $this->garbageCollect();
234
235 $res = $this->_query(
236 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
237 if(!$res) {
238 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
239 return false;
240 }
241 if($row=$this->_fetchobject($res)) {
242 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
243 return $this->_unserialize($this->_blobdecode($row->value));
244 } else {
245 $this->_debug('get: no matching rows');
246 }
247 return false;
248 }
249
250 function set($key,$value,$exptime=0) {
251 $exptime = intval($exptime);
252 if($exptime < 0) $exptime = 0;
253 if($exptime == 0) {
254 $exp = $this->_maxdatetime();
255 } else {
256 if($exptime < 3600*24*30)
257 $exptime += time();
258 $exp = $this->_fromunixtime($exptime);
259 }
260 $this->delete( $key );
261 $this->_doinsert($this->getTableName(), array(
262 'keyname' => $key,
263 'value' => $this->_blobencode($this->_serialize($value)),
264 'exptime' => $exp
265 ));
266 return true; /* ? */
267 }
268
269 function delete($key,$time=0) {
270 $this->_query(
271 "DELETE FROM $0 WHERE keyname='$1'", $key );
272 return true; /* ? */
273 }
274
275 function getTableName() {
276 return $this->table;
277 }
278
279 function _query($sql) {
280 $reps = func_get_args();
281 $reps[0] = $this->getTableName();
282 // ewwww
283 for($i=0;$i<count($reps);$i++) {
284 $sql = str_replace(
285 '$' . $i,
286 $i > 0 ? $this->_strencode($reps[$i]) : $reps[$i],
287 $sql);
288 }
289 $res = $this->_doquery($sql);
290 if($res == false) {
291 $this->_debug('query failed: ' . $this->_dberror($res));
292 }
293 return $res;
294 }
295
296 function _strencode($str) {
297 /* Protect strings in SQL */
298 return str_replace( "'", "''", $str );
299 }
300 function _blobencode($str) {
301 return $str;
302 }
303 function _blobdecode($str) {
304 return $str;
305 }
306
307 abstract function _doinsert($table, $vals);
308 abstract function _doquery($sql);
309
310 function _freeresult($result) {
311 /* stub */
312 return false;
313 }
314
315 function _dberror($result) {
316 /* stub */
317 return 'unknown error';
318 }
319
320 abstract function _maxdatetime();
321 abstract function _fromunixtime($ts);
322
323 function garbageCollect() {
324 /* Ignore 99% of requests */
325 if ( !mt_rand( 0, 100 ) ) {
326 $nowtime = time();
327 /* Avoid repeating the delete within a few seconds */
328 if ( $nowtime > ($this->lastexpireall + 1) ) {
329 $this->lastexpireall = $nowtime;
330 $this->expireall();
331 }
332 }
333 }
334
335 function expireall() {
336 /* Remove any items that have expired */
337 $now = $this->_fromunixtime( time() );
338 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
339 }
340
341 function deleteall() {
342 /* Clear *all* items from cache table */
343 $this->_query( "DELETE FROM $0" );
344 }
345
346 /**
347 * Serialize an object and, if possible, compress the representation.
348 * On typical message and page data, this can provide a 3X decrease
349 * in storage requirements.
350 *
351 * @param mixed $data
352 * @return string
353 */
354 function _serialize( &$data ) {
355 $serial = serialize( $data );
356 if( function_exists( 'gzdeflate' ) ) {
357 return gzdeflate( $serial );
358 } else {
359 return $serial;
360 }
361 }
362
363 /**
364 * Unserialize and, if necessary, decompress an object.
365 * @param string $serial
366 * @return mixed
367 */
368 function _unserialize( $serial ) {
369 if( function_exists( 'gzinflate' ) ) {
370 $decomp = @gzinflate( $serial );
371 if( false !== $decomp ) {
372 $serial = $decomp;
373 }
374 }
375 $ret = unserialize( $serial );
376 return $ret;
377 }
378 }
379
380 /**
381 * @todo document
382 * @package MediaWiki
383 */
384 class MediaWikiBagOStuff extends SqlBagOStuff {
385 var $tableInitialised = false;
386
387 function _doquery($sql) {
388 $dbw =& wfGetDB( DB_MASTER );
389 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
390 }
391 function _doinsert($t, $v) {
392 $dbw =& wfGetDB( DB_MASTER );
393 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert');
394 }
395 function _fetchobject($result) {
396 $dbw =& wfGetDB( DB_MASTER );
397 return $dbw->fetchObject($result);
398 }
399 function _freeresult($result) {
400 $dbw =& wfGetDB( DB_MASTER );
401 return $dbw->freeResult($result);
402 }
403 function _dberror($result) {
404 $dbw =& wfGetDB( DB_MASTER );
405 return $dbw->lastError();
406 }
407 function _maxdatetime() {
408 $dbw =& wfGetDB(DB_MASTER);
409 return $dbw->timestamp('9999-12-31 12:59:59');
410 }
411 function _fromunixtime($ts) {
412 $dbw =& wfGetDB(DB_MASTER);
413 return $dbw->timestamp($ts);
414 }
415 function _strencode($s) {
416 $dbw =& wfGetDB( DB_MASTER );
417 return $dbw->strencode($s);
418 }
419 function _blobencode($s) {
420 $dbw =& wfGetDB( DB_MASTER );
421 return $dbw->encodeBlob($s);
422 }
423 function _blobdecode($s) {
424 $dbw =& wfGetDB( DB_MASTER );
425 return $dbw->decodeBlob($s);
426 }
427 function getTableName() {
428 if ( !$this->tableInitialised ) {
429 $dbw =& wfGetDB( DB_MASTER );
430 /* This is actually a hack, we should be able
431 to use Language classes here... or not */
432 if (!$dbw)
433 throw new MWException("Could not connect to database");
434 $this->table = $dbw->tableName( $this->table );
435 $this->tableInitialised = true;
436 }
437 return $this->table;
438 }
439 }
440
441 /**
442 * This is a wrapper for Turck MMCache's shared memory functions.
443 *
444 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
445 * to use a weird custom serializer that randomly segfaults. So we wrap calls
446 * with serialize()/unserialize().
447 *
448 * The thing I noticed about the Turck serialized data was that unlike ordinary
449 * serialize(), it contained the names of methods, and judging by the amount of
450 * binary data, perhaps even the bytecode of the methods themselves. It may be
451 * that Turck's serializer is faster, so a possible future extension would be
452 * to use it for arrays but not for objects.
453 *
454 * @package MediaWiki
455 */
456 class TurckBagOStuff extends BagOStuff {
457 function get($key) {
458 $val = mmcache_get( $key );
459 if ( is_string( $val ) ) {
460 $val = unserialize( $val );
461 }
462 return $val;
463 }
464
465 function set($key, $value, $exptime=0) {
466 mmcache_put( $key, serialize( $value ), $exptime );
467 return true;
468 }
469
470 function delete($key, $time=0) {
471 mmcache_rm( $key );
472 return true;
473 }
474
475 function lock($key, $waitTimeout = 0 ) {
476 mmcache_lock( $key );
477 return true;
478 }
479
480 function unlock($key) {
481 mmcache_unlock( $key );
482 return true;
483 }
484 }
485
486 /**
487 * This is a wrapper for APC's shared memory functions
488 *
489 * @package MediaWiki
490 */
491
492 class APCBagOStuff extends BagOStuff {
493 function get($key) {
494 $val = apc_fetch($key);
495 return $val;
496 }
497
498 function set($key, $value, $exptime=0) {
499 apc_store($key, $value, $exptime);
500 return true;
501 }
502
503 function delete($key, $time=0) {
504 apc_delete($key);
505 return true;
506 }
507 }
508
509
510 /**
511 * This is a wrapper for eAccelerator's shared memory functions.
512 *
513 * This is basically identical to the Turck MMCache version,
514 * mostly because eAccelerator is based on Turck MMCache.
515 *
516 * @package MediaWiki
517 */
518 class eAccelBagOStuff extends BagOStuff {
519 function get($key) {
520 $val = eaccelerator_get( $key );
521 if ( is_string( $val ) ) {
522 $val = unserialize( $val );
523 }
524 return $val;
525 }
526
527 function set($key, $value, $exptime=0) {
528 eaccelerator_put( $key, serialize( $value ), $exptime );
529 return true;
530 }
531
532 function delete($key, $time=0) {
533 eaccelerator_rm( $key );
534 return true;
535 }
536
537 function lock($key, $waitTimeout = 0 ) {
538 eaccelerator_lock( $key );
539 return true;
540 }
541
542 function unlock($key) {
543 eaccelerator_unlock( $key );
544 return true;
545 }
546 }
547
548 class DBABagOStuff extends BagOStuff {
549 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
550
551 function __construct( $handler = 'db3', $dir = false ) {
552 if ( $dir === false ) {
553 global $wgTmpDirectory;
554 $dir = $wgTmpDirectory;
555 }
556 global $wgDBname, $wgDBprefix;
557 $this->mFile = "$dir/mw-cache-$wgDBname";
558 if ( $wgDBprefix ) {
559 $this->mFile .= '-' . $wgDBprefix;
560 }
561 $this->mFile .= '.db';
562 $this->mHandler = $handler;
563 }
564
565 /**
566 * Encode value and expiry for storage
567 */
568 function encode( $value, $expiry ) {
569 # Convert to absolute time
570 $expiry = BagOStuff::convertExpiry( $expiry );
571 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
572 }
573
574 /**
575 * @return list containing value first and expiry second
576 */
577 function decode( $blob ) {
578 if ( !is_string( $blob ) ) {
579 return array( null, 0 );
580 } else {
581 return array(
582 unserialize( substr( $blob, 11 ) ),
583 intval( substr( $blob, 0, 10 ) )
584 );
585 }
586 }
587
588 function getReader() {
589 if ( file_exists( $this->mFile ) ) {
590 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
591 } else {
592 $handle = $this->getWriter();
593 }
594 if ( !$handle ) {
595 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
596 }
597 return $handle;
598 }
599
600 function getWriter() {
601 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
602 if ( !$handle ) {
603 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
604 }
605 return $handle;
606 }
607
608 function get( $key ) {
609 wfProfileIn( __METHOD__ );
610 wfDebug( __METHOD__."($key)\n" );
611 $handle = $this->getReader();
612 if ( !$handle ) {
613 return null;
614 }
615 $val = dba_fetch( $key, $handle );
616 list( $val, $expiry ) = $this->decode( $val );
617 # Must close ASAP because locks are held
618 dba_close( $handle );
619
620 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
621 # Key is expired, delete it
622 $handle = $this->getWriter();
623 dba_delete( $key, $handle );
624 dba_close( $handle );
625 wfDebug( __METHOD__.": $key expired\n" );
626 $val = null;
627 }
628 wfProfileOut( __METHOD__ );
629 return $val;
630 }
631
632 function set( $key, $value, $exptime=0 ) {
633 wfProfileIn( __METHOD__ );
634 wfDebug( __METHOD__."($key)\n" );
635 $blob = $this->encode( $value, $exptime );
636 $handle = $this->getWriter();
637 if ( !$handle ) {
638 return false;
639 }
640 $ret = dba_replace( $key, $blob, $handle );
641 dba_close( $handle );
642 wfProfileOut( __METHOD__ );
643 return $ret;
644 }
645
646 function delete( $key, $time = 0 ) {
647 wfProfileIn( __METHOD__ );
648 $handle = $this->getWriter();
649 if ( !$handle ) {
650 return false;
651 }
652 $ret = dba_delete( $key, $handle );
653 dba_close( $handle );
654 wfProfileOut( __METHOD__ );
655 return $ret;
656 }
657
658 function add( $key, $value, $exptime = 0 ) {
659 wfProfileIn( __METHOD__ );
660 $blob = $this->encode( $value, $exptime );
661 $handle = $this->getWriter();
662 if ( !$handle ) {
663 return false;
664 }
665 $ret = dba_insert( $key, $blob, $handle );
666 # Insert failed, check to see if it failed due to an expired key
667 if ( !$ret ) {
668 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
669 if ( $expiry < time() ) {
670 # Yes expired, delete and try again
671 dba_delete( $key, $handle );
672 $ret = dba_insert( $key, $blob, $handle );
673 # This time if it failed then it will be handled by the caller like any other race
674 }
675 }
676
677 dba_close( $handle );
678 wfProfileOut( __METHOD__ );
679 return $ret;
680 }
681 }
682
683 ?>