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) {
81 /* *** Emulated functions *** */
82 /* Better performance can likely be got with custom written versions */
83 function get_multi($keys) {
85 foreach($keys as $key)
86 $out[$key] = $this->get($key);
90 function set_multi($hash, $exptime=0) {
91 foreach($hash as $key => $value)
92 $this->set($key, $value, $exptime);
95 function add($key, $value, $exptime=0) {
96 if( $this->get($key) == false ) {
97 $this->set($key, $value, $exptime);
102 function add_multi($hash, $exptime=0) {
103 foreach($hash as $key => $value)
104 $this->add($key, $value, $exptime);
107 function delete_multi($keys, $time=0) {
108 foreach($keys as $key)
109 $this->delete($key, $time);
112 function replace($key, $value, $exptime=0) {
113 if( $this->get($key) !== false )
114 $this->set($key, $value, $exptime);
117 function incr($key, $value=1) {
118 if ( !$this->lock($key) ) {
121 $value = intval($value);
122 if($value < 0) $value = 0;
125 if( ($n = $this->get($key)) !== false ) {
127 $this->set($key, $n); // exptime?
133 function decr($key, $value=1) {
134 if ( !$this->lock($key) ) {
137 $value = intval($value);
138 if($value < 0) $value = 0;
141 if( ($n = $this->get($key)) !== false ) {
144 $this->set($key, $m); // exptime?
150 function _debug($text) {
152 wfDebug("BagOStuff debug: $text\n");
156 * Convert an optionally relative time to an absolute time
158 static function convertExpiry( $exptime ) {
159 if(($exptime != 0) && ($exptime < 3600*24*30)) {
160 return time() +
$exptime;
169 * Functional versions!
172 class HashBagOStuff
extends BagOStuff
{
174 This is a test of the interface, mainly. It stores
175 things in an associative array, which is not going to
176 persist between program runs.
180 function __construct() {
181 $this->bag
= array();
184 function _expire($key) {
185 $et = $this->bag
[$key][1];
186 if(($et == 0) ||
($et > time()))
193 if(!$this->bag
[$key])
195 if($this->_expire($key))
197 return $this->bag
[$key][0];
200 function set($key,$value,$exptime=0) {
201 $this->bag
[$key] = array( $value, BagOStuff
::convertExpiry( $exptime ) );
204 function delete($key,$time=0) {
205 if(!$this->bag
[$key])
207 unset($this->bag
[$key]);
212 return array_keys( $this->bag
);
217 CREATE TABLE objectcache (
218 keyname char(255) binary not null default '',
221 unique key (keyname),
230 abstract class SqlBagOStuff
extends BagOStuff
{
232 var $lastexpireall = 0;
234 function __construct($tablename = 'objectcache') {
235 $this->table
= $tablename;
239 /* expire old entries if any */
240 $this->garbageCollect();
242 $res = $this->_query(
243 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
245 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
248 if($row=$this->_fetchobject($res)) {
249 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
250 if ( $row->exptime
!= $this->_maxdatetime() &&
251 wfTimestamp( TS_UNIX
, $row->exptime
) < time() )
253 $this->_debug("get: key has expired, deleting");
257 return $this->_unserialize($this->_blobdecode($row->value
));
259 $this->_debug('get: no matching rows');
264 function set($key,$value,$exptime=0) {
265 if ( wfReadOnly() ) {
268 $exptime = intval($exptime);
269 if($exptime < 0) $exptime = 0;
271 $exp = $this->_maxdatetime();
273 if($exptime < 3.16e8
) # ~10 years
275 $exp = $this->_fromunixtime($exptime);
277 $this->delete( $key );
278 $this->_doinsert($this->getTableName(), array(
280 'value' => $this->_blobencode($this->_serialize($value)),
286 function delete($key,$time=0) {
287 if ( wfReadOnly() ) {
291 "DELETE FROM $0 WHERE keyname='$1'", $key );
296 $res = $this->_query( "SELECT keyname FROM $0" );
298 $this->_debug("keys: ** error: " . $this->_dberror($res) . " **");
302 while( $row = $this->_fetchobject($res) ) {
303 $result[] = $row->keyname
;
308 function getTableName() {
312 function _query($sql) {
313 $reps = func_get_args();
314 $reps[0] = $this->getTableName();
316 for($i=0;$i<count($reps);$i++
) {
319 $i > 0 ?
$this->_strencode($reps[$i]) : $reps[$i],
322 $res = $this->_doquery($sql);
324 $this->_debug('query failed: ' . $this->_dberror($res));
329 function _strencode($str) {
330 /* Protect strings in SQL */
331 return str_replace( "'", "''", $str );
333 function _blobencode($str) {
336 function _blobdecode($str) {
340 abstract function _doinsert($table, $vals);
341 abstract function _doquery($sql);
343 function _freeresult($result) {
348 function _dberror($result) {
350 return 'unknown error';
353 abstract function _maxdatetime();
354 abstract function _fromunixtime($ts);
356 function garbageCollect() {
357 /* Ignore 99% of requests */
358 if ( !mt_rand( 0, 100 ) ) {
360 /* Avoid repeating the delete within a few seconds */
361 if ( $nowtime > ($this->lastexpireall +
1) ) {
362 $this->lastexpireall
= $nowtime;
368 function expireall() {
369 /* Remove any items that have expired */
370 if ( wfReadOnly() ) {
373 $now = $this->_fromunixtime( time() );
374 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
377 function deleteall() {
378 /* Clear *all* items from cache table */
379 if ( wfReadOnly() ) {
382 $this->_query( "DELETE FROM $0" );
386 * Serialize an object and, if possible, compress the representation.
387 * On typical message and page data, this can provide a 3X decrease
388 * in storage requirements.
393 function _serialize( &$data ) {
394 $serial = serialize( $data );
395 if( function_exists( 'gzdeflate' ) ) {
396 return gzdeflate( $serial );
403 * Unserialize and, if necessary, decompress an object.
404 * @param string $serial
407 function _unserialize( $serial ) {
408 if( function_exists( 'gzinflate' ) ) {
409 $decomp = @gzinflate
( $serial );
410 if( false !== $decomp ) {
414 $ret = unserialize( $serial );
422 class MediaWikiBagOStuff
extends SqlBagOStuff
{
423 var $tableInitialised = false;
425 function _doquery($sql) {
426 $dbw = wfGetDB( DB_MASTER
);
427 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
429 function _doinsert($t, $v) {
430 $dbw = wfGetDB( DB_MASTER
);
431 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
434 function _fetchobject($result) {
435 $dbw = wfGetDB( DB_MASTER
);
436 return $dbw->fetchObject($result);
438 function _freeresult($result) {
439 $dbw = wfGetDB( DB_MASTER
);
440 return $dbw->freeResult($result);
442 function _dberror($result) {
443 $dbw = wfGetDB( DB_MASTER
);
444 return $dbw->lastError();
446 function _maxdatetime() {
447 if ( time() > 0x7fffffff ) {
448 return $this->_fromunixtime( 1<<62 );
450 return $this->_fromunixtime( 0x7fffffff );
453 function _fromunixtime($ts) {
454 $dbw = wfGetDB(DB_MASTER
);
455 return $dbw->timestamp($ts);
457 function _strencode($s) {
458 $dbw = wfGetDB( DB_MASTER
);
459 return $dbw->strencode($s);
461 function _blobencode($s) {
462 $dbw = wfGetDB( DB_MASTER
);
463 return $dbw->encodeBlob($s);
465 function _blobdecode($s) {
466 $dbw = wfGetDB( DB_MASTER
);
467 return $dbw->decodeBlob($s);
469 function getTableName() {
470 if ( !$this->tableInitialised
) {
471 $dbw = wfGetDB( DB_MASTER
);
472 /* This is actually a hack, we should be able
473 to use Language classes here... or not */
475 throw new MWException("Could not connect to database");
476 $this->table
= $dbw->tableName( $this->table
);
477 $this->tableInitialised
= true;
484 * This is a wrapper for Turck MMCache's shared memory functions.
486 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
487 * to use a weird custom serializer that randomly segfaults. So we wrap calls
488 * with serialize()/unserialize().
490 * The thing I noticed about the Turck serialized data was that unlike ordinary
491 * serialize(), it contained the names of methods, and judging by the amount of
492 * binary data, perhaps even the bytecode of the methods themselves. It may be
493 * that Turck's serializer is faster, so a possible future extension would be
494 * to use it for arrays but not for objects.
497 class TurckBagOStuff
extends BagOStuff
{
499 $val = mmcache_get( $key );
500 if ( is_string( $val ) ) {
501 $val = unserialize( $val );
506 function set($key, $value, $exptime=0) {
507 mmcache_put( $key, serialize( $value ), $exptime );
511 function delete($key, $time=0) {
516 function lock($key, $waitTimeout = 0 ) {
517 mmcache_lock( $key );
521 function unlock($key) {
522 mmcache_unlock( $key );
528 * This is a wrapper for APC's shared memory functions
531 class APCBagOStuff
extends BagOStuff
{
533 $val = apc_fetch($key);
534 if ( is_string( $val ) ) {
535 $val = unserialize( $val );
540 function set($key, $value, $exptime=0) {
541 apc_store($key, serialize($value), $exptime);
545 function delete($key, $time=0) {
553 * This is a wrapper for eAccelerator's shared memory functions.
555 * This is basically identical to the Turck MMCache version,
556 * mostly because eAccelerator is based on Turck MMCache.
559 class eAccelBagOStuff
extends BagOStuff
{
561 $val = eaccelerator_get( $key );
562 if ( is_string( $val ) ) {
563 $val = unserialize( $val );
568 function set($key, $value, $exptime=0) {
569 eaccelerator_put( $key, serialize( $value ), $exptime );
573 function delete($key, $time=0) {
574 eaccelerator_rm( $key );
578 function lock($key, $waitTimeout = 0 ) {
579 eaccelerator_lock( $key );
583 function unlock($key) {
584 eaccelerator_unlock( $key );
590 * Wrapper for XCache object caching functions; identical interface
593 class XCacheBagOStuff
extends BagOStuff
{
596 * Get a value from the XCache object cache
598 * @param string $key Cache key
601 public function get( $key ) {
602 $val = xcache_get( $key );
603 if( is_string( $val ) )
604 $val = unserialize( $val );
609 * Store a value in the XCache object cache
611 * @param string $key Cache key
612 * @param mixed $value Object to store
613 * @param int $expire Expiration time
616 public function set( $key, $value, $expire = 0 ) {
617 xcache_set( $key, serialize( $value ), $expire );
622 * Remove a value from the XCache object cache
624 * @param string $key Cache key
625 * @param int $time Not used in this implementation
628 public function delete( $key, $time = 0 ) {
629 xcache_unset( $key );
638 class DBABagOStuff
extends BagOStuff
{
639 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
641 function __construct( $handler = 'db3', $dir = false ) {
642 if ( $dir === false ) {
643 global $wgTmpDirectory;
644 $dir = $wgTmpDirectory;
646 $this->mFile
= "$dir/mw-cache-" . wfWikiID();
647 $this->mFile
.= '.db';
648 $this->mHandler
= $handler;
652 * Encode value and expiry for storage
654 function encode( $value, $expiry ) {
655 # Convert to absolute time
656 $expiry = BagOStuff
::convertExpiry( $expiry );
657 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
661 * @return list containing value first and expiry second
663 function decode( $blob ) {
664 if ( !is_string( $blob ) ) {
665 return array( null, 0 );
668 unserialize( substr( $blob, 11 ) ),
669 intval( substr( $blob, 0, 10 ) )
674 function getReader() {
675 if ( file_exists( $this->mFile
) ) {
676 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
678 $handle = $this->getWriter();
681 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
686 function getWriter() {
687 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
689 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
694 function get( $key ) {
695 wfProfileIn( __METHOD__
);
696 wfDebug( __METHOD__
."($key)\n" );
697 $handle = $this->getReader();
701 $val = dba_fetch( $key, $handle );
702 list( $val, $expiry ) = $this->decode( $val );
703 # Must close ASAP because locks are held
704 dba_close( $handle );
706 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
707 # Key is expired, delete it
708 $handle = $this->getWriter();
709 dba_delete( $key, $handle );
710 dba_close( $handle );
711 wfDebug( __METHOD__
.": $key expired\n" );
714 wfProfileOut( __METHOD__
);
718 function set( $key, $value, $exptime=0 ) {
719 wfProfileIn( __METHOD__
);
720 wfDebug( __METHOD__
."($key)\n" );
721 $blob = $this->encode( $value, $exptime );
722 $handle = $this->getWriter();
726 $ret = dba_replace( $key, $blob, $handle );
727 dba_close( $handle );
728 wfProfileOut( __METHOD__
);
732 function delete( $key, $time = 0 ) {
733 wfProfileIn( __METHOD__
);
734 wfDebug( __METHOD__
."($key)\n" );
735 $handle = $this->getWriter();
739 $ret = dba_delete( $key, $handle );
740 dba_close( $handle );
741 wfProfileOut( __METHOD__
);
745 function add( $key, $value, $exptime = 0 ) {
746 wfProfileIn( __METHOD__
);
747 $blob = $this->encode( $value, $exptime );
748 $handle = $this->getWriter();
752 $ret = dba_insert( $key, $blob, $handle );
753 # Insert failed, check to see if it failed due to an expired key
755 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
756 if ( $expiry < time() ) {
757 # Yes expired, delete and try again
758 dba_delete( $key, $handle );
759 $ret = dba_insert( $key, $blob, $handle );
760 # This time if it failed then it will be handled by the caller like any other race
764 dba_close( $handle );
765 wfProfileOut( __METHOD__
);
770 $reader = $this->getReader();
771 $k1 = dba_firstkey( $reader );
776 while( $key = dba_nextkey( $reader ) ) {