MediaWiki REL1_31
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'] = 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
245 public static function newAnything( $params ) {
248 foreach ( $candidates as $candidate ) {
249 $cache = false;
250 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
251 $cache = self::getInstance( $candidate );
252 // CACHE_ACCEL might default to nothing if no APCu
253 // See includes/ServiceWiring.php
254 if ( !( $cache instanceof EmptyBagOStuff ) ) {
255 return $cache;
256 }
257 }
258 }
259
260 if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
261 // The LoadBalancer is disabled, probably because
262 // MediaWikiServices::disableStorageBackend was called.
263 $candidate = CACHE_NONE;
264 } else {
265 $candidate = CACHE_DB;
266 }
267
268 return self::getInstance( $candidate );
269 }
270
288 public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
289 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
290 if ( $cache instanceof EmptyBagOStuff ) {
291 if ( is_array( $fallback ) ) {
292 $fallback = isset( $fallback['fallback'] ) ? $fallback['fallback'] : CACHE_NONE;
293 }
294 $cache = self::getInstance( $fallback );
295 }
296
297 return $cache;
298 }
299
308 public static function newWANCacheFromId( $id ) {
310
311 if ( !isset( $wgWANObjectCaches[$id] ) ) {
312 throw new UnexpectedValueException(
313 "Cache type \"$id\" requested is not present in \$wgWANObjectCaches." );
314 }
315
317 if ( !isset( $wgObjectCaches[$params['cacheId']] ) ) {
318 throw new UnexpectedValueException(
319 "Cache type \"{$params['cacheId']}\" is not present in \$wgObjectCaches." );
320 }
321 $params['store'] = $wgObjectCaches[$params['cacheId']];
322
323 return self::newWANCacheFromParams( $params );
324 }
325
334 public static function newWANCacheFromParams( array $params ) {
335 global $wgCommandLineMode;
336
337 $services = MediaWikiServices::getInstance();
338
339 $erGroup = $services->getEventRelayerGroup();
340 foreach ( $params['channels'] as $action => $channel ) {
341 $params['relayers'][$action] = $erGroup->getRelayer( $channel );
342 $params['channels'][$action] = $channel;
343 }
344 $params['cache'] = self::newFromParams( $params['store'] );
345 if ( isset( $params['loggroup'] ) ) {
346 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
347 } else {
348 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
349 }
350 if ( !$wgCommandLineMode ) {
351 // Send the statsd data post-send on HTTP requests; avoid in CLI mode (T181385)
352 $params['stats'] = $services->getStatsdDataFactory();
353 // Let pre-emptive refreshes happen post-send on HTTP requests
354 $params['asyncHandler'] = [ DeferredUpdates::class, 'addCallableUpdate' ];
355 }
356 $class = $params['class'];
357
358 return new $class( $params );
359 }
360
367 public static function getLocalClusterInstance() {
368 global $wgMainCacheType;
369
370 return self::getInstance( $wgMainCacheType );
371 }
372
380 public static function getMainWANInstance() {
381 return MediaWikiServices::getInstance()->getMainWANObjectCache();
382 }
383
403 public static function getMainStashInstance() {
404 return MediaWikiServices::getInstance()->getMainObjectStash();
405 }
406
410 public static function clear() {
411 self::$instances = [];
412 self::$wanInstances = [];
413 }
414}
$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
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
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 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.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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:2273
const CACHE_NONE
Definition Defines.php:112
const CACHE_ANYTHING
Definition Defines.php:111
const CACHE_DB
Definition Defines.php:113
$cache
Definition mcc.php:33
CACHE_MEMCACHED $wgMainCacheType
Definition memcached.txt:63
$params