Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ObjectCache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ObjectCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 80 | class ObjectCache { |
||
| 81 | /** @var BagOStuff[] Map of (id => BagOStuff) */ |
||
| 82 | public static $instances = []; |
||
| 83 | /** @var WANObjectCache[] Map of (id => WANObjectCache) */ |
||
| 84 | public static $wanInstances = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get a cached instance of the specified type of cache object. |
||
| 88 | * |
||
| 89 | * @param string $id A key in $wgObjectCaches. |
||
| 90 | * @return BagOStuff |
||
| 91 | */ |
||
| 92 | public static function getInstance( $id ) { |
||
| 93 | if ( !isset( self::$instances[$id] ) ) { |
||
| 94 | self::$instances[$id] = self::newFromId( $id ); |
||
| 95 | } |
||
| 96 | |||
| 97 | return self::$instances[$id]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a cached instance of the specified type of WAN cache object. |
||
| 102 | * |
||
| 103 | * @since 1.26 |
||
| 104 | * @param string $id A key in $wgWANObjectCaches. |
||
| 105 | * @return WANObjectCache |
||
| 106 | */ |
||
| 107 | public static function getWANInstance( $id ) { |
||
| 108 | if ( !isset( self::$wanInstances[$id] ) ) { |
||
| 109 | self::$wanInstances[$id] = self::newWANCacheFromId( $id ); |
||
| 110 | } |
||
| 111 | |||
| 112 | return self::$wanInstances[$id]; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Create a new cache object of the specified type. |
||
| 117 | * |
||
| 118 | * @param string $id A key in $wgObjectCaches. |
||
| 119 | * @return BagOStuff |
||
| 120 | * @throws InvalidArgumentException |
||
| 121 | */ |
||
| 122 | public static function newFromId( $id ) { |
||
| 123 | global $wgObjectCaches; |
||
| 124 | |||
| 125 | if ( !isset( $wgObjectCaches[$id] ) ) { |
||
| 126 | // Always recognize these ones |
||
| 127 | if ( $id === CACHE_NONE ) { |
||
|
|
|||
| 128 | return new EmptyBagOStuff(); |
||
| 129 | } elseif ( $id === 'hash' ) { |
||
| 130 | return new HashBagOStuff(); |
||
| 131 | } |
||
| 132 | |||
| 133 | throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " . |
||
| 134 | "It is not present in \$wgObjectCaches." ); |
||
| 135 | } |
||
| 136 | |||
| 137 | return self::newFromParams( $wgObjectCaches[$id] ); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the default keyspace for this wiki. |
||
| 142 | * |
||
| 143 | * This is either the value of the `CachePrefix` configuration variable, |
||
| 144 | * or (if the former is unset) the `DBname` configuration variable, with |
||
| 145 | * `DBprefix` (if defined). |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public static function getDefaultKeyspace() { |
||
| 150 | global $wgCachePrefix; |
||
| 151 | |||
| 152 | $keyspace = $wgCachePrefix; |
||
| 153 | if ( is_string( $keyspace ) && $keyspace !== '' ) { |
||
| 154 | return $keyspace; |
||
| 155 | } |
||
| 156 | |||
| 157 | return wfWikiID(); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Create a new cache object from parameters. |
||
| 162 | * |
||
| 163 | * @param array $params Must have 'factory' or 'class' property. |
||
| 164 | * - factory: Callback passed $params that returns BagOStuff. |
||
| 165 | * - class: BagOStuff subclass constructed with $params. |
||
| 166 | * - loggroup: Alias to set 'logger' key with LoggerFactory group. |
||
| 167 | * - .. Other parameters passed to factory or class. |
||
| 168 | * @return BagOStuff |
||
| 169 | * @throws InvalidArgumentException |
||
| 170 | */ |
||
| 171 | public static function newFromParams( $params ) { |
||
| 172 | View Code Duplication | if ( isset( $params['loggroup'] ) ) { |
|
| 173 | $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] ); |
||
| 174 | } else { |
||
| 175 | $params['logger'] = LoggerFactory::getInstance( 'objectcache' ); |
||
| 176 | } |
||
| 177 | if ( !isset( $params['keyspace'] ) ) { |
||
| 178 | $params['keyspace'] = self::getDefaultKeyspace(); |
||
| 179 | } |
||
| 180 | if ( isset( $params['factory'] ) ) { |
||
| 181 | return call_user_func( $params['factory'], $params ); |
||
| 182 | } elseif ( isset( $params['class'] ) ) { |
||
| 183 | $class = $params['class']; |
||
| 184 | // Automatically set the 'async' update handler |
||
| 185 | $params['asyncHandler'] = isset( $params['asyncHandler'] ) |
||
| 186 | ? $params['asyncHandler'] |
||
| 187 | : 'DeferredUpdates::addCallableUpdate'; |
||
| 188 | // Enable reportDupes by default |
||
| 189 | $params['reportDupes'] = isset( $params['reportDupes'] ) |
||
| 190 | ? $params['reportDupes'] |
||
| 191 | : true; |
||
| 192 | // Do b/c logic for SqlBagOStuff |
||
| 193 | if ( is_a( $class, SqlBagOStuff::class, true ) ) { |
||
| 194 | if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) { |
||
| 195 | $params['servers'] = [ $params['server'] ]; |
||
| 196 | unset( $params['server'] ); |
||
| 197 | } |
||
| 198 | // In the past it was not required to set 'dbDirectory' in $wgObjectCaches |
||
| 199 | if ( isset( $params['servers'] ) ) { |
||
| 200 | foreach ( $params['servers'] as &$server ) { |
||
| 201 | if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) { |
||
| 202 | $server['dbDirectory'] = MediaWikiServices::getInstance() |
||
| 203 | ->getMainConfig()->get( 'SQLiteDataDir' ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Do b/c logic for MemcachedBagOStuff |
||
| 210 | if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) { |
||
| 211 | if ( !isset( $params['servers'] ) ) { |
||
| 212 | $params['servers'] = $GLOBALS['wgMemCachedServers']; |
||
| 213 | } |
||
| 214 | if ( !isset( $params['debug'] ) ) { |
||
| 215 | $params['debug'] = $GLOBALS['wgMemCachedDebug']; |
||
| 216 | } |
||
| 217 | if ( !isset( $params['persistent'] ) ) { |
||
| 218 | $params['persistent'] = $GLOBALS['wgMemCachedPersistent']; |
||
| 219 | } |
||
| 220 | if ( !isset( $params['timeout'] ) ) { |
||
| 221 | $params['timeout'] = $GLOBALS['wgMemCachedTimeout']; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | return new $class( $params ); |
||
| 225 | } else { |
||
| 226 | throw new InvalidArgumentException( "The definition of cache type \"" |
||
| 227 | . print_r( $params, true ) . "\" lacks both " |
||
| 228 | . "factory and class parameters." ); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php) |
||
| 234 | * |
||
| 235 | * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option. |
||
| 236 | * If a caching method is configured for any of the main caches ($wgMainCacheType, |
||
| 237 | * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively |
||
| 238 | * be an alias to the configured cache choice for that. |
||
| 239 | * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE), |
||
| 240 | * then CACHE_ANYTHING will forward to CACHE_DB. |
||
| 241 | * |
||
| 242 | * @param array $params |
||
| 243 | * @return BagOStuff |
||
| 244 | */ |
||
| 245 | public static function newAnything( $params ) { |
||
| 246 | global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType; |
||
| 247 | $candidates = [ $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType ]; |
||
| 248 | foreach ( $candidates as $candidate ) { |
||
| 249 | if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) { |
||
| 250 | return self::getInstance( $candidate ); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) { |
||
| 255 | // The LoadBalancer is disabled, probably because |
||
| 256 | // MediaWikiServices::disableStorageBackend was called. |
||
| 257 | $candidate = CACHE_NONE; |
||
| 258 | } else { |
||
| 259 | $candidate = CACHE_DB; |
||
| 260 | } |
||
| 261 | |||
| 262 | return self::getInstance( $candidate ); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Factory function for CACHE_ACCEL (referenced from DefaultSettings.php) |
||
| 267 | * |
||
| 268 | * This will look for any APC or APCu style server-local cache. |
||
| 269 | * A fallback cache can be specified if none is found. |
||
| 270 | * |
||
| 271 | * // Direct calls |
||
| 272 | * ObjectCache::getLocalServerInstance( $fallbackType ); |
||
| 273 | * |
||
| 274 | * // From $wgObjectCaches via newFromParams() |
||
| 275 | * ObjectCache::getLocalServerInstance( [ 'fallback' => $fallbackType ] ); |
||
| 276 | * |
||
| 277 | * @param int|string|array $fallback Fallback cache or parameter map with 'fallback' |
||
| 278 | * @return BagOStuff |
||
| 279 | * @throws InvalidArgumentException |
||
| 280 | * @since 1.27 |
||
| 281 | */ |
||
| 282 | public static function getLocalServerInstance( $fallback = CACHE_NONE ) { |
||
| 283 | $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
||
| 284 | if ( $cache instanceof EmptyBagOStuff ) { |
||
| 285 | if ( is_array( $fallback ) ) { |
||
| 286 | $fallback = isset( $fallback['fallback'] ) ? $fallback['fallback'] : CACHE_NONE; |
||
| 287 | } |
||
| 288 | $cache = self::getInstance( $fallback ); |
||
| 289 | } |
||
| 290 | |||
| 291 | return $cache; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $params [optional] Array key 'fallback' for $fallback. |
||
| 296 | * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24) |
||
| 297 | * @return BagOStuff |
||
| 298 | * @deprecated since 1.27 |
||
| 299 | */ |
||
| 300 | public static function newAccelerator( $params = [], $fallback = null ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Create a new cache object of the specified type. |
||
| 314 | * |
||
| 315 | * @since 1.26 |
||
| 316 | * @param string $id A key in $wgWANObjectCaches. |
||
| 317 | * @return WANObjectCache |
||
| 318 | * @throws UnexpectedValueException |
||
| 319 | */ |
||
| 320 | public static function newWANCacheFromId( $id ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Create a new cache object of the specified type. |
||
| 340 | * |
||
| 341 | * @since 1.28 |
||
| 342 | * @param array $params |
||
| 343 | * @return WANObjectCache |
||
| 344 | * @throws UnexpectedValueException |
||
| 345 | */ |
||
| 346 | public static function newWANCacheFromParams( array $params ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the main cluster-local cache object. |
||
| 365 | * |
||
| 366 | * @since 1.27 |
||
| 367 | * @return BagOStuff |
||
| 368 | */ |
||
| 369 | public static function getLocalClusterInstance() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get the main WAN cache object. |
||
| 377 | * |
||
| 378 | * @since 1.26 |
||
| 379 | * @return WANObjectCache |
||
| 380 | * @deprecated Since 1.28 Use MediaWikiServices::getMainWANCache() |
||
| 381 | */ |
||
| 382 | public static function getMainWANInstance() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the cache object for the main stash. |
||
| 388 | * |
||
| 389 | * Stash objects are BagOStuff instances suitable for storing light |
||
| 390 | * weight data that is not canonically stored elsewhere (such as RDBMS). |
||
| 391 | * Stashes should be configured to propagate changes to all data-centers. |
||
| 392 | * |
||
| 393 | * Callers should be prepared for: |
||
| 394 | * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs |
||
| 395 | * - b) Reads to be eventually consistent, e.g. for get()/getMulti() |
||
| 396 | * In general, this means avoiding updates on idempotent HTTP requests and |
||
| 397 | * avoiding an assumption of perfect serializability (or accepting anomalies). |
||
| 398 | * Reads may be eventually consistent or data might rollback as nodes flap. |
||
| 399 | * Callers can use BagOStuff:READ_LATEST to see the latest available data. |
||
| 400 | * |
||
| 401 | * @return BagOStuff |
||
| 402 | * @since 1.26 |
||
| 403 | * @deprecated Since 1.28 Use MediaWikiServices::getMainObjectStash |
||
| 404 | */ |
||
| 405 | public static function getMainStashInstance() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Clear all the cached instances. |
||
| 411 | */ |
||
| 412 | public static function clear() { |
||
| 416 | } |
||
| 417 |