API: Standardize limits. Use ApiBase constants to avoid similar breakage and inconsis...
[lhc/web/wiklou.git] / includes / BagOStuff.php
index a958d41..a40d020 100644 (file)
  * the PHP memcached client.
  *
  * backends for local hash array and SQL table included:
- * $bag = new HashBagOStuff();
- * $bag = new MysqlBagOStuff($tablename); # connect to db first
+ * <code>
+ *   $bag = new HashBagOStuff();
+ *   $bag = new MysqlBagOStuff($tablename); # connect to db first
+ * </code>
  *
  */
 class BagOStuff {
@@ -170,7 +172,7 @@ class HashBagOStuff extends BagOStuff {
        */
        var $bag;
 
-       function HashBagOStuff() {
+       function __construct() {
                $this->bag = array();
        }
 
@@ -220,7 +222,7 @@ abstract class SqlBagOStuff extends BagOStuff {
        var $table;
        var $lastexpireall = 0;
 
-       function SqlBagOStuff($tablename = 'objectcache') {
+       function __construct($tablename = 'objectcache') {
                $this->table = $tablename;
        }
 
@@ -251,6 +253,9 @@ abstract class SqlBagOStuff extends BagOStuff {
        }
 
        function set($key,$value,$exptime=0) {
+               if ( wfReadOnly() ) {
+                       return false;
+               }
                $exptime = intval($exptime);
                if($exptime < 0) $exptime = 0;
                if($exptime == 0) {
@@ -270,6 +275,9 @@ abstract class SqlBagOStuff extends BagOStuff {
        }
 
        function delete($key,$time=0) {
+               if ( wfReadOnly() ) {
+                       return false;
+               }
                $this->_query(
                        "DELETE FROM $0 WHERE keyname='$1'", $key );
                return true; /* ? */
@@ -337,12 +345,18 @@ abstract class SqlBagOStuff extends BagOStuff {
 
        function expireall() {
                /* Remove any items that have expired */
+               if ( wfReadOnly() ) {
+                       return false;
+               }
                $now = $this->_fromunixtime( time() );
                $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
        }
 
        function deleteall() {
                /* Clear *all* items from cache table */
+               if ( wfReadOnly() ) {
+                       return false;
+               }
                $this->_query( "DELETE FROM $0" );
        }
 
@@ -387,24 +401,24 @@ class MediaWikiBagOStuff extends SqlBagOStuff {
        var $tableInitialised = false;
 
        function _doquery($sql) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
        }
        function _doinsert($t, $v) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
                        array( 'IGNORE' ) );
        }
        function _fetchobject($result) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->fetchObject($result);
        }
        function _freeresult($result) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->freeResult($result);
        }
        function _dberror($result) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->lastError();
        }
        function _maxdatetime() {
@@ -415,24 +429,24 @@ class MediaWikiBagOStuff extends SqlBagOStuff {
                }
        }
        function _fromunixtime($ts) {
-               $dbw =& wfGetDB(DB_MASTER);
+               $dbw = wfGetDB(DB_MASTER);
                return $dbw->timestamp($ts);
        }
        function _strencode($s) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->strencode($s);
        }
        function _blobencode($s) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->encodeBlob($s);
        }
        function _blobdecode($s) {
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                return $dbw->decodeBlob($s);
        }
        function getTableName() {
                if ( !$this->tableInitialised ) {
-                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                        /* This is actually a hack, we should be able
                           to use Language classes here... or not */
                        if (!$dbw)
@@ -492,7 +506,6 @@ class TurckBagOStuff extends BagOStuff {
  * This is a wrapper for APC's shared memory functions
  *
  */
-
 class APCBagOStuff extends BagOStuff {
        function get($key) {
                $val = apc_fetch($key);
@@ -551,6 +564,55 @@ class eAccelBagOStuff extends BagOStuff {
        }
 }
 
+/**
+ * Wrapper for XCache object caching functions; identical interface
+ * to the APC wrapper
+ */
+class XCacheBagOStuff extends BagOStuff {
+
+       /**
+        * Get a value from the XCache object cache
+        *
+        * @param string $key Cache key
+        * @return mixed
+        */
+       public function get( $key ) {
+               $val = xcache_get( $key );
+               if( is_string( $val ) )
+                       $val = unserialize( $val );
+               return $val;
+       }
+       
+       /**
+        * Store a value in the XCache object cache
+        *
+        * @param string $key Cache key
+        * @param mixed $value Object to store
+        * @param int $expire Expiration time
+        * @return bool
+        */
+       public function set( $key, $value, $expire = 0 ) {
+               xcache_set( $key, serialize( $value ), $expire );
+               return true;
+       }
+       
+       /**
+        * Remove a value from the XCache object cache
+        *
+        * @param string $key Cache key
+        * @param int $time Not used in this implementation
+        * @return bool
+        */
+       public function delete( $key, $time = 0 ) {
+               xcache_unset( $key );
+               return true;
+       }
+       
+}
+
+/**
+ * @todo document
+ */
 class DBABagOStuff extends BagOStuff {
        var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
        
@@ -647,6 +709,7 @@ class DBABagOStuff extends BagOStuff {
 
        function delete( $key, $time = 0 ) {
                wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__."($key)\n" );
                $handle = $this->getWriter();
                if ( !$handle ) {
                        return false;
@@ -682,4 +745,4 @@ class DBABagOStuff extends BagOStuff {
        }
 }
        
-?>
+