From 7b20e0ef7a5a3a310b4ccb427c8ad75a2c2a6db2 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 10 Jun 2015 04:53:41 +0100 Subject: [PATCH] objectcache: Minor code clean up in ObjectCache.php * Whitespace. * Simplify logic. * Apply coding conventions to documentation blocks (empty line before annotations, no empty lines between annotation, consistent order of annotations). Change-Id: I3e5268d6a6295643d5725c66ea2a01bccf610ed8 --- includes/objectcache/ObjectCache.php | 49 ++++++++++++---------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index 7faf4bb6a8..c5850b6a7f 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -38,6 +38,7 @@ use MediaWiki\Logger\LoggerFactory; class ObjectCache { /** @var Array Map of (id => BagOStuff) */ public static $instances = array(); + /** @var Array Map of (id => WANObjectCache) */ public static $wanInstances = array(); @@ -45,35 +46,29 @@ class ObjectCache { * Get a cached instance of the specified type of cache object. * * @param string $id - * * @return BagOStuff */ static function getInstance( $id ) { - if ( isset( self::$instances[$id] ) ) { - return self::$instances[$id]; + if ( !isset( self::$instances[$id] ) ) { + self::$instances[$id] = self::newFromId( $id ); } - $object = self::newFromId( $id ); - self::$instances[$id] = $object; - return $object; + return self::$instances[$id]; } /** * Get a cached instance of the specified type of cache object. * + * @since 1.26 * @param string $id - * * @return WANObjectCache - * @since 1.26 */ static function getWANInstance( $id ) { - if ( isset( self::$wanInstances[$id] ) ) { - return self::$wanInstances[$id]; + if ( !isset( self::$wanInstances[$id] ) ) { + self::$wanInstances[$id] = self::newWANCacheFromId( $id ); } - $object = self::newWANCacheFromId( $id ); - self::$wanInstances[$id] = $object; - return $object; + return self::$wanInstances[$id]; } /** @@ -88,9 +83,8 @@ class ObjectCache { * Create a new cache object of the specified type. * * @param string $id - * - * @throws MWException * @return BagOStuff + * @throws MWException */ static function newFromId( $id ) { global $wgObjectCaches; @@ -107,9 +101,8 @@ class ObjectCache { * Create a new cache object from parameters * * @param array $params - * - * @throws MWException * @return BagOStuff + * @throws MWException */ static function newFromParams( $params ) { if ( isset( $params['loggroup'] ) ) { @@ -140,6 +133,7 @@ class ObjectCache { * 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. + * * @param array $params * @return BagOStuff */ @@ -162,8 +156,8 @@ class ObjectCache { * * @param array $params * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24) - * @throws MWException * @return BagOStuff + * @throws MWException */ static function newAccelerator( $params, $fallback = null ) { if ( function_exists( 'apc_fetch' ) ) { @@ -173,11 +167,11 @@ class ObjectCache { } elseif ( function_exists( 'wincache_ucache_get' ) ) { $id = 'wincache'; } else { - if ( $fallback !== null ) { - return self::newFromId( $fallback ); + if ( $fallback === null ) { + throw new MWException( 'CACHE_ACCEL requested but no suitable object ' . + 'cache is present. You may want to install APC.' ); } - throw new MWException( "CACHE_ACCEL requested but no suitable object " . - "cache is present. You may want to install APC." ); + $id = $fallback; } return self::newFromId( $id ); } @@ -190,7 +184,6 @@ class ObjectCache { * switching between the two clients randomly would be disastrous. * * @param array $params - * * @return MemcachedPhpBagOStuff */ static function newMemcached( $params ) { @@ -200,11 +193,10 @@ class ObjectCache { /** * Create a new cache object of the specified type * + * @since 1.26 * @param string $id - * - * @throws MWException * @return WANObjectCache - * @since 1.26 + * @throws MWException */ static function newWANCacheFromId( $id ) { global $wgWANObjectCaches; @@ -226,8 +218,8 @@ class ObjectCache { /** * Get the main WAN cache object * - * @return WANObjectCache * @since 1.26 + * @return WANObjectCache */ static function getMainWANInstance() { global $wgMainWANCache; @@ -247,9 +239,8 @@ class ObjectCache { * avoiding an assumption of perfect serializability (or accepting anomalies). * Reads may be eventually consistent or data might rollback as nodes flap. * - * - * @return BagOStuff * @since 1.26 + * @return BagOStuff */ static function getMainStashInstance() { global $wgMainStash; -- 2.20.1