MediaWiki REL1_32
ObjectCache.php
Go to the documentation of this file.
1<?php
26
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 ) {
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
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
171 public static function newFromParams( $params ) {
172 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'] = $params['asyncHandler'] ?? 'DeferredUpdates::addCallableUpdate';
186 // Enable reportDupes by default
187 $params['reportDupes'] = $params['reportDupes'] ?? true;
188 // Do b/c logic for SqlBagOStuff
189 if ( is_a( $class, SqlBagOStuff::class, true ) ) {
190 if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
191 $params['servers'] = [ $params['server'] ];
192 unset( $params['server'] );
193 }
194 // In the past it was not required to set 'dbDirectory' in $wgObjectCaches
195 if ( isset( $params['servers'] ) ) {
196 foreach ( $params['servers'] as &$server ) {
197 if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) {
198 $server['dbDirectory'] = MediaWikiServices::getInstance()
199 ->getMainConfig()->get( 'SQLiteDataDir' );
200 }
201 }
202 }
203 }
204
205 // Do b/c logic for MemcachedBagOStuff
206 if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
207 if ( !isset( $params['servers'] ) ) {
208 $params['servers'] = $GLOBALS['wgMemCachedServers'];
209 }
210 if ( !isset( $params['debug'] ) ) {
211 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
212 }
213 if ( !isset( $params['persistent'] ) ) {
214 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
215 }
216 if ( !isset( $params['timeout'] ) ) {
217 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
218 }
219 }
220 return new $class( $params );
221 } else {
222 throw new InvalidArgumentException( "The definition of cache type \""
223 . print_r( $params, true ) . "\" lacks both "
224 . "factory and class parameters." );
225 }
226 }
227
241 public static function newAnything( $params ) {
244 foreach ( $candidates as $candidate ) {
245 $cache = false;
246 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
247 $cache = self::getInstance( $candidate );
248 // CACHE_ACCEL might default to nothing if no APCu
249 // See includes/ServiceWiring.php
250 if ( !( $cache instanceof EmptyBagOStuff ) ) {
251 return $cache;
252 }
253 }
254 }
255
256 if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
257 // The LoadBalancer is disabled, probably because
258 // MediaWikiServices::disableStorageBackend was called.
259 $candidate = CACHE_NONE;
260 } else {
261 $candidate = CACHE_DB;
262 }
263
264 return self::getInstance( $candidate );
265 }
266
284 public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
285 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
286 if ( $cache instanceof EmptyBagOStuff ) {
287 if ( is_array( $fallback ) ) {
288 $fallback = $fallback['fallback'] ?? CACHE_NONE;
289 }
290 $cache = self::getInstance( $fallback );
291 }
292
293 return $cache;
294 }
295
304 public static function newWANCacheFromId( $id ) {
306
307 if ( !isset( $wgWANObjectCaches[$id] ) ) {
308 throw new UnexpectedValueException(
309 "Cache type \"$id\" requested is not present in \$wgWANObjectCaches." );
310 }
311
313 if ( !isset( $wgObjectCaches[$params['cacheId']] ) ) {
314 throw new UnexpectedValueException(
315 "Cache type \"{$params['cacheId']}\" is not present in \$wgObjectCaches." );
316 }
317 $params['store'] = $wgObjectCaches[$params['cacheId']];
318
319 return self::newWANCacheFromParams( $params );
320 }
321
330 public static function newWANCacheFromParams( array $params ) {
331 global $wgCommandLineMode;
332
333 $services = MediaWikiServices::getInstance();
334
335 $erGroup = $services->getEventRelayerGroup();
336 if ( isset( $params['channels'] ) ) {
337 foreach ( $params['channels'] as $action => $channel ) {
338 $params['relayers'][$action] = $erGroup->getRelayer( $channel );
339 $params['channels'][$action] = $channel;
340 }
341 }
342 $params['cache'] = self::newFromParams( $params['store'] );
343 if ( isset( $params['loggroup'] ) ) {
344 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
345 } else {
346 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
347 }
348 if ( !$wgCommandLineMode ) {
349 // Send the statsd data post-send on HTTP requests; avoid in CLI mode (T181385)
350 $params['stats'] = $services->getStatsdDataFactory();
351 // Let pre-emptive refreshes happen post-send on HTTP requests
352 $params['asyncHandler'] = [ DeferredUpdates::class, 'addCallableUpdate' ];
353 }
354 $class = $params['class'];
355
356 return new $class( $params );
357 }
358
365 public static function getLocalClusterInstance() {
366 global $wgMainCacheType;
367
368 return self::getInstance( $wgMainCacheType );
369 }
370
378 public static function getMainWANInstance() {
379 return MediaWikiServices::getInstance()->getMainWANObjectCache();
380 }
381
401 public static function getMainStashInstance() {
402 return MediaWikiServices::getInstance()->getMainObjectStash();
403 }
404
408 public static function clear() {
409 self::$instances = [];
410 self::$wanInstances = [];
411 }
412
419 public static function detectLocalServerCache() {
420 if ( function_exists( 'apc_fetch' ) ) {
421 return 'apc';
422 } elseif ( function_exists( 'apcu_fetch' ) ) {
423 return 'apcu';
424 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
425 return 'wincache';
426 }
427 return CACHE_NONE;
428 }
429}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$GLOBALS['IP']
$wgObjectCaches
Advanced object cache configuration.
$wgParserCacheType
The cache type for storing article HTML.
$wgCachePrefix
Overwrite the caching key prefix with custom value.
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
$wgWANObjectCaches
Advanced WAN object cache configuration.
global $wgCommandLineMode
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
$fallback
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
A BagOStuff object with no objects in it.
Simple store for keeping values in an associative array for the current process.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Functions to get cache objects.
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)
static getMainWANInstance()
Get the main WAN cache object.
static getMainStashInstance()
Get the cache object for the main stash.
static newFromId( $id)
Create a new cache object of the specified type.
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
static getDefaultKeyspace()
Get the default keyspace for this wiki.
static clear()
Clear all the cached instances.
static newWANCacheFromId( $id)
Create a new cache object of the specified type.
static BagOStuff[] $instances
Map of (id => BagOStuff)
static newWANCacheFromParams(array $params)
Create a new cache object of the specified type.
static WANObjectCache[] $wanInstances
Map of (id => WANObjectCache)
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it.
static getInstance( $id)
Get a cached instance of the specified type of cache object.
static newFromParams( $params)
Create a new cache object from parameters.
static getLocalClusterInstance()
Get the main cluster-local cache object.
Multi-datacenter aware caching interface.
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
const CACHE_NONE
Definition Defines.php:102
const CACHE_ANYTHING
Definition Defines.php:101
const CACHE_DB
Definition Defines.php:103
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2335
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:37
$cache
Definition mcc.php:33
CACHE_MEMCACHED $wgMainCacheType
Definition memcached.txt:63
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params