MediaWiki  1.34.0
ObjectCache Class Reference

Functions to get cache objects. More...

Collaboration diagram for ObjectCache:

Static Public Member Functions

static clear ()
 Clear all the cached instances. More...
 
static detectLocalServerCache ()
 Detects which local server cache library is present and returns a configuration for it. More...
 
static getInstance ( $id)
 Get a cached instance of the specified type of cache object. More...
 
static getLocalClusterInstance ()
 Get the main cluster-local cache object. More...
 
static getLocalServerInstance ( $fallback=CACHE_NONE)
 Factory function for CACHE_ACCEL (referenced from DefaultSettings.php) More...
 
static getWANInstance ( $id)
 Get a cached instance of the specified type of WAN cache object. More...
 
static newAnything ( $params)
 Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php) More...
 
static newFromParams ( $params)
 Create a new cache object from parameters. More...
 
static newWANCacheFromParams (array $params)
 Create a new cache object of the specified type. More...
 

Static Public Attributes

static BagOStuff[] $instances = []
 Map of (id => BagOStuff) More...
 
static WANObjectCache[] $wanInstances = []
 Map of (id => WANObjectCache) More...
 

Static Private Member Functions

static getDefaultKeyspace ()
 Get the default keyspace for this wiki. More...
 
static newFromId ( $id)
 Create a new cache object of the specified type. More...
 
static newWANCacheFromId ( $id)
 Create a new cache object of the specified type. More...
 

Detailed Description

Functions to get cache objects.

The word "cache" has two main dictionary meanings, and both are used in this factory class. They are:

  • a) Cache (the computer science definition). A place to store copies or computations on existing data for higher access speeds.
  • b) Storage. A place to store lightweight data that is not canonically stored anywhere else (e.g. a "hoard" of objects).

The former should always use strongly consistent stores, so callers don't have to deal with stale reads. The latter may be eventually consistent, but callers can use BagOStuff:READ_LATEST to see the latest available data.

Primary entry points:

  • ObjectCache::getLocalServerInstance( $fallbackType ) Purpose: Memory cache for very hot keys. 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.
  • ObjectCache::getLocalClusterInstance() Purpose: Memory storage for per-cluster coordination and tracking. A typical use case would be a rate limit counter or cache regeneration mutex. Stored centrally within the local data-center. Not replicated to other DCs. Configured by $wgMainCacheType.
  • ObjectCache::getInstance( $cacheType ) Purpose: Special cases (like tiered memory/disk caches). Get a specific cache type by key in $wgObjectCaches.

All the above cache instances (BagOStuff and WANObjectCache) have their makeKey() method scoped to the current wiki ID. Use makeGlobalKey() to avoid this scoping when using keys that need to be shared amongst wikis.

Definition at line 68 of file ObjectCache.php.

Member Function Documentation

◆ clear()

static ObjectCache::clear ( )
static

Clear all the cached instances.

Definition at line 351 of file ObjectCache.php.

Referenced by MediaWiki\MediaWikiServices\disableStorageBackend(), and ForkController\prepareEnvironment().

◆ detectLocalServerCache()

static ObjectCache::detectLocalServerCache ( )
static

Detects which local server cache library is present and returns a configuration for it.

Since
1.32
Returns
int|string Index to cache in $wgObjectCaches

Definition at line 362 of file ObjectCache.php.

References CACHE_NONE.

Referenced by ExtensionRegistry\loadFromQueue().

◆ getDefaultKeyspace()

static ObjectCache::getDefaultKeyspace ( )
staticprivate

Get the default keyspace for this wiki.

This is either the value of the CachePrefix configuration variable, or (if the former is unset) the DBname configuration variable, with DBprefix (if defined).

Returns
string

Definition at line 139 of file ObjectCache.php.

References $wgCachePrefix, and WikiMap\getCurrentWikiDbDomain().

Referenced by newFromParams().

◆ getInstance()

static ObjectCache::getInstance (   $id)
static

◆ getLocalClusterInstance()

◆ getLocalServerInstance()

static ObjectCache::getLocalServerInstance (   $fallback = CACHE_NONE)
static

Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)

This will look for any APC or APCu style server-local cache. A fallback cache can be specified if none is found.

// Direct calls
ObjectCache::getLocalServerInstance( $fallbackType );

