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
25 * Simple generic object store
27 * interface is intended to be more or less compatible with
28 * the PHP memcached client.
30 * backends for local hash array and SQL table included:
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
40 function __construct() {
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 */
56 function set($key, $value, $exptime=0) {
61 function delete($key, $time=0) {
66 function lock($key, $timeout = 0) {
71 function unlock($key) {
76 /* *** Emulated functions *** */
77 /* Better performance can likely be got with custom written versions */
78 function get_multi($keys) {
80 foreach($keys as $key)
81 $out[$key] = $this->get($key);
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);
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) ) {
116 $value = intval($value);
117 if($value < 0) $value = 0;
120 if( ($n = $this->get($key)) !== false ) {
122 $this->set($key, $n); // exptime?
128 function decr($key, $value=1) {
129 if ( !$this->lock($key) ) {
132 $value = intval($value);
133 if($value < 0) $value = 0;
136 if( ($n = $this->get($key)) !== false ) {
139 $this->set($key, $m); // exptime?
145 function _debug($text) {
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;
164 * Functional versions!
167 class HashBagOStuff
extends BagOStuff
{
169 This is a test of the interface, mainly. It stores
170 things in an associative array, which is not going to
171 persist between program runs.
175 function HashBagOStuff() {
176 $this->bag
= array();
179 function _expire($key) {
180 $et = $this->bag
[$key][1];
181 if(($et == 0) ||
($et > time()))
188 if(!$this->bag
[$key])
190 if($this->_expire($key))
192 return $this->bag
[$key][0];
195 function set($key,$value,$exptime=0) {
196 $this->bag
[$key] = array( $value, BagOStuff
::convertExpiry( $exptime ) );
199 function delete($key,$time=0) {
200 if(!$this->bag
[$key])
202 unset($this->bag
[$key]);
208 CREATE TABLE objectcache (
209 keyname char(255) binary not null default '',
212 unique key (keyname),
221 abstract class SqlBagOStuff
extends BagOStuff
{
223 var $lastexpireall = 0;
225 function SqlBagOStuff($tablename = 'objectcache') {
226 $this->table
= $tablename;
230 /* expire old entries if any */
231 $this->garbageCollect();
233 $res = $this->_query(
234 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
236 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
239 if($row=$this->_fetchobject($res)) {
240 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
241 if ( $row->exptime
!= $this->_maxdatetime() &&
242 wfTimestamp( TS_UNIX
, $row->exptime
) < time() )
244 $this->_debug("get: key has expired, deleting");
248 return $this->_unserialize($this->_blobdecode($row->value
));
250 $this->_debug('get: no matching rows');
255 function set($key,$value,$exptime=0) {
256 $exptime = intval($exptime);
257 if($exptime < 0) $exptime = 0;
259 $exp = $this->_maxdatetime();
261 if($exptime < 3.16e8
) # ~10 years
263 $exp = $this->_fromunixtime($exptime);
265 $this->delete( $key );
266 $this->_doinsert($this->getTableName(), array(
268 'value' => $this->_blobencode($this->_serialize($value)),
274 function delete($key,$time=0) {
276 "DELETE FROM $0 WHERE keyname='$1'", $key );
280 function getTableName() {
284 function _query($sql) {
285 $reps = func_get_args();
286 $reps[0] = $this->getTableName();
288 for($i=0;$i<count($reps);$i++
) {
291 $i > 0 ?
$this->_strencode($reps[$i]) : $reps[$i],
294 $res = $this->_doquery($sql);
296 $this->_debug('query failed: ' . $this->_dberror($res));
301 function _strencode($str) {
302 /* Protect strings in SQL */
303 return str_replace( "'", "''", $str );
305 function _blobencode($str) {
308 function _blobdecode($str) {
312 abstract function _doinsert($table, $vals);
313 abstract function _doquery($sql);
315 function _freeresult($result) {
320 function _dberror($result) {
322 return 'unknown error';
325 abstract function _maxdatetime();
326 abstract function _fromunixtime($ts);
328 function garbageCollect() {
329 /* Ignore 99% of requests */
330 if ( !mt_rand( 0, 100 ) ) {
332 /* Avoid repeating the delete within a few seconds */
333 if ( $nowtime > ($this->lastexpireall +
1) ) {
334 $this->lastexpireall
= $nowtime;
340 function expireall() {
341 /* Remove any items that have expired */
342 $now = $this->_fromunixtime( time() );
343 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
346 function deleteall() {
347 /* Clear *all* items from cache table */
348 $this->_query( "DELETE FROM $0" );
352 * Serialize an object and, if possible, compress the representation.
353 * On typical message and page data, this can provide a 3X decrease
354 * in storage requirements.
359 function _serialize( &$data ) {
360 $serial = serialize( $data );
361 if( function_exists( 'gzdeflate' ) ) {
362 return gzdeflate( $serial );
369 * Unserialize and, if necessary, decompress an object.
370 * @param string $serial
373 function _unserialize( $serial ) {
374 if( function_exists( 'gzinflate' ) ) {
375 $decomp = @gzinflate
( $serial );
376 if( false !== $decomp ) {
380 $ret = unserialize( $serial );
388 class MediaWikiBagOStuff
extends SqlBagOStuff
{
389 var $tableInitialised = false;
391 function _doquery($sql) {
392 $dbw = wfGetDB( DB_MASTER
);
393 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
395 function _doinsert($t, $v) {
396 $dbw = wfGetDB( DB_MASTER
);
397 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
400 function _fetchobject($result) {
401 $dbw = wfGetDB( DB_MASTER
);
402 return $dbw->fetchObject($result);
404 function _freeresult($result) {
405 $dbw = wfGetDB( DB_MASTER
);
406 return $dbw->freeResult($result);
408 function _dberror($result) {
409 $dbw = wfGetDB( DB_MASTER
);
410 return $dbw->lastError();
412 function _maxdatetime() {
413 if ( time() > 0x7fffffff ) {
414 return $this->_fromunixtime( 1<<62 );
416 return $this->_fromunixtime( 0x7fffffff );
419 function _fromunixtime($ts) {
420 $dbw = wfGetDB(DB_MASTER
);
421 return $dbw->timestamp($ts);
423 function _strencode($s) {
424 $dbw = wfGetDB( DB_MASTER
);
425 return $dbw->strencode($s);
427 function _blobencode($s) {
428 $dbw = wfGetDB( DB_MASTER
);
429 return $dbw->encodeBlob($s);
431 function _blobdecode($s) {
432 $dbw = wfGetDB( DB_MASTER
);
433 return $dbw->decodeBlob($s);
435 function getTableName() {
436 if ( !$this->tableInitialised
) {
437 $dbw = wfGetDB( DB_MASTER
);
438 /* This is actually a hack, we should be able
439 to use Language classes here... or not */
441 throw new MWException("Could not connect to database");
442 $this->table
= $dbw->tableName( $this->table
);
443 $this->tableInitialised
= true;
450 * This is a wrapper for Turck MMCache's shared memory functions.
452 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
453 * to use a weird custom serializer that randomly segfaults. So we wrap calls
454 * with serialize()/unserialize().
456 * The thing I noticed about the Turck serialized data was that unlike ordinary
457 * serialize(), it contained the names of methods, and judging by the amount of
458 * binary data, perhaps even the bytecode of the methods themselves. It may be
459 * that Turck's serializer is faster, so a possible future extension would be
460 * to use it for arrays but not for objects.
463 class TurckBagOStuff
extends BagOStuff
{
465 $val = mmcache_get( $key );
466 if ( is_string( $val ) ) {
467 $val = unserialize( $val );
472 function set($key, $value, $exptime=0) {
473 mmcache_put( $key, serialize( $value ), $exptime );
477 function delete($key, $time=0) {
482 function lock($key, $waitTimeout = 0 ) {
483 mmcache_lock( $key );
487 function unlock($key) {
488 mmcache_unlock( $key );
494 * This is a wrapper for APC's shared memory functions
497 class APCBagOStuff
extends BagOStuff
{
499 $val = apc_fetch($key);
500 if ( is_string( $val ) ) {
501 $val = unserialize( $val );
506 function set($key, $value, $exptime=0) {
507 apc_store($key, serialize($value), $exptime);
511 function delete($key, $time=0) {
519 * This is a wrapper for eAccelerator's shared memory functions.
521 * This is basically identical to the Turck MMCache version,
522 * mostly because eAccelerator is based on Turck MMCache.
525 class eAccelBagOStuff
extends BagOStuff
{
527 $val = eaccelerator_get( $key );
528 if ( is_string( $val ) ) {
529 $val = unserialize( $val );
534 function set($key, $value, $exptime=0) {
535 eaccelerator_put( $key, serialize( $value ), $exptime );
539 function delete($key, $time=0) {
540 eaccelerator_rm( $key );
544 function lock($key, $waitTimeout = 0 ) {
545 eaccelerator_lock( $key );
549 function unlock($key) {
550 eaccelerator_unlock( $key );
558 class DBABagOStuff
extends BagOStuff
{
559 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
561 function __construct( $handler = 'db3', $dir = false ) {
562 if ( $dir === false ) {
563 global $wgTmpDirectory;
564 $dir = $wgTmpDirectory;
566 $this->mFile
= "$dir/mw-cache-" . wfWikiID();
567 $this->mFile
.= '.db';
568 $this->mHandler
= $handler;
572 * Encode value and expiry for storage
574 function encode( $value, $expiry ) {
575 # Convert to absolute time
576 $expiry = BagOStuff
::convertExpiry( $expiry );
577 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
581 * @return list containing value first and expiry second
583 function decode( $blob ) {
584 if ( !is_string( $blob ) ) {
585 return array( null, 0 );
588 unserialize( substr( $blob, 11 ) ),
589 intval( substr( $blob, 0, 10 ) )
594 function getReader() {
595 if ( file_exists( $this->mFile
) ) {
596 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
598 $handle = $this->getWriter();
601 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
606 function getWriter() {
607 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
609 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
614 function get( $key ) {
615 wfProfileIn( __METHOD__
);
616 wfDebug( __METHOD__
."($key)\n" );
617 $handle = $this->getReader();
621 $val = dba_fetch( $key, $handle );
622 list( $val, $expiry ) = $this->decode( $val );
623 # Must close ASAP because locks are held
624 dba_close( $handle );
626 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
627 # Key is expired, delete it
628 $handle = $this->getWriter();
629 dba_delete( $key, $handle );
630 dba_close( $handle );
631 wfDebug( __METHOD__
.": $key expired\n" );
634 wfProfileOut( __METHOD__
);
638 function set( $key, $value, $exptime=0 ) {
639 wfProfileIn( __METHOD__
);
640 wfDebug( __METHOD__
."($key)\n" );
641 $blob = $this->encode( $value, $exptime );
642 $handle = $this->getWriter();
646 $ret = dba_replace( $key, $blob, $handle );
647 dba_close( $handle );
648 wfProfileOut( __METHOD__
);
652 function delete( $key, $time = 0 ) {
653 wfProfileIn( __METHOD__
);
654 $handle = $this->getWriter();
658 $ret = dba_delete( $key, $handle );
659 dba_close( $handle );
660 wfProfileOut( __METHOD__
);
664 function add( $key, $value, $exptime = 0 ) {
665 wfProfileIn( __METHOD__
);
666 $blob = $this->encode( $value, $exptime );
667 $handle = $this->getWriter();
671 $ret = dba_insert( $key, $blob, $handle );
672 # Insert failed, check to see if it failed due to an expired key
674 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
675 if ( $expiry < time() ) {
676 # Yes expired, delete and try again
677 dba_delete( $key, $handle );
678 $ret = dba_insert( $key, $blob, $handle );
679 # This time if it failed then it will be handled by the caller like any other race
683 dba_close( $handle );
684 wfProfileOut( __METHOD__
);