Update.
[mediawiki.git] / includes / BagOStuff.php
blobc720807d38630e4da4c89b9884c68f9ea8be190d
1 <?php
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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 /**
22 * @package MediaWiki
25 /**
26 * Simple generic object store
28 * interface is intended to be more or less compatible with
29 * the PHP memcached client.
31 * backends for local hash array and SQL table included:
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
35 * @package MediaWiki
37 class BagOStuff {
38 var $debugmode;
40 function BagOStuff() {
41 $this->set_debug( false );
44 function set_debug($bool) {
45 $this->debugmode = $bool;
48 /* *** THE GUTS OF THE OPERATION *** */
49 /* Override these with functional things in subclasses */
51 function get($key) {
52 /* stub */
53 return false;
56 function set($key, $value, $exptime=0) {
57 /* stub */
58 return false;
61 function delete($key, $time=0) {
62 /* stub */
63 return false;
66 function lock($key, $timeout = 0) {
67 /* stub */
68 return true;
71 function unlock($key) {
72 /* stub */
73 return true;
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;
85 function set_multi($hash, $exptime=0) {
86 foreach($hash as $key => $value)
87 $this->set($key, $value, $exptime);
90 function add($key, $value, $exptime=0) {
91 if( $this->get($key) == false ) {
92 $this->set($key, $value, $exptime);
93 return true;
97 function add_multi($hash, $exptime=0) {
98 foreach($hash as $key => $value)
99 $this->add($key, $value, $exptime);
102 function delete_multi($keys, $time=0) {
103 foreach($keys as $key)
104 $this->delete($key, $time);
107 function replace($key, $value, $exptime=0) {
108 if( $this->get($key) !== false )
109 $this->set($key, $value, $exptime);
112 function incr($key, $value=1) {
113 if ( !$this->lock($key) ) {
114 return false;
116 $value = intval($value);
117 if($value < 0) $value = 0;
119 $n = false;
120 if( ($n = $this->get($key)) !== false ) {
121 $n += $value;
122 $this->set($key, $n); // exptime?
124 $this->unlock($key);
125 return $n;
128 function decr($key, $value=1) {
129 if ( !$this->lock($key) ) {
130 return false;
132 $value = intval($value);
133 if($value < 0) $value = 0;
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?
141 $this->unlock($key);
142 return $m;
145 function _debug($text) {
146 if($this->debugmode)
147 wfDebug("BagOStuff debug: $text\n");
151 * Convert an optionally relative time to an absolute time
153 static function convertExpiry( $exptime ) {
154 if(($exptime != 0) && ($exptime < 3600*24*30)) {
155 return time() + $exptime;
156 } else {
157 return $exptime;
164 * Functional versions!
165 * @todo document
166 * @package MediaWiki
168 class HashBagOStuff extends BagOStuff {
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.
174 var $bag;
176 function HashBagOStuff() {
177 $this->bag = array();
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;
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];
196 function set($key,$value,$exptime=0) {
197 $this->bag[$key] = array( $value, BagOStuff::convertExpiry( $exptime ) );
200 function delete($key,$time=0) {
201 if(!$this->bag[$key])
202 return false;
203 unset($this->bag[$key]);
204 return true;
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)
219 * @todo document
220 * @abstract
221 * @package MediaWiki
223 abstract class SqlBagOStuff extends BagOStuff {
224 var $table;
225 var $lastexpireall = 0;
227 function SqlBagOStuff($tablename = 'objectcache') {
228 $this->table = $tablename;
231 function get($key) {
232 /* expire old entries if any */
233 $this->garbageCollect();
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;
241 if($row=$this->_fetchobject($res)) {
242 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
243 if ( $row->exptime != $this->_maxdatetime() &&
244 wfTimestamp( TS_UNIX, $row->exptime ) < time() )
246 $this->_debug("get: key has expired, deleting");
247 $this->delete($key);
248 return false;
250 return $this->_unserialize($this->_blobdecode($row->value));
251 } else {
252 $this->_debug('get: no matching rows');
254 return false;
257 function set($key,$value,$exptime=0) {
258 $exptime = intval($exptime);
259 if($exptime < 0) $exptime = 0;
260 if($exptime == 0) {
261 $exp = $this->_maxdatetime();
262 } else {
263 if($exptime < 3.16e8) # ~10 years
264 $exptime += time();
265 $exp = $this->_fromunixtime($exptime);
267 $this->delete( $key );
268 $this->_doinsert($this->getTableName(), array(
269 'keyname' => $key,
270 'value' => $this->_blobencode($this->_serialize($value)),
271 'exptime' => $exp
273 return true; /* ? */
276 function delete($key,$time=0) {
277 $this->_query(
278 "DELETE FROM $0 WHERE keyname='$1'", $key );
279 return true; /* ? */
282 function getTableName() {
283 return $this->table;
286 function _query($sql) {
287 $reps = func_get_args();
288 $reps[0] = $this->getTableName();
289 // ewwww
290 for($i=0;$i<count($reps);$i++) {
291 $sql = str_replace(
292 '$' . $i,
293 $i > 0 ? $this->_strencode($reps[$i]) : $reps[$i],
294 $sql);
296 $res = $this->_doquery($sql);
297 if($res == false) {
298 $this->_debug('query failed: ' . $this->_dberror($res));
300 return $res;
303 function _strencode($str) {
304 /* Protect strings in SQL */
305 return str_replace( "'", "''", $str );
307 function _blobencode($str) {
308 return $str;
310 function _blobdecode($str) {
311 return $str;
314 abstract function _doinsert($table, $vals);
315 abstract function _doquery($sql);
317 function _freeresult($result) {
318 /* stub */
319 return false;
322 function _dberror($result) {
323 /* stub */
324 return 'unknown error';
327 abstract function _maxdatetime();
328 abstract function _fromunixtime($ts);
330 function garbageCollect() {
331 /* Ignore 99% of requests */
332 if ( !mt_rand( 0, 100 ) ) {
333 $nowtime = time();
334 /* Avoid repeating the delete within a few seconds */
335 if ( $nowtime > ($this->lastexpireall + 1) ) {
336 $this->lastexpireall = $nowtime;
337 $this->expireall();
342 function expireall() {
343 /* Remove any items that have expired */
344 $now = $this->_fromunixtime( time() );
345 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
348 function deleteall() {
349 /* Clear *all* items from cache table */
350 $this->_query( "DELETE FROM $0" );
354 * Serialize an object and, if possible, compress the representation.
355 * On typical message and page data, this can provide a 3X decrease
356 * in storage requirements.
358 * @param mixed $data
359 * @return string
361 function _serialize( &$data ) {
362 $serial = serialize( $data );
363 if( function_exists( 'gzdeflate' ) ) {
364 return gzdeflate( $serial );
365 } else {
366 return $serial;
371 * Unserialize and, if necessary, decompress an object.
372 * @param string $serial
373 * @return mixed
375 function _unserialize( $serial ) {
376 if( function_exists( 'gzinflate' ) ) {
377 $decomp = @gzinflate( $serial );
378 if( false !== $decomp ) {
379 $serial = $decomp;
382 $ret = unserialize( $serial );
383 return $ret;
388 * @todo document
389 * @package MediaWiki
391 class MediaWikiBagOStuff extends SqlBagOStuff {
392 var $tableInitialised = false;
394 function _doquery($sql) {
395 $dbw =& wfGetDB( DB_MASTER );
396 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
398 function _doinsert($t, $v) {
399 $dbw =& wfGetDB( DB_MASTER );
400 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
401 array( 'IGNORE' ) );
403 function _fetchobject($result) {
404 $dbw =& wfGetDB( DB_MASTER );
405 return $dbw->fetchObject($result);
407 function _freeresult($result) {
408 $dbw =& wfGetDB( DB_MASTER );
409 return $dbw->freeResult($result);
411 function _dberror($result) {
412 $dbw =& wfGetDB( DB_MASTER );
413 return $dbw->lastError();
415 function _maxdatetime() {
416 $dbw =& wfGetDB(DB_MASTER);
417 if ( time() > 0x7fffffff ) {
418 return $this->_fromunixtime( 1<<62 );
419 } else {
420 return $this->_fromunixtime( 0x7fffffff );
423 function _fromunixtime($ts) {
424 $dbw =& wfGetDB(DB_MASTER);
425 return $dbw->timestamp($ts);
427 function _strencode($s) {
428 $dbw =& wfGetDB( DB_MASTER );
429 return $dbw->strencode($s);
431 function _blobencode($s) {
432 $dbw =& wfGetDB( DB_MASTER );
433 return $dbw->encodeBlob($s);
435 function _blobdecode($s) {
436 $dbw =& wfGetDB( DB_MASTER );
437 return $dbw->decodeBlob($s);
439 function getTableName() {
440 if ( !$this->tableInitialised ) {
441 $dbw =& wfGetDB( DB_MASTER );
442 /* This is actually a hack, we should be able
443 to use Language classes here... or not */
444 if (!$dbw)
445 throw new MWException("Could not connect to database");
446 $this->table = $dbw->tableName( $this->table );
447 $this->tableInitialised = true;
449 return $this->table;
454 * This is a wrapper for Turck MMCache's shared memory functions.
456 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
457 * to use a weird custom serializer that randomly segfaults. So we wrap calls
458 * with serialize()/unserialize().
460 * The thing I noticed about the Turck serialized data was that unlike ordinary
461 * serialize(), it contained the names of methods, and judging by the amount of
462 * binary data, perhaps even the bytecode of the methods themselves. It may be
463 * that Turck's serializer is faster, so a possible future extension would be
464 * to use it for arrays but not for objects.
466 * @package MediaWiki
468 class TurckBagOStuff extends BagOStuff {
469 function get($key) {
470 $val = mmcache_get( $key );
471 if ( is_string( $val ) ) {
472 $val = unserialize( $val );
474 return $val;
477 function set($key, $value, $exptime=0) {
478 mmcache_put( $key, serialize( $value ), $exptime );
479 return true;
482 function delete($key, $time=0) {
483 mmcache_rm( $key );
484 return true;
487 function lock($key, $waitTimeout = 0 ) {
488 mmcache_lock( $key );
489 return true;
492 function unlock($key) {
493 mmcache_unlock( $key );
494 return true;
499 * This is a wrapper for APC's shared memory functions
501 * @package MediaWiki
504 class APCBagOStuff extends BagOStuff {
505 function get($key) {
506 $val = apc_fetch($key);
507 if ( is_string( $val ) ) {
508 $val = unserialize( $val );
510 return $val;
513 function set($key, $value, $exptime=0) {
514 apc_store($key, serialize($value), $exptime);
515 return true;
518 function delete($key, $time=0) {
519 apc_delete($key);
520 return true;
526 * This is a wrapper for eAccelerator's shared memory functions.
528 * This is basically identical to the Turck MMCache version,
529 * mostly because eAccelerator is based on Turck MMCache.
531 * @package MediaWiki
533 class eAccelBagOStuff extends BagOStuff {
534 function get($key) {
535 $val = eaccelerator_get( $key );
536 if ( is_string( $val ) ) {
537 $val = unserialize( $val );
539 return $val;
542 function set($key, $value, $exptime=0) {
543 eaccelerator_put( $key, serialize( $value ), $exptime );
544 return true;
547 function delete($key, $time=0) {
548 eaccelerator_rm( $key );
549 return true;
552 function lock($key, $waitTimeout = 0 ) {
553 eaccelerator_lock( $key );
554 return true;
557 function unlock($key) {
558 eaccelerator_unlock( $key );
559 return true;
563 class DBABagOStuff extends BagOStuff {
564 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
566 function __construct( $handler = 'db3', $dir = false ) {
567 if ( $dir === false ) {
568 global $wgTmpDirectory;
569 $dir = $wgTmpDirectory;
571 $this->mFile = "$dir/mw-cache-" . wfWikiID();
572 $this->mFile .= '.db';
573 $this->mHandler = $handler;
577 * Encode value and expiry for storage
579 function encode( $value, $expiry ) {
580 # Convert to absolute time
581 $expiry = BagOStuff::convertExpiry( $expiry );
582 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
586 * @return list containing value first and expiry second
588 function decode( $blob ) {
589 if ( !is_string( $blob ) ) {
590 return array( null, 0 );
591 } else {
592 return array(
593 unserialize( substr( $blob, 11 ) ),
594 intval( substr( $blob, 0, 10 ) )
599 function getReader() {
600 if ( file_exists( $this->mFile ) ) {
601 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
602 } else {
603 $handle = $this->getWriter();
605 if ( !$handle ) {
606 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
608 return $handle;
611 function getWriter() {
612 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
613 if ( !$handle ) {
614 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
616 return $handle;
619 function get( $key ) {
620 wfProfileIn( __METHOD__ );
621 wfDebug( __METHOD__."($key)\n" );
622 $handle = $this->getReader();
623 if ( !$handle ) {
624 return null;
626 $val = dba_fetch( $key, $handle );
627 list( $val, $expiry ) = $this->decode( $val );
628 # Must close ASAP because locks are held
629 dba_close( $handle );
631 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
632 # Key is expired, delete it
633 $handle = $this->getWriter();
634 dba_delete( $key, $handle );
635 dba_close( $handle );
636 wfDebug( __METHOD__.": $key expired\n" );
637 $val = null;
639 wfProfileOut( __METHOD__ );
640 return $val;
643 function set( $key, $value, $exptime=0 ) {
644 wfProfileIn( __METHOD__ );
645 wfDebug( __METHOD__."($key)\n" );
646 $blob = $this->encode( $value, $exptime );
647 $handle = $this->getWriter();
648 if ( !$handle ) {
649 return false;
651 $ret = dba_replace( $key, $blob, $handle );
652 dba_close( $handle );
653 wfProfileOut( __METHOD__ );
654 return $ret;
657 function delete( $key, $time = 0 ) {
658 wfProfileIn( __METHOD__ );
659 $handle = $this->getWriter();
660 if ( !$handle ) {
661 return false;
663 $ret = dba_delete( $key, $handle );
664 dba_close( $handle );
665 wfProfileOut( __METHOD__ );
666 return $ret;
669 function add( $key, $value, $exptime = 0 ) {
670 wfProfileIn( __METHOD__ );
671 $blob = $this->encode( $value, $exptime );
672 $handle = $this->getWriter();
673 if ( !$handle ) {
674 return false;
676 $ret = dba_insert( $key, $blob, $handle );
677 # Insert failed, check to see if it failed due to an expired key
678 if ( !$ret ) {
679 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
680 if ( $expiry < time() ) {
681 # Yes expired, delete and try again
682 dba_delete( $key, $handle );
683 $ret = dba_insert( $key, $blob, $handle );
684 # This time if it failed then it will be handled by the caller like any other race
688 dba_close( $handle );
689 wfProfileOut( __METHOD__ );
690 return $ret;