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 __construct() {
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 __construct($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 if ( wfReadOnly() ) {
259 $exptime = intval($exptime);
260 if($exptime < 0) $exptime = 0;
262 $exp = $this->_maxdatetime();
264 if($exptime < 3.16e8) # ~10 years
266 $exp = $this->_fromunixtime($exptime);
268 $this->delete( $key );
269 $this->_doinsert($this->getTableName(), array(
271 'value' => $this->_blobencode($this->_serialize($value)),
277 function delete($key,$time=0) {
278 if ( wfReadOnly() ) {
282 "DELETE FROM $0 WHERE keyname='$1'", $key );
286 function getTableName() {
290 function _query($sql) {
291 $reps = func_get_args();
292 $reps[0] = $this->getTableName();
294 for($i=0;$i<count($reps);$i++) {
297 $i > 0 ? $this->_strencode($reps[$i]) : $reps[$i],
300 $res = $this->_doquery($sql);
302 $this->_debug('query failed: ' . $this->_dberror($res));
307 function _strencode($str) {
308 /* Protect strings in SQL */
309 return str_replace( "'", "''", $str );
311 function _blobencode($str) {
314 function _blobdecode($str) {
318 abstract function _doinsert($table, $vals);
319 abstract function _doquery($sql);
321 function _freeresult($result) {
326 function _dberror($result) {
328 return 'unknown error';
331 abstract function _maxdatetime();
332 abstract function _fromunixtime($ts);
334 function garbageCollect() {
335 /* Ignore 99% of requests */
336 if ( !mt_rand( 0, 100 ) ) {
338 /* Avoid repeating the delete within a few seconds */
339 if ( $nowtime > ($this->lastexpireall + 1) ) {
340 $this->lastexpireall = $nowtime;
346 function expireall() {
347 /* Remove any items that have expired */
348 if ( wfReadOnly() ) {
351 $now = $this->_fromunixtime( time() );
352 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
355 function deleteall() {
356 /* Clear *all* items from cache table */
357 if ( wfReadOnly() ) {
360 $this->_query( "DELETE FROM $0" );
364 * Serialize an object and, if possible, compress the representation.
365 * On typical message and page data, this can provide a 3X decrease
366 * in storage requirements.
371 function _serialize( &$data ) {
372 $serial = serialize( $data );
373 if( function_exists( 'gzdeflate' ) ) {
374 return gzdeflate( $serial );
381 * Unserialize and, if necessary, decompress an object.
382 * @param string $serial
385 function _unserialize( $serial ) {
386 if( function_exists( 'gzinflate' ) ) {
387 $decomp = @gzinflate( $serial );
388 if( false !== $decomp ) {
392 $ret = unserialize( $serial );
400 class MediaWikiBagOStuff extends SqlBagOStuff {
401 var $tableInitialised = false;
403 function _doquery($sql) {
404 $dbw = wfGetDB( DB_MASTER );
405 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
407 function _doinsert($t, $v) {
408 $dbw = wfGetDB( DB_MASTER );
409 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
412 function _fetchobject($result) {
413 $dbw = wfGetDB( DB_MASTER );
414 return $dbw->fetchObject($result);
416 function _freeresult($result) {
417 $dbw = wfGetDB( DB_MASTER );
418 return $dbw->freeResult($result);
420 function _dberror($result) {
421 $dbw = wfGetDB( DB_MASTER );
422 return $dbw->lastError();
424 function _maxdatetime() {
425 if ( time() > 0x7fffffff ) {
426 return $this->_fromunixtime( 1<<62 );
428 return $this->_fromunixtime( 0x7fffffff );
431 function _fromunixtime($ts) {
432 $dbw = wfGetDB(DB_MASTER);
433 return $dbw->timestamp($ts);
435 function _strencode($s) {
436 $dbw = wfGetDB( DB_MASTER );
437 return $dbw->strencode($s);
439 function _blobencode($s) {
440 $dbw = wfGetDB( DB_MASTER );
441 return $dbw->encodeBlob($s);
443 function _blobdecode($s) {
444 $dbw = wfGetDB( DB_MASTER );
445 return $dbw->decodeBlob($s);
447 function getTableName() {
448 if ( !$this->tableInitialised ) {
449 $dbw = wfGetDB( DB_MASTER );
450 /* This is actually a hack, we should be able
451 to use Language classes here... or not */
453 throw new MWException("Could not connect to database");
454 $this->table = $dbw->tableName( $this->table );
455 $this->tableInitialised = true;
462 * This is a wrapper for Turck MMCache's shared memory functions.
464 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
465 * to use a weird custom serializer that randomly segfaults. So we wrap calls
466 * with serialize()/unserialize().
468 * The thing I noticed about the Turck serialized data was that unlike ordinary
469 * serialize(), it contained the names of methods, and judging by the amount of
470 * binary data, perhaps even the bytecode of the methods themselves. It may be
471 * that Turck's serializer is faster, so a possible future extension would be
472 * to use it for arrays but not for objects.
475 class TurckBagOStuff extends BagOStuff {
477 $val = mmcache_get( $key );
478 if ( is_string( $val ) ) {
479 $val = unserialize( $val );
484 function set($key, $value, $exptime=0) {
485 mmcache_put( $key, serialize( $value ), $exptime );
489 function delete($key, $time=0) {
494 function lock($key, $waitTimeout = 0 ) {
495 mmcache_lock( $key );
499 function unlock($key) {
500 mmcache_unlock( $key );
506 * This is a wrapper for APC's shared memory functions
509 class APCBagOStuff extends BagOStuff {
511 $val = apc_fetch($key);
512 if ( is_string( $val ) ) {
513 $val = unserialize( $val );
518 function set($key, $value, $exptime=0) {
519 apc_store($key, serialize($value), $exptime);
523 function delete($key, $time=0) {
531 * This is a wrapper for eAccelerator's shared memory functions.
533 * This is basically identical to the Turck MMCache version,
534 * mostly because eAccelerator is based on Turck MMCache.
537 class eAccelBagOStuff extends BagOStuff {
539 $val = eaccelerator_get( $key );
540 if ( is_string( $val ) ) {
541 $val = unserialize( $val );
546 function set($key, $value, $exptime=0) {
547 eaccelerator_put( $key, serialize( $value ), $exptime );
551 function delete($key, $time=0) {
552 eaccelerator_rm( $key );
556 function lock($key, $waitTimeout = 0 ) {
557 eaccelerator_lock( $key );
561 function unlock($key) {
562 eaccelerator_unlock( $key );
568 * Wrapper for XCache object caching functions; identical interface
571 class XCacheBagOStuff extends BagOStuff {
574 * Get a value from the XCache object cache
576 * @param string $key Cache key
579 public function get( $key ) {
580 $val = xcache_get( $key );
581 if( is_string( $val ) )
582 $val = unserialize( $val );
587 * Store a value in the XCache object cache
589 * @param string $key Cache key
590 * @param mixed $value Object to store
591 * @param int $expire Expiration time
594 public function set( $key, $value, $expire = 0 ) {
595 xcache_set( $key, serialize( $value ), $expire );
600 * Remove a value from the XCache object cache
602 * @param string $key Cache key
603 * @param int $time Not used in this implementation
606 public function delete( $key, $time = 0 ) {
607 xcache_unset( $key );
616 class DBABagOStuff extends BagOStuff {
617 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
619 function __construct( $handler = 'db3', $dir = false ) {
620 if ( $dir === false ) {
621 global $wgTmpDirectory;
622 $dir = $wgTmpDirectory;
624 $this->mFile = "$dir/mw-cache-" . wfWikiID();
625 $this->mFile .= '.db';
626 $this->mHandler = $handler;
630 * Encode value and expiry for storage
632 function encode( $value, $expiry ) {
633 # Convert to absolute time
634 $expiry = BagOStuff::convertExpiry( $expiry );
635 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
639 * @return list containing value first and expiry second
641 function decode( $blob ) {
642 if ( !is_string( $blob ) ) {
643 return array( null, 0 );
646 unserialize( substr( $blob, 11 ) ),
647 intval( substr( $blob, 0, 10 ) )
652 function getReader() {
653 if ( file_exists( $this->mFile ) ) {
654 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
656 $handle = $this->getWriter();
659 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
664 function getWriter() {
665 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
667 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
672 function get( $key ) {
673 wfProfileIn( __METHOD__ );
674 wfDebug( __METHOD__."($key)\n" );
675 $handle = $this->getReader();
679 $val = dba_fetch( $key, $handle );
680 list( $val, $expiry ) = $this->decode( $val );
681 # Must close ASAP because locks are held
682 dba_close( $handle );
684 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
685 # Key is expired, delete it
686 $handle = $this->getWriter();
687 dba_delete( $key, $handle );
688 dba_close( $handle );
689 wfDebug( __METHOD__.": $key expired\n" );
692 wfProfileOut( __METHOD__ );
696 function set( $key, $value, $exptime=0 ) {
697 wfProfileIn( __METHOD__ );
698 wfDebug( __METHOD__."($key)\n" );
699 $blob = $this->encode( $value, $exptime );
700 $handle = $this->getWriter();
704 $ret = dba_replace( $key, $blob, $handle );
705 dba_close( $handle );
706 wfProfileOut( __METHOD__ );
710 function delete( $key, $time = 0 ) {
711 wfProfileIn( __METHOD__ );
712 wfDebug( __METHOD__."($key)\n" );
713 $handle = $this->getWriter();
717 $ret = dba_delete( $key, $handle );
718 dba_close( $handle );
719 wfProfileOut( __METHOD__ );
723 function add( $key, $value, $exptime = 0 ) {
724 wfProfileIn( __METHOD__ );
725 $blob = $this->encode( $value, $exptime );
726 $handle = $this->getWriter();
730 $ret = dba_insert( $key, $blob, $handle );
731 # Insert failed, check to see if it failed due to an expired key
733 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
734 if ( $expiry < time() ) {
735 # Yes expired, delete and try again
736 dba_delete( $key, $handle );
737 $ret = dba_insert( $key, $blob, $handle );
738 # This time if it failed then it will be handled by the caller like any other race
742 dba_close( $handle );
743 wfProfileOut( __METHOD__ );