MediaWiki  1.34.0
ObjectCache.php
Go to the documentation of this file.
1 <?php
26 
68 class ObjectCache {
70  public static $instances = [];
72  public static $wanInstances = [];
73 
80  public static function getInstance( $id ) {
81  if ( !isset( self::$instances[$id] ) ) {
82  self::$instances[$id] = self::newFromId( $id );
83  }
84 
85  return self::$instances[$id];
86  }
87 
96  public static function getWANInstance( $id ) {
97  wfDeprecated( __METHOD__, '1.34' );
98  if ( !isset( self::$wanInstances[$id] ) ) {
99  self::$wanInstances[$id] = self::newWANCacheFromId( $id );
100  }
101 
102  return self::$wanInstances[$id];
103  }
104 
112  private static function newFromId( $id ) {
113  global $wgObjectCaches;
114 
115  if ( !isset( $wgObjectCaches[$id] ) ) {
116  // Always recognize these ones
117  if ( $id === CACHE_NONE ) {
118  return new EmptyBagOStuff();
119  } elseif ( $id === 'hash' ) {
120  return new HashBagOStuff();
121  }
122 
123  throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " .
124  "It is not present in \$wgObjectCaches." );
125  }
126 
127  return self::newFromParams( $wgObjectCaches[$id] );
128  }
129 
139  private static function getDefaultKeyspace() {
140  global $wgCachePrefix;
141 
142  $keyspace = $wgCachePrefix;
143  if ( is_string( $keyspace ) && $keyspace !== '' ) {
144  return $keyspace;
145  }
146 
147  return WikiMap::getCurrentWikiDbDomain()->getId();
148  }
149 
161  public static function newFromParams( $params ) {
162  $params['logger'] = $params['logger'] ??
163  LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' );
164  if ( !isset( $params['keyspace'] ) ) {
165  $params['keyspace'] = self::getDefaultKeyspace();
166  }
167  if ( isset( $params['factory'] ) ) {
168  return call_user_func( $params['factory'], $params );
169  } elseif ( isset( $params['class'] ) ) {
170  $class = $params['class'];
171  // Automatically set the 'async' update handler
172  $params['asyncHandler'] = $params['asyncHandler']
173  ?? [ DeferredUpdates::class, 'addCallableUpdate' ];
174  // Enable reportDupes by default
175  $params['reportDupes'] = $params['reportDupes'] ?? true;
176  // Do b/c logic for SqlBagOStuff
177  if ( is_a( $class, SqlBagOStuff::class, true ) ) {
178  if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
179  $params['servers'] = [ $params['server'] ];
180  unset( $params['server'] );
181  }
182  // In the past it was not required to set 'dbDirectory' in $wgObjectCaches
183  if ( isset( $params['servers'] ) ) {
184  foreach ( $params['servers'] as &$server ) {
185  if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) {
186  $server['dbDirectory'] = MediaWikiServices::getInstance()
187  ->getMainConfig()->get( 'SQLiteDataDir' );
188  }
189  }
190  }
191  }
192 
193  // Do b/c logic for MemcachedBagOStuff
194  if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
195  if ( !isset( $params['servers'] ) ) {
196  $params['servers'] = $GLOBALS['wgMemCachedServers'];
197  }
198  if ( !isset( $params['persistent'] ) ) {
199  $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
200  }
201  if ( !isset( $params['timeout'] ) ) {
202  $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
203  }
204  }
205  return new $class( $params );
206  } else {
207  throw new InvalidArgumentException( "The definition of cache type \""
208  . print_r( $params, true ) . "\" lacks both "
209  . "factory and class parameters." );
210  }
211  }
212 
226  public static function newAnything( $params ) {
229  foreach ( $candidates as $candidate ) {
230  if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
231  $cache = self::getInstance( $candidate );
232  // CACHE_ACCEL might default to nothing if no APCu
233  // See includes/ServiceWiring.php
234  if ( !( $cache instanceof EmptyBagOStuff ) ) {
235  return $cache;
236  }
237  }
238  }
239 
240  if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
241  // The LoadBalancer is disabled, probably because
242  // MediaWikiServices::disableStorageBackend was called.
243  $candidate = CACHE_NONE;
244  } else {
245  $candidate = CACHE_DB;
246  }
247 
248  return self::getInstance( $candidate );
249  }
250 
268  public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
269  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
270  if ( $cache instanceof EmptyBagOStuff ) {
271  if ( is_array( $fallback ) ) {
272  $fallback = $fallback['fallback'] ?? CACHE_NONE;
273  }
275  }
276 
277  return $cache;
278  }
279 
288  private static function newWANCacheFromId( $id ) {
290 
291  if ( !isset( $wgWANObjectCaches[$id] ) ) {
292  throw new UnexpectedValueException(
293  "Cache type \"$id\" requested is not present in \$wgWANObjectCaches." );
294  }
295 
296  $params = $wgWANObjectCaches[$id];
297  if ( !isset( $wgObjectCaches[$params['cacheId']] ) ) {
298  throw new UnexpectedValueException(
299  "Cache type \"{$params['cacheId']}\" is not present in \$wgObjectCaches." );
300  }
301  $params['store'] = $wgObjectCaches[$params['cacheId']];
302 
303  return self::newWANCacheFromParams( $params );
304  }
305 
317  public static function newWANCacheFromParams( array $params ) {
318  wfDeprecated( __METHOD__, '1.34' );
320 
321  $services = MediaWikiServices::getInstance();
322  $params['cache'] = self::newFromParams( $params['store'] );
323  $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' );
324  if ( !$wgCommandLineMode ) {
325  // Send the statsd data post-send on HTTP requests; avoid in CLI mode (T181385)
326  $params['stats'] = $services->getStatsdDataFactory();
327  // Let pre-emptive refreshes happen post-send on HTTP requests
328  $params['asyncHandler'] = [ DeferredUpdates::class, 'addCallableUpdate' ];
329  }
330  $params['secret'] = $params['secret'] ?? $wgSecretKey;
331  $class = $params['class'];
332 
333  return new $class( $params );
334  }
335 
342  public static function getLocalClusterInstance() {
343  global $wgMainCacheType;
344 
346  }
347 
351  public static function clear() {
352  self::$instances = [];
353  self::$wanInstances = [];
354  }
355 
362  public static function detectLocalServerCache() {
363  if ( function_exists( 'apcu_fetch' ) ) {
364  // Make sure the APCu methods actually store anything
365  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
366  return 'apcu';
367  }
368  } elseif ( function_exists( 'apc_fetch' ) ) {
369  // Make sure the APC methods actually store anything
370  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
371  return 'apc';
372  }
373  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
374  return 'wincache';
375  }
376 
377  return CACHE_NONE;
378  }
379 }
ObjectCache\clear
static clear()
Clear all the cached instances.
Definition: ObjectCache.php:351
ObjectCache\newFromId
static newFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:112
ObjectCache
Functions to get cache objects.
Definition: ObjectCache.php:68
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:342
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ObjectCache\detectLocalServerCache
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it.
Definition: ObjectCache.php:362
CACHE_NONE
const CACHE_NONE
Definition: Defines.php:82
$fallback
$fallback
Definition: MessagesAb.php:11
$wgWANObjectCaches
$wgWANObjectCaches
Advanced WAN object cache configuration.
Definition: DefaultSettings.php:2454
ObjectCache\$instances
static BagOStuff[] $instances
Map of (id => BagOStuff)
Definition: ObjectCache.php:70
$wgMessageCacheType
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
Definition: DefaultSettings.php:2353
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:63
ObjectCache\newAnything
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
Definition: ObjectCache.php:226
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgMainCacheType
$wgMainCacheType
Main cache type.
Definition: DefaultSettings.php:2345
ObjectCache\$wanInstances
static WANObjectCache[] $wanInstances
Map of (id => WANObjectCache)
Definition: ObjectCache.php:72
$wgCachePrefix
$wgCachePrefix
Overwrite the caching key prefix with custom value.
Definition: DefaultSettings.php:6501
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:28
ObjectCache\newWANCacheFromParams
static newWANCacheFromParams(array $params)
Create a new cache object of the specified type.
Definition: ObjectCache.php:317
$wgObjectCaches
$wgObjectCaches
Advanced object cache configuration.
Definition: DefaultSettings.php:2393
ObjectCache\getWANInstance
static getWANInstance( $id)
Get a cached instance of the specified type of WAN cache object.
Definition: ObjectCache.php:96
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:80
$wgParserCacheType
$wgParserCacheType
The cache type for storing article HTML.
Definition: DefaultSettings.php:2361
ObjectCache\newWANCacheFromId
static newWANCacheFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:288
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:81
ObjectCache\getDefaultKeyspace
static getDefaultKeyspace()
Get the default keyspace for this wiki.
Definition: ObjectCache.php:139
$cache
$cache
Definition: mcc.php:33
ObjectCache\newFromParams
static newFromParams( $params)
Create a new cache object from parameters.
Definition: ObjectCache.php:161
$wgSecretKey
$wgSecretKey
This should always be customised in LocalSettings.php.
Definition: DefaultSettings.php:5989
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
CACHE_DB
const CACHE_DB
Definition: Defines.php:83
ObjectCache\getLocalServerInstance
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
Definition: ObjectCache.php:268