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
22 * @defgroup Cache Cache
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
32 * backends for local hash array and SQL table included:
34 * $bag = new HashBagOStuff();
35 * $bag = new MediaWikiBagOStuff($tablename); # connect to db first
43 function __construct() {
44 $this->set_debug( false );
47 function set_debug($bool) {
48 $this->debugmode
= $bool;
51 /* *** THE GUTS OF THE OPERATION *** */
52 /* Override these with functional things in subclasses */
59 function set($key, $value, $exptime=0) {
64 function delete($key, $time=0) {
69 function lock($key, $timeout = 0) {
74 function unlock($key) {
84 /* *** Emulated functions *** */
85 /* Better performance can likely be got with custom written versions */
86 function get_multi($keys) {
88 foreach($keys as $key)
89 $out[$key] = $this->get($key);
93 function set_multi($hash, $exptime=0) {
94 foreach($hash as $key => $value)
95 $this->set($key, $value, $exptime);
98 function add($key, $value, $exptime=0) {
99 if( $this->get($key) == false ) {
100 $this->set($key, $value, $exptime);
105 function add_multi($hash, $exptime=0) {
106 foreach($hash as $key => $value)
107 $this->add($key, $value, $exptime);
110 function delete_multi($keys, $time=0) {
111 foreach($keys as $key)
112 $this->delete($key, $time);
115 function replace($key, $value, $exptime=0) {
116 if( $this->get($key) !== false )
117 $this->set($key, $value, $exptime);
120 function incr($key, $value=1) {
121 if ( !$this->lock($key) ) {
124 $value = intval($value);
125 if($value < 0) $value = 0;
128 if( ($n = $this->get($key)) !== false ) {
130 $this->set($key, $n); // exptime?
136 function decr($key, $value=1) {
137 if ( !$this->lock($key) ) {
140 $value = intval($value);
141 if($value < 0) $value = 0;
144 if( ($n = $this->get($key)) !== false ) {
147 $this->set($key, $m); // exptime?
153 function _debug($text) {
155 wfDebug("BagOStuff debug: $text\n");
159 * Convert an optionally relative time to an absolute time
161 static function convertExpiry( $exptime ) {
162 if(($exptime != 0) && ($exptime < 3600*24*30)) {
163 return time() +
$exptime;
172 * Functional versions!
173 * This is a test of the interface, mainly. It stores things in an associative
174 * array, which is not going to persist between program runs.
178 class HashBagOStuff
extends BagOStuff
{
181 function __construct() {
182 $this->bag
= array();
185 function _expire($key) {
186 $et = $this->bag
[$key][1];
187 if(($et == 0) ||
($et > time()))
194 if(!$this->bag
[$key])
196 if($this->_expire($key))
198 return $this->bag
[$key][0];
201 function set($key,$value,$exptime=0) {
202 $this->bag
[$key] = array( $value, BagOStuff
::convertExpiry( $exptime ) );
205 function delete($key,$time=0) {
206 if(!$this->bag
[$key])
208 unset($this->bag
[$key]);
213 return array_keys( $this->bag
);
218 * Generic class to store objects in a database
222 abstract class SqlBagOStuff
extends BagOStuff
{
224 var $lastexpireall = 0;
229 * @param $tablename String: name of the table to use
231 function __construct($tablename = 'objectcache') {
232 $this->table
= $tablename;
236 /* expire old entries if any */
237 $this->garbageCollect();
239 $res = $this->_query(
240 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
242 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
245 if($row=$this->_fetchobject($res)) {
246 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
247 if ( $row->exptime
!= $this->_maxdatetime() &&
248 wfTimestamp( TS_UNIX
, $row->exptime
) < time() )
250 $this->_debug("get: key has expired, deleting");
254 return $this->_unserialize($this->_blobdecode($row->value
));
256 $this->_debug('get: no matching rows');
261 function set($key,$value,$exptime=0) {
262 if ( $this->_readonly() ) {
265 $exptime = intval($exptime);
266 if($exptime < 0) $exptime = 0;
268 $exp = $this->_maxdatetime();
270 if($exptime < 3.16e8
) # ~10 years
272 $exp = $this->_fromunixtime($exptime);
276 "DELETE FROM $0 WHERE keyname='$1'", $key );
277 $this->_doinsert($this->getTableName(), array(
279 'value' => $this->_blobencode($this->_serialize($value)),
286 function delete($key,$time=0) {
287 if ( $this->_readonly() ) {
292 "DELETE FROM $0 WHERE keyname='$1'", $key );
298 $res = $this->_query( "SELECT keyname FROM $0" );
300 $this->_debug("keys: ** error: " . $this->_dberror($res) . " **");
304 while( $row = $this->_fetchobject($res) ) {
305 $result[] = $row->keyname
;
310 function getTableName() {
314 function _query($sql) {
315 $reps = func_get_args();
316 $reps[0] = $this->getTableName();
318 for($i=0;$i<count($reps);$i++
) {
321 $i > 0 ?
$this->_strencode($reps[$i]) : $reps[$i],
324 $res = $this->_doquery($sql);
326 $this->_debug('query failed: ' . $this->_dberror($res));
331 function _strencode($str) {
332 /* Protect strings in SQL */
333 return str_replace( "'", "''", $str );
335 function _blobencode($str) {
338 function _blobdecode($str) {
342 abstract function _doinsert($table, $vals);
343 abstract function _doquery($sql);
345 abstract function _readonly();
348 function _commit() {}
350 function _freeresult($result) {
355 function _dberror($result) {
357 return 'unknown error';
360 abstract function _maxdatetime();
361 abstract function _fromunixtime($ts);
363 function garbageCollect() {
364 /* Ignore 99% of requests */
365 if ( !mt_rand( 0, 100 ) ) {
367 /* Avoid repeating the delete within a few seconds */
368 if ( $nowtime > ($this->lastexpireall +
1) ) {
369 $this->lastexpireall
= $nowtime;
375 function expireall() {
376 /* Remove any items that have expired */
377 if ( $this->_readonly() ) {
380 $now = $this->_fromunixtime( time() );
382 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
386 function deleteall() {
387 /* Clear *all* items from cache table */
388 if ( $this->_readonly() ) {
392 $this->_query( "DELETE FROM $0" );
397 * Serialize an object and, if possible, compress the representation.
398 * On typical message and page data, this can provide a 3X decrease
399 * in storage requirements.
404 function _serialize( &$data ) {
405 $serial = serialize( $data );
406 if( function_exists( 'gzdeflate' ) ) {
407 return gzdeflate( $serial );
414 * Unserialize and, if necessary, decompress an object.
415 * @param $serial string
418 function _unserialize( $serial ) {
419 if( function_exists( 'gzinflate' ) ) {
420 $decomp = @gzinflate
( $serial );
421 if( false !== $decomp ) {
425 $ret = unserialize( $serial );
431 * Stores objects in the main database of the wiki
435 class MediaWikiBagOStuff
extends SqlBagOStuff
{
436 var $tableInitialised = false;
440 if ( !isset( $this->lb
) ) {
441 $this->lb
= wfGetLBFactory()->newMainLB();
442 $this->db
= $this->lb
->getConnection( DB_MASTER
);
443 $this->db
->clearFlag( DBO_TRX
);
448 $this->_getDB()->begin();
451 $this->_getDB()->commit();
453 function _doquery($sql) {
454 return $this->_getDB()->query( $sql, __METHOD__
);
456 function _doinsert($t, $v) {
457 return $this->_getDB()->insert($t, $v, __METHOD__
, array( 'IGNORE' ) );
459 function _fetchobject($result) {
460 return $this->_getDB()->fetchObject($result);
462 function _freeresult($result) {
463 return $this->_getDB()->freeResult($result);
465 function _dberror($result) {
466 return $this->_getDB()->lastError();
468 function _maxdatetime() {
469 if ( time() > 0x7fffffff ) {
470 return $this->_fromunixtime( 1<<62 );
472 return $this->_fromunixtime( 0x7fffffff );
475 function _fromunixtime($ts) {
476 return $this->_getDB()->timestamp($ts);
478 function _readonly(){
481 function _strencode($s) {
482 return $this->_getDB()->strencode($s);
484 function _blobencode($s) {
485 return $this->_getDB()->encodeBlob($s);
487 function _blobdecode($s) {
488 return $this->_getDB()->decodeBlob($s);
490 function getTableName() {
491 if ( !$this->tableInitialised
) {
492 $dbw = $this->_getDB();
493 /* This is actually a hack, we should be able
494 to use Language classes here... or not */
496 throw new MWException("Could not connect to database");
497 $this->table
= $dbw->tableName( $this->table
);
498 $this->tableInitialised
= true;
505 * This is a wrapper for Turck MMCache's shared memory functions.
507 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
508 * to use a weird custom serializer that randomly segfaults. So we wrap calls
509 * with serialize()/unserialize().
511 * The thing I noticed about the Turck serialized data was that unlike ordinary
512 * serialize(), it contained the names of methods, and judging by the amount of
513 * binary data, perhaps even the bytecode of the methods themselves. It may be
514 * that Turck's serializer is faster, so a possible future extension would be
515 * to use it for arrays but not for objects.
519 class TurckBagOStuff
extends BagOStuff
{
521 $val = mmcache_get( $key );
522 if ( is_string( $val ) ) {
523 $val = unserialize( $val );
528 function set($key, $value, $exptime=0) {
529 mmcache_put( $key, serialize( $value ), $exptime );
533 function delete($key, $time=0) {
538 function lock($key, $waitTimeout = 0 ) {
539 mmcache_lock( $key );
543 function unlock($key) {
544 mmcache_unlock( $key );
550 * This is a wrapper for APC's shared memory functions
554 class APCBagOStuff
extends BagOStuff
{
556 $val = apc_fetch($key);
557 if ( is_string( $val ) ) {
558 $val = unserialize( $val );
563 function set($key, $value, $exptime=0) {
564 apc_store($key, serialize($value), $exptime);
568 function delete($key, $time=0) {
576 * This is a wrapper for eAccelerator's shared memory functions.
578 * This is basically identical to the Turck MMCache version,
579 * mostly because eAccelerator is based on Turck MMCache.
583 class eAccelBagOStuff
extends BagOStuff
{
585 $val = eaccelerator_get( $key );
586 if ( is_string( $val ) ) {
587 $val = unserialize( $val );
592 function set($key, $value, $exptime=0) {
593 eaccelerator_put( $key, serialize( $value ), $exptime );
597 function delete($key, $time=0) {
598 eaccelerator_rm( $key );
602 function lock($key, $waitTimeout = 0 ) {
603 eaccelerator_lock( $key );
607 function unlock($key) {
608 eaccelerator_unlock( $key );
614 * Wrapper for XCache object caching functions; identical interface
619 class XCacheBagOStuff
extends BagOStuff
{
622 * Get a value from the XCache object cache
624 * @param $key String: cache key
627 public function get( $key ) {
628 $val = xcache_get( $key );
629 if( is_string( $val ) )
630 $val = unserialize( $val );
635 * Store a value in the XCache object cache
637 * @param $key String: cache key
638 * @param $value Mixed: object to store
639 * @param $expire Int: expiration time
642 public function set( $key, $value, $expire = 0 ) {
643 xcache_set( $key, serialize( $value ), $expire );
648 * Remove a value from the XCache object cache
650 * @param $key String: cache key
651 * @param $time Int: not used in this implementation
654 public function delete( $key, $time = 0 ) {
655 xcache_unset( $key );
665 class DBABagOStuff
extends BagOStuff
{
666 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
668 function __construct( $handler = 'db3', $dir = false ) {
669 if ( $dir === false ) {
670 global $wgTmpDirectory;
671 $dir = $wgTmpDirectory;
673 $this->mFile
= "$dir/mw-cache-" . wfWikiID();
674 $this->mFile
.= '.db';
675 wfDebug( __CLASS__
.": using cache file {$this->mFile}\n" );
676 $this->mHandler
= $handler;
680 * Encode value and expiry for storage
682 function encode( $value, $expiry ) {
683 # Convert to absolute time
684 $expiry = BagOStuff
::convertExpiry( $expiry );
685 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
689 * @return list containing value first and expiry second
691 function decode( $blob ) {
692 if ( !is_string( $blob ) ) {
693 return array( null, 0 );
696 unserialize( substr( $blob, 11 ) ),
697 intval( substr( $blob, 0, 10 ) )
702 function getReader() {
703 if ( file_exists( $this->mFile
) ) {
704 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
706 $handle = $this->getWriter();
709 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
714 function getWriter() {
715 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
717 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
722 function get( $key ) {
723 wfProfileIn( __METHOD__
);
724 wfDebug( __METHOD__
."($key)\n" );
725 $handle = $this->getReader();
729 $val = dba_fetch( $key, $handle );
730 list( $val, $expiry ) = $this->decode( $val );
731 # Must close ASAP because locks are held
732 dba_close( $handle );
734 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
735 # Key is expired, delete it
736 $handle = $this->getWriter();
737 dba_delete( $key, $handle );
738 dba_close( $handle );
739 wfDebug( __METHOD__
.": $key expired\n" );
742 wfProfileOut( __METHOD__
);
746 function set( $key, $value, $exptime=0 ) {
747 wfProfileIn( __METHOD__
);
748 wfDebug( __METHOD__
."($key)\n" );
749 $blob = $this->encode( $value, $exptime );
750 $handle = $this->getWriter();
754 $ret = dba_replace( $key, $blob, $handle );
755 dba_close( $handle );
756 wfProfileOut( __METHOD__
);
760 function delete( $key, $time = 0 ) {
761 wfProfileIn( __METHOD__
);
762 wfDebug( __METHOD__
."($key)\n" );
763 $handle = $this->getWriter();
767 $ret = dba_delete( $key, $handle );
768 dba_close( $handle );
769 wfProfileOut( __METHOD__
);
773 function add( $key, $value, $exptime = 0 ) {
774 wfProfileIn( __METHOD__
);
775 $blob = $this->encode( $value, $exptime );
776 $handle = $this->getWriter();
780 $ret = dba_insert( $key, $blob, $handle );
781 # Insert failed, check to see if it failed due to an expired key
783 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
784 if ( $expiry < time() ) {
785 # Yes expired, delete and try again
786 dba_delete( $key, $handle );
787 $ret = dba_insert( $key, $blob, $handle );
788 # This time if it failed then it will be handled by the caller like any other race
792 dba_close( $handle );
793 wfProfileOut( __METHOD__
);
798 $reader = $this->getReader();
799 $k1 = dba_firstkey( $reader );
804 while( $key = dba_nextkey( $reader ) ) {