Add/update function level parameter documentation
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
index 6824d21..a59ca05 100644 (file)
 <?php
 /**
- * Functions to get cache objects
+ * Functions to get cache objects.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
  * @ingroup Cache
  */
 
 /**
- * FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
- * It acts as a memcached server with no RAM, that is, all objects are
- * cleared the moment they are set. All set operations succeed and all
- * get operations return null.
+ * Functions to get cache objects
+ *
  * @ingroup Cache
  */
-class FakeMemCachedClient {
-       function add ($key, $val, $exp = 0) { return true; }
-       function decr ($key, $amt=1) { return null; }
-       function delete ($key, $time = 0) { return false; }
-       function disconnect_all () { }
-       function enable_compress ($enable) { }
-       function forget_dead_hosts () { }
-       function get ($key) { return null; }
-       function get_multi ($keys) { return array_pad(array(), count($keys), null); }
-       function incr ($key, $amt=1) { return null; }
-       function replace ($key, $value, $exp=0) { return false; }
-       function run_command ($sock, $cmd) { return null; }
-       function set ($key, $value, $exp=0){ return true; }
-       function set_compress_threshold ($thresh){ }
-       function set_debug ($dbg) { }
-       function set_servers ($list) { }
-}
+class ObjectCache {
+       static $instances = array();
 
-global $wgCaches;
-$wgCaches = array();
+       /**
+        * Get a cached instance of the specified type of cache object.
+        *
+        * @param $id
+        *
+        * @return object
+        */
+       static function getInstance( $id ) {
+               if ( isset( self::$instances[$id] ) ) {
+                       return self::$instances[$id];
+               }
 
-/**
- * Get a cache object.
- * @param $inputType Integer: cache type, one the the CACHE_* constants.
- *
- * @return BagOStuff
- */
-function &wfGetCache( $inputType ) {
-       global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
-       $cache = false;
+               $object = self::newFromId( $id );
+               self::$instances[$id] = $object;
+               return $object;
+       }
 
-       if ( $inputType == CACHE_ANYTHING ) {
-               reset( $wgCaches );
-               $type = key( $wgCaches );
-               if ( $type === false || $type === CACHE_NONE ) {
-                       $type = CACHE_DB;
-               }
-       } else {
-               $type = $inputType;
+       /**
+        * Clear all the cached instances.
+        */
+       static function clear() {
+               self::$instances = array();
        }
 
-       if ( $type == CACHE_MEMCACHED ) {
-               if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ) {
-                       $wgCaches[CACHE_MEMCACHED] = new MemCachedClientforWiki(
-                               array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
-                       $wgCaches[CACHE_MEMCACHED]->set_servers( $wgMemCachedServers );
-                       $wgCaches[CACHE_MEMCACHED]->set_debug( $wgMemCachedDebug );
-               }
-               $cache =& $wgCaches[CACHE_MEMCACHED];
-       } elseif ( $type == CACHE_ACCEL ) {
-               if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
-                       if ( function_exists( 'eaccelerator_get' ) ) {
-                               $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
-                       } elseif ( function_exists( 'apc_fetch') ) {
-                               $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
-                       } elseif( function_exists( 'xcache_get' ) ) {
-                               $wgCaches[CACHE_ACCEL] = new XCacheBagOStuff();
-                       } elseif( function_exists( 'wincache_ucache_get' ) ) {
-                               $wgCaches[CACHE_ACCEL] = new WinCacheBagOStuff();
-                       } else {
-                               $wgCaches[CACHE_ACCEL] = false;
-                       }
-               }
-               if ( $wgCaches[CACHE_ACCEL] !== false ) {
-                       $cache =& $wgCaches[CACHE_ACCEL];
-               }
-       } elseif ( $type == CACHE_DBA ) {
-               if ( !array_key_exists( CACHE_DBA, $wgCaches ) ) {
-                       $wgCaches[CACHE_DBA] = new DBABagOStuff;
+       /**
+        * Create a new cache object of the specified type.
+        *
+        * @param $id
+        *
+        * @return ObjectCache
+        */
+       static function newFromId( $id ) {
+               global $wgObjectCaches;
+
+               if ( !isset( $wgObjectCaches[$id] ) ) {
+                       throw new MWException( "Invalid object cache type \"$id\" requested. " .
+                               "It is not present in \$wgObjectCaches." );
                }
-               $cache =& $wgCaches[CACHE_DBA];
+
+               return self::newFromParams( $wgObjectCaches[$id] );
        }
 
-       if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
-               if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
-                       $wgCaches[CACHE_DB] = new SqlBagOStuff('objectcache');
+       /**
+        * Create a new cache object from parameters
+        *
+        * @param $params array
+        *
+        * @return ObjectCache
+        */
+       static function newFromParams( $params ) {
+               if ( isset( $params['factory'] ) ) {
+                       return call_user_func( $params['factory'], $params );
+               } elseif ( isset( $params['class'] ) ) {
+                       $class = $params['class'];
+                       return new $class( $params );
+               } else {
+                       throw new MWException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " .
+                               "factory and class parameters." );
                }
-               $cache =& $wgCaches[CACHE_DB];
        }
 
-       if ( $cache === false ) {
-               if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
-                       $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
+       /**
+        * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
+        *
+        * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
+        * If a caching method is configured for any of the main caches ($wgMainCacheType,
+        * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
+        * be an alias to the configured cache choice for that.
+        * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
+        * then CACHE_ANYTHING will forward to CACHE_DB.
+        */
+       static function newAnything( $params ) {
+               global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
+               $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
+               foreach ( $candidates as $candidate ) {
+                       if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
+                               return self::getInstance( $candidate );
+                       }
                }
-               $cache =& $wgCaches[CACHE_NONE];
+               return self::getInstance( CACHE_DB );
        }
 
-       return $cache;
-}
-
-/** Get the main cache object */
-function &wfGetMainCache() {
-       global $wgMainCacheType;
-       $ret =& wfGetCache( $wgMainCacheType );
-       return $ret;
-}
-
-/** Get the cache object used by the message cache */
-function &wfGetMessageCacheStorage() {
-       global $wgMessageCacheType;
-       $ret =& wfGetCache( $wgMessageCacheType );
-       return $ret;
-}
+       /**
+        * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
+        *
+        * @return ObjectCache
+        */
+       static function newAccelerator( $params ) {
+               if ( function_exists( 'apc_fetch') ) {
+                       $id = 'apc';
+               } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
+                       $id = 'xcache';
+               } elseif( function_exists( 'wincache_ucache_get' ) ) {
+                       $id = 'wincache';
+               } else {
+                       throw new MWException( "CACHE_ACCEL requested but no suitable object " .
+                               "cache is present. You may want to install APC." );
+               }
+               return self::newFromId( $id );
+       }
 
-/** Get the cache object used by the parser cache */
-function &wfGetParserCacheStorage() {
-       global $wgParserCacheType;
-       $ret =& wfGetCache( $wgParserCacheType );
-       return $ret;
+       /**
+        * Factory function that creates a memcached client object.
+        * The idea of this is that it might eventually detect and automatically
+        * support the PECL extension, assuming someone can get it to compile.
+        *
+        * @param $params array
+        *
+        * @return MemcachedPhpBagOStuff
+        */
+       static function newMemcached( $params ) {
+               return new MemcachedPhpBagOStuff( $params );
+       }
 }