* (bug 8437) Make Title::loadRestrictions() initialise $mRestrictions properly
[lhc/web/wiklou.git] / includes / BagOStuff.php
index 182756a..6edef87 100644 (file)
@@ -146,6 +146,17 @@ class BagOStuff {
                if($this->debugmode)
                        wfDebug("BagOStuff debug: $text\n");
        }
+
+       /**
+        * Convert an optionally relative time to an absolute time
+        */
+       static function convertExpiry( $exptime ) {
+               if(($exptime != 0) && ($exptime < 3600*24*30)) {
+                       return time() + $exptime;
+               } else {
+                       return $exptime;
+               }
+       }
 }
 
 
@@ -183,9 +194,7 @@ class HashBagOStuff extends BagOStuff {
        }
 
        function set($key,$value,$exptime=0) {
-               if(($exptime != 0) && ($exptime < 3600*24*30))
-                       $exptime = time() + $exptime;
-               $this->bag[$key] = array( $value, $exptime );
+               $this->bag[$key] = array( $value, BagOStuff::convertExpiry( $exptime ) );
        }
 
        function delete($key,$time=0) {
@@ -381,7 +390,8 @@ class MediaWikiBagOStuff extends SqlBagOStuff {
        }
        function _doinsert($t, $v) {
                $dbw =& wfGetDB( DB_MASTER );
-               return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert');
+               return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
+                       array( 'IGNORE' ) );
        }
        function _fetchobject($result) {
                $dbw =& wfGetDB( DB_MASTER );
@@ -483,15 +493,18 @@ class TurckBagOStuff extends BagOStuff {
 class APCBagOStuff extends BagOStuff {
        function get($key) {
                $val = apc_fetch($key);
+               if ( is_string( $val ) ) {
+                       $val = unserialize( $val );
+               }
                return $val;
        }
        
        function set($key, $value, $exptime=0) {
-               apc_store($key, $value, $exptime);
+               apc_store($key, serialize($value), $exptime);
                return true;
        }
        
-       function delete($key) {
+       function delete($key, $time=0) {
                apc_delete($key);
                return true;
        }
@@ -535,4 +548,136 @@ class eAccelBagOStuff extends BagOStuff {
                return true;
        }
 }
+
+class DBABagOStuff extends BagOStuff {
+       var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
+       
+       function __construct( $handler = 'db3', $dir = false ) {
+               if ( $dir === false ) {
+                       global $wgTmpDirectory;
+                       $dir = $wgTmpDirectory;
+               }
+               $this->mFile = "$dir/mw-cache-" . wfWikiID();
+               $this->mFile .= '.db';
+               $this->mHandler = $handler;
+       }
+
+       /**
+        * Encode value and expiry for storage
+        */
+       function encode( $value, $expiry ) {
+               # Convert to absolute time
+               $expiry = BagOStuff::convertExpiry( $expiry );
+               return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
+       }
+
+       /**
+        * @return list containing value first and expiry second
+        */
+       function decode( $blob ) {
+               if ( !is_string( $blob ) ) {
+                       return array( null, 0 );
+               } else {
+                       return array( 
+                               unserialize( substr( $blob, 11 ) ),
+                               intval( substr( $blob, 0, 10 ) )
+                       );
+               }
+       }
+
+       function getReader() {
+               if ( file_exists( $this->mFile ) ) {
+                       $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
+               } else {
+                       $handle = $this->getWriter();
+               }
+               if ( !$handle ) {
+                       wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
+               }
+               return $handle;
+       }
+
+       function getWriter() {
+               $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
+               if ( !$handle ) {
+                       wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
+               }
+               return $handle;
+       }
+
+       function get( $key ) {
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__."($key)\n" );
+               $handle = $this->getReader();
+               if ( !$handle ) {
+                       return null;
+               }
+               $val = dba_fetch( $key, $handle );
+               list( $val, $expiry ) = $this->decode( $val );
+               # Must close ASAP because locks are held
+               dba_close( $handle );
+
+               if ( !is_null( $val ) && $expiry && $expiry < time() ) {
+                       # Key is expired, delete it
+                       $handle = $this->getWriter();
+                       dba_delete( $key, $handle );
+                       dba_close( $handle );
+                       wfDebug( __METHOD__.": $key expired\n" );
+                       $val = null;
+               }
+               wfProfileOut( __METHOD__ );
+               return $val;
+       }
+
+       function set( $key, $value, $exptime=0 ) {
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__."($key)\n" );
+               $blob = $this->encode( $value, $exptime );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_replace( $key, $blob, $handle );
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
+       function delete( $key, $time = 0 ) {
+               wfProfileIn( __METHOD__ );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_delete( $key, $handle );
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
+       function add( $key, $value, $exptime = 0 ) {
+               wfProfileIn( __METHOD__ );
+               $blob = $this->encode( $value, $exptime );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_insert( $key, $blob, $handle );
+               # Insert failed, check to see if it failed due to an expired key
+               if ( !$ret ) {
+                       list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
+                       if ( $expiry < time() ) {
+                               # Yes expired, delete and try again
+                               dba_delete( $key, $handle );
+                               $ret = dba_insert( $key, $blob, $handle );
+                               # This time if it failed then it will be handled by the caller like any other race
+                       }
+               }
+
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+}
+       
 ?>