Merge "Add `makeKey` and `makeGlobalKey` to BagOStuff"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
index c40f696..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.
  *
  * - ObjectCache::getMainClusterInstance()
- *   Purpose: Cache.
+ *   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'] ) ) {
@@ -268,9 +295,9 @@ class ObjectCache {
         * @return BagOStuff
         */
        public static function getMainClusterInstance() {
-               $config = RequestContext::getMain()->getConfig();
-               $id = $config->get( 'MainCacheType' );
-               return self::getInstance( $id );
+               global $wgMainCacheType;
+
+               return self::getInstance( $wgMainCacheType );
        }
 
        /**