use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
-use MediaWiki\Services\ServiceDisabledException;
/**
* Functions to get cache objects
*
* - ObjectCache::getLocalServerInstance( $fallbackType )
* Purpose: Memory cache for very hot keys.
- * Stored only on the individual web server (typically APC for web requests,
+ * Stored only on the individual web server (typically APC or APCu for web requests,
* and EmptyBagOStuff in CLI mode).
* Not replicated to the other servers.
*
*
* @param string $id A key in $wgObjectCaches.
* @return BagOStuff
- * @throws MWException
+ * @throws InvalidArgumentException
*/
public static function newFromId( $id ) {
global $wgObjectCaches;
if ( !isset( $wgObjectCaches[$id] ) ) {
- throw new MWException( "Invalid object cache type \"$id\" requested. " .
+ // Always recognize these ones
+ if ( $id === CACHE_NONE ) {
+ return new EmptyBagOStuff();
+ } elseif ( $id === 'hash' ) {
+ return new HashBagOStuff();
+ }
+
+ throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " .
"It is not present in \$wgObjectCaches." );
}
* - loggroup: Alias to set 'logger' key with LoggerFactory group.
* - .. Other parameters passed to factory or class.
* @return BagOStuff
- * @throws MWException
+ * @throws InvalidArgumentException
*/
public static function newFromParams( $params ) {
if ( isset( $params['loggroup'] ) ) {
? $params['reportDupes']
: true;
// Do b/c logic for SqlBagOStuff
- if ( is_subclass_of( $class, SqlBagOStuff::class ) ) {
+ if ( is_a( $class, SqlBagOStuff::class, true ) ) {
if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
$params['servers'] = [ $params['server'] ];
- unset( $param['server'] );
+ unset( $params['server'] );
}
// In the past it was not required to set 'dbDirectory' in $wgObjectCaches
if ( isset( $params['servers'] ) ) {
}
return new $class( $params );
} else {
- throw new MWException( "The definition of cache type \""
+ throw new InvalidArgumentException( "The definition of cache type \""
. print_r( $params, true ) . "\" lacks both "
. "factory and class parameters." );
}
/**
* Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
*
- * This will look for any APC style server-local cache.
+ * This will look for any APC or APCu style server-local cache.
* A fallback cache can be specified if none is found.
*
* // Direct calls
*
* @param int|string|array $fallback Fallback cache or parameter map with 'fallback'
* @return BagOStuff
- * @throws MWException
+ * @throws InvalidArgumentException
* @since 1.27
*/
public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
- 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 {
+ $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
+ if ( $cache instanceof EmptyBagOStuff ) {
if ( is_array( $fallback ) ) {
- $id = isset( $fallback['fallback'] ) ? $fallback['fallback'] : CACHE_NONE;
- } else {
- $id = $fallback;
- }
- }
-
- return self::getInstance( $id );
- }
-
- /**
- * @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
- * @deprecated since 1.27
- */
- public static function newAccelerator( $params = [], $fallback = null ) {
- if ( $fallback === null ) {
- if ( is_array( $params ) && isset( $params['fallback'] ) ) {
- $fallback = $params['fallback'];
- } elseif ( !is_array( $params ) ) {
- $fallback = $params;
+ $fallback = isset( $fallback['fallback'] ) ? $fallback['fallback'] : CACHE_NONE;
}
+ $cache = self::getInstance( $fallback );
}
- return self::getLocalServerInstance( $fallback );
+ return $cache;
}
/**
* @since 1.26
* @param string $id A key in $wgWANObjectCaches.
* @return WANObjectCache
- * @throws MWException
+ * @throws UnexpectedValueException
*/
public static function newWANCacheFromId( $id ) {
- global $wgWANObjectCaches;
+ global $wgWANObjectCaches, $wgObjectCaches;
if ( !isset( $wgWANObjectCaches[$id] ) ) {
- throw new MWException( "Invalid object cache type \"$id\" requested. " .
- "It is not present in \$wgWANObjectCaches." );
+ throw new UnexpectedValueException(
+ "Cache type \"$id\" requested is not present in \$wgWANObjectCaches." );
}
$params = $wgWANObjectCaches[$id];
+ if ( !isset( $wgObjectCaches[$params['cacheId']] ) ) {
+ throw new UnexpectedValueException(
+ "Cache type \"{$params['cacheId']}\" is not present in \$wgObjectCaches." );
+ }
+ $params['store'] = $wgObjectCaches[$params['cacheId']];
+
+ return self::newWANCacheFromParams( $params );
+ }
+
+ /**
+ * Create a new cache object of the specified type.
+ *
+ * @since 1.28
+ * @param array $params
+ * @return WANObjectCache
+ * @throws UnexpectedValueException
+ */
+ public static function newWANCacheFromParams( array $params ) {
foreach ( $params['channels'] as $action => $channel ) {
$params['relayers'][$action] = MediaWikiServices::getInstance()->getEventRelayerGroup()
->getRelayer( $channel );
$params['channels'][$action] = $channel;
}
- $params['cache'] = self::newFromId( $params['cacheId'] );
+ $params['cache'] = self::newFromParams( $params['store'] );
if ( isset( $params['loggroup'] ) ) {
$params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
} else {
*
* @since 1.26
* @return WANObjectCache
+ * @deprecated Since 1.28 Use MediaWikiServices::getMainWANCache()
*/
public static function getMainWANInstance() {
- global $wgMainWANCache;
-
- return self::getWANInstance( $wgMainWANCache );
+ return MediaWikiServices::getInstance()->getMainWANObjectCache();
}
/**
*
* @return BagOStuff
* @since 1.26
+ * @deprecated Since 1.28 Use MediaWikiServices::getMainObjectStash
*/
public static function getMainStashInstance() {
- global $wgMainStash;
-
- return self::getInstance( $wgMainStash );
+ return MediaWikiServices::getInstance()->getMainObjectStash();
}
/**