// From $wgObjectCaches via newFromParams()
ObjectCache::getLocalServerInstance( [ 'fallback' => $fallbackType ] );
Parameters
int | string | array$fallbackFallback cache or parameter map with 'fallback'
Returns
BagOStuff
Exceptions
InvalidArgumentException
Since
1.27

Definition at line 268 of file ObjectCache.php.

References $cache, $fallback, CACHE_NONE, and getInstance().

Referenced by FileContentsHasher\__construct(), ResourceLoaderFileModule\compileLessFile(), FileBackendGroup\config(), IcuCollation\getFirstLetterData(), MediaWikiGadgetsDefinitionRepo\loadGadgets(), Scribunto_LuaEngine\loadLibraryFromFile(), and ScribuntoHooks\reportTiming().

◆ getWANInstance()

static ObjectCache::getWANInstance (   $id)
static

Get a cached instance of the specified type of WAN cache object.

Since
1.26
Parameters
string$idA key in $wgWANObjectCaches.
Returns
WANObjectCache
Deprecated:
since 1.34 Use MediaWikiServices::getMainWANObjectCache instead

Definition at line 96 of file ObjectCache.php.

References newWANCacheFromId(), and wfDeprecated().

◆ newAnything()

static ObjectCache::newAnything (   $params)
static

Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)

CACHE_ANYTHING means that stuff has to be cached, not caching is not an option. If a caching method is configured for any of the main caches ($wgMainCacheType, $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively 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.

Parameters
array$params
Returns
BagOStuff

Definition at line 226 of file ObjectCache.php.

References $cache, $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, CACHE_ANYTHING, CACHE_DB, CACHE_NONE, and getInstance().

◆ newFromId()

static ObjectCache::newFromId (   $id)
staticprivate

Create a new cache object of the specified type.

Parameters
string$idA key in $wgObjectCaches.
Returns
BagOStuff
Exceptions
InvalidArgumentException

Definition at line 112 of file ObjectCache.php.

References $wgObjectCaches, CACHE_NONE, and newFromParams().

Referenced by getInstance().

◆ newFromParams()

static ObjectCache::newFromParams (   $params)
static

Create a new cache object from parameters.

Parameters
array$paramsMust have 'factory' or 'class' property.
  • factory: Callback passed $params that returns BagOStuff.
  • class: BagOStuff subclass constructed with $params.
  • loggroup: Alias to set 'logger' key with LoggerFactory group.
  • .. Other parameters passed to factory or class.
Returns
BagOStuff
Exceptions
InvalidArgumentException

Definition at line 161 of file ObjectCache.php.

References $GLOBALS, and getDefaultKeyspace().

Referenced by ExtensionRegistry\loadFromQueue(), newFromId(), and newWANCacheFromParams().

◆ newWANCacheFromId()

static ObjectCache::newWANCacheFromId (   $id)
staticprivate

Create a new cache object of the specified type.

Since
1.26
Parameters
string$idA key in $wgWANObjectCaches.
Returns
WANObjectCache
Exceptions
UnexpectedValueException

Definition at line 288 of file ObjectCache.php.

References $wgObjectCaches, $wgWANObjectCaches, and newWANCacheFromParams().

Referenced by getWANInstance().

◆ newWANCacheFromParams()

static ObjectCache::newWANCacheFromParams ( array  $params)
static

Create a new cache object of the specified type.

Since
1.28
Parameters
array$params
Returns
WANObjectCache
Exceptions
UnexpectedValueExceptionPhanTypeMismatchReturn
Deprecated:
since 1.34 Use MediaWikiServices::getMainWANObjectCache instead or use WANObjectCache::__construct directly

Definition at line 317 of file ObjectCache.php.

References $wgCommandLineMode, $wgSecretKey, newFromParams(), and wfDeprecated().

Referenced by newWANCacheFromId().

Member Data Documentation

◆ $instances

BagOStuff [] ObjectCache::$instances = []
static

Map of (id => BagOStuff)

Definition at line 70 of file ObjectCache.php.

Referenced by CaptchaPreAuthenticationProviderTest\setUp().

◆ $wanInstances

WANObjectCache [] ObjectCache::$wanInstances = []
static

Map of (id => WANObjectCache)

Definition at line 72 of file ObjectCache.php.


The documentation for this class was generated from the following file: