Merge "Add `makeKey` and `makeGlobalKey` to BagOStuff"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
index 6019105..d739d8e 100644 (file)
@@ -43,12 +43,12 @@ use MediaWiki\Logger\LoggerFactory;
  * Primary entry points:
  *
  * - ObjectCache::newAccelerator( $fallbackType )
- *   Purpose: Cache.
+ *   Purpose: Cache for very hot keys.
  *   Stored only on the individual web server.
  *   Not associated with other servers.
  *
- * - wfGetMainCache()
- *   Purpose: Cache.
+ * - ObjectCache::getMainClusterInstance()
+ *   Purpose: Memory storage for per-cluster coordination and tracking.
  *   Stored centrally within the local data-center.
  *   Not replicated to other DCs.
  *   Also known as $wgMemc. Configured by $wgMainCacheType.
@@ -71,10 +71,9 @@ use MediaWiki\Logger\LoggerFactory;
  * @ingroup Cache
  */
 class ObjectCache {
-       /** @var Array Map of (id => BagOStuff) */
+       /** @var BagOStuff[] Map of (id => BagOStuff) */
        public static $instances = array();
-
-       /** @var Array Map of (id => WANObjectCache) */
+       /** @var WANObjectCache[] Map of (id => WANObjectCache) */
        public static $wanInstances = array();
 
        /**
@@ -124,6 +123,31 @@ class ObjectCache {
                return self::newFromParams( $wgObjectCaches[$id] );
        }
 
+       /**
+        * Get the default keyspace for this wiki.
+        *
+        * This is either the value of the `CachePrefix` configuration variable,
+        * or (if the former is unset) the `DBname` configuration variable, with
+        * `DBprefix` (if defined).
+        *
+        * @return string
+        */
+       public static function getDefaultKeyspace() {
+               global $wgCachePrefix, $wgDBname, $wgDBprefix;
+
+               $keyspace = $wgCachePrefix;
+               if ( is_string( $keyspace ) && $keyspace !== '' ) {
+                       return $keyspace;
+               }
+
+               $keyspace = $wgDBname;
+               if ( is_string( $wgDBprefix ) && $wgDBprefix !== '' ) {
+                       $keyspace .= '-' . $wgDBprefix;
+               }
+
+               return $keyspace;
+       }
+
        /**
         * Create a new cache object from parameters.
         *
@@ -143,6 +167,9 @@ class ObjectCache {
                        // have all logging suddenly disappear
                        $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
                }
+               if ( !isset( $params['keyspace'] ) ) {
+                       $params['keyspace'] = self::getDefaultKeyspace();
+               }
                if ( isset( $params['factory'] ) ) {
                        return call_user_func( $params['factory'], $params );
                } elseif ( isset( $params['class'] ) ) {
@@ -185,14 +212,26 @@ class ObjectCache {
         * This will look for any APC style server-local cache.
         * A fallback cache can be specified if none is found.
         *
-        * @param array $params [optional]
+        *     // Direct calls
+        *     ObjectCache::newAccelerator( $fallbackType );
+        *
+        *     // From $wgObjectCaches via newFromParams()
+        *     ObjectCache::newAccelerator( array( 'fallback' => $fallbackType ) );
+        *
+        * @param array $params [optional] Array key 'fallback' for $fallback.
         * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
         * @return BagOStuff
         * @throws MWException
         */
        public static function newAccelerator( $params = array(), $fallback = null ) {
-               if ( !is_array( $params ) && $fallback === null ) {
-                       $fallback = $params;
+               if ( $fallback === null ) {
+                       // The is_array check here is needed because in PHP 5.3:
+                       // $a = 'hash'; isset( $params['fallback'] ); yields true
+                       if ( is_array( $params ) && isset( $params['fallback'] ) ) {
+                               $fallback = $params['fallback'];
+                       } elseif ( !is_array( $params ) ) {
+                               $fallback = $params;
+                       }
                }
                if ( function_exists( 'apc_fetch' ) ) {
                        $id = 'apc';
@@ -249,6 +288,18 @@ class ObjectCache {
                return new $class( $params );
        }
 
+       /**
+        * Get the main cluster-local cache object.
+        *
+        * @since 1.27
+        * @return BagOStuff
+        */
+       public static function getMainClusterInstance() {
+               global $wgMainCacheType;
+
+               return self::getInstance( $wgMainCacheType );
+       }
+
        /**
         * Get the main WAN cache object.
         *