},
'LocalServerObjectCache' => function ( MediaWikiServices $services ) {
- $mainConfig = $services->getMainConfig();
-
- if ( function_exists( 'apc_fetch' ) ) {
- $id = 'apc';
- } elseif ( function_exists( 'apcu_fetch' ) ) {
- $id = 'apcu';
- } elseif ( function_exists( 'wincache_ucache_get' ) ) {
- $id = 'wincache';
- } else {
- $id = CACHE_NONE;
- }
-
- if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
- throw new UnexpectedValueException(
- "Cache type \"$id\" is not present in \$wgObjectCaches." );
- }
-
- return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
+ $cacheId = \ObjectCache::detectLocalServerCache();
+ return \ObjectCache::newFromId( $cacheId );
},
'VirtualRESTServiceClient' => function ( MediaWikiServices $services ) {
self::$instances = [];
self::$wanInstances = [];
}
+
+ /**
+ * Detects which local server cache library is present and returns a configuration for it
+ * @since 1.32
+ *
+ * @return int|string Index to cache in $wgObjectCaches
+ */
+ public static function detectLocalServerCache() {
+ if ( function_exists( 'apc_fetch' ) ) {
+ return 'apc';
+ } elseif ( function_exists( 'apcu_fetch' ) ) {
+ return 'apcu';
+ } elseif ( function_exists( 'wincache_ucache_get' ) ) {
+ return 'wincache';
+ }
+ return CACHE_NONE;
+ }
}
<?php
-use MediaWiki\MediaWikiServices;
-
/**
* ExtensionRegistry class
*
// We use a try/catch because we don't want to fail here
// if $wgObjectCaches is not configured properly for APC setup
try {
- $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
+ // Don't use MediaWikiServices here to prevent instantiating it before extensions have
+ // been loaded
+ $cacheId = ObjectCache::detectLocalServerCache();
+ $cache = ObjectCache::newFromId( $cacheId );
} catch ( MWException $e ) {
$cache = new EmptyBagOStuff();
}