MediaWiki  1.27.1
ObjectCache.php
Go to the documentation of this file.
1 <?php
26 
80 class ObjectCache {
82  public static $instances = [];
84  public static $wanInstances = [];
85 
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 
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 
122  public static function newFromId( $id ) {
124 
125  if ( !isset( $wgObjectCaches[$id] ) ) {
126  throw new MWException( "Invalid object cache type \"$id\" requested. " .
127  "It is not present in \$wgObjectCaches." );
128  }
129 
130  return self::newFromParams( $wgObjectCaches[$id] );
131  }
132 
142  public static function getDefaultKeyspace() {
143  global $wgCachePrefix;
144 
145  $keyspace = $wgCachePrefix;
146  if ( is_string( $keyspace ) && $keyspace !== '' ) {
147  return $keyspace;
148  }
149 
150  return wfWikiID();
151  }
152 
164  public static function newFromParams( $params ) {
165  if ( isset( $params['loggroup'] ) ) {
166  $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
167  } else {
168  $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
169  }
170  if ( !isset( $params['keyspace'] ) ) {
171  $params['keyspace'] = self::getDefaultKeyspace();
172  }
173  if ( isset( $params['factory'] ) ) {
174  return call_user_func( $params['factory'], $params );
175  } elseif ( isset( $params['class'] ) ) {
176  $class = $params['class'];
177  // Automatically set the 'async' update handler
178  $params['asyncHandler'] = isset( $params['asyncHandler'] )
179  ? $params['asyncHandler']
180  : 'DeferredUpdates::addCallableUpdate';
181  // Enable reportDupes by default
182  $params['reportDupes'] = isset( $params['reportDupes'] )
183  ? $params['reportDupes']
184  : true;
185  // Do b/c logic for MemcachedBagOStuff
186  if ( is_subclass_of( $class, 'MemcachedBagOStuff' ) ) {
187  if ( !isset( $params['servers'] ) ) {
188  $params['servers'] = $GLOBALS['wgMemCachedServers'];
189  }
190  if ( !isset( $params['debug'] ) ) {
191  $params['debug'] = $GLOBALS['wgMemCachedDebug'];
192  }
193  if ( !isset( $params['persistent'] ) ) {
194  $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
195  }
196  if ( !isset( $params['timeout'] ) ) {
197  $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
198  }
199  }
200  return new $class( $params );
201  } else {
202  throw new MWException( "The definition of cache type \""
203  . print_r( $params, true ) . "\" lacks both "
204  . "factory and class parameters." );
205  }
206  }
207 
221  public static function newAnything( $params ) {
224  foreach ( $candidates as $candidate ) {
225  if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
226  return self::getInstance( $candidate );
227  }
228  }
229  return self::getInstance( CACHE_DB );
230  }
231 
249  public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
250  if ( function_exists( 'apc_fetch' ) ) {
251  $id = 'apc';
252  } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
253  $id = 'xcache';
254  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
255  $id = 'wincache';
256  } else {
257  if ( is_array( $fallback ) ) {
258  $id = isset( $fallback['fallback'] ) ? $fallback['fallback'] : CACHE_NONE;
259  } else {
260  $id = $fallback;
261  }
262  }
263 
264  return self::getInstance( $id );
265  }
266 
273  public static function newAccelerator( $params = [], $fallback = null ) {
274  if ( $fallback === null ) {
275  if ( is_array( $params ) && isset( $params['fallback'] ) ) {
276  $fallback = $params['fallback'];
277  } elseif ( !is_array( $params ) ) {
278  $fallback = $params;
279  }
280  }
281 
282  return self::getLocalServerInstance( $fallback );
283  }
284 
293  public static function newWANCacheFromId( $id ) {
295 
296  if ( !isset( $wgWANObjectCaches[$id] ) ) {
297  throw new MWException( "Invalid object cache type \"$id\" requested. " .
298  "It is not present in \$wgWANObjectCaches." );
299  }
300 
301  $params = $wgWANObjectCaches[$id];
302  foreach ( $params['channels'] as $action => $channel ) {
303  $params['relayers'][$action] = MediaWikiServices::getInstance()->getEventRelayerGroup()
304  ->getRelayer( $channel );
305  $params['channels'][$action] = $channel;
306  }
307  $params['cache'] = self::newFromId( $params['cacheId'] );
308  if ( isset( $params['loggroup'] ) ) {
309  $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
310  } else {
311  $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
312  }
313  $class = $params['class'];
314 
315  return new $class( $params );
316  }
317 
324  public static function getLocalClusterInstance() {
326 
327  return self::getInstance( $wgMainCacheType );
328  }
329 
336  public static function getMainWANInstance() {
338 
339  return self::getWANInstance( $wgMainWANCache );
340  }
341 
360  public static function getMainStashInstance() {
362 
363  return self::getInstance( $wgMainStash );
364  }
365 
369  public static function clear() {
370  self::$instances = [];
371  self::$wanInstances = [];
372  }
373 }
static getMainWANInstance()
Get the main WAN cache object.
$wgMainStash
Main object stash type.
static newWANCacheFromId($id)
Create a new cache object of the specified type.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getInstance($id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:92
static getLocalClusterInstance()
Get the main cluster-local cache object.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
$wgMainWANCache
Main Wide-Area-Network cache type.
static getMainStashInstance()
Get the cache object for the main stash.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
$wgWANObjectCaches
Advanced WAN object cache configuration.
static BagOStuff[] $instances
Map of (id => BagOStuff)
Definition: ObjectCache.php:82
$wgParserCacheType
The cache type for storing article HTML.
static newFromParams($params)
Create a new cache object from parameters.
$GLOBALS['IP']
wfIniGetBool($setting)
Safety wrapper around ini_get() for boolean settings.
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
static newAccelerator($params=[], $fallback=null)
$params
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
static getDefaultKeyspace()
Get the default keyspace for this wiki.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
static WANObjectCache[] $wanInstances
Map of (id => WANObjectCache)
Definition: ObjectCache.php:84
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$fallback
Definition: MessagesAb.php:11
static newAnything($params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
static getWANInstance($id)
Get a cached instance of the specified type of WAN cache object.
static getLocalServerInstance($fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
const CACHE_ANYTHING
Definition: Defines.php:101
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method.MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances.The"Spi"in MediaWiki\Logger\Spi stands for"service provider interface".An SPI is an API intended to be implemented or extended by a third party.This software design pattern is intended to enable framework extension and replaceable components.It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki.The service provider interface allows the backend logging library to be implemented in multiple ways.The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime.This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance.Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
$wgObjectCaches
Advanced object cache configuration.
static clear()
Clear all the cached instances.
static newFromId($id)
Create a new cache object of the specified type.
const CACHE_NONE
Definition: Defines.php:102
Functions to get cache objects.
Definition: ObjectCache.php:80
const CACHE_DB
Definition: Defines.php:103