MediaWiki REL1_34
ObjectCache.php
Go to the documentation of this file.
1<?php
26
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 }
274 $cache = self::getInstance( $fallback );
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
345 return self::getInstance( $wgMainCacheType );
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}
$GLOBALS['IP']
$wgObjectCaches
Advanced object cache configuration.
$wgParserCacheType
The cache type for storing article HTML.
$wgMainCacheType
Main cache type.
$wgSecretKey
This should always be customised in LocalSettings.php.
$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
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
$fallback
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:63
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 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.
const CACHE_NONE
Definition Defines.php:91
const CACHE_ANYTHING
Definition Defines.php:90
const CACHE_DB
Definition Defines.php:92
$cache
Definition mcc.php:33