MediaWiki  master
ObjectCache.php
Go to the documentation of this file.
1 <?php
26 
68 class ObjectCache {
70  public static $instances = [];
71 
78  public static function getInstance( $id ) {
79  if ( !isset( self::$instances[$id] ) ) {
80  self::$instances[$id] = self::newFromId( $id );
81  }
82 
83  return self::$instances[$id];
84  }
85 
93  private static function newFromId( $id ) {
94  global $wgObjectCaches;
95 
96  if ( !isset( $wgObjectCaches[$id] ) ) {
97  // Always recognize these ones
98  if ( $id === CACHE_NONE ) {
99  return new EmptyBagOStuff();
100  } elseif ( $id === 'hash' ) {
101  return new HashBagOStuff();
102  }
103 
104  throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " .
105  "It is not present in \$wgObjectCaches." );
106  }
107 
108  return self::newFromParams( $wgObjectCaches[$id] );
109  }
110 
120  private static function getDefaultKeyspace() {
121  global $wgCachePrefix;
122 
123  $keyspace = $wgCachePrefix;
124  if ( is_string( $keyspace ) && $keyspace !== '' ) {
125  return $keyspace;
126  }
127 
128  return WikiMap::getCurrentWikiDbDomain()->getId();
129  }
130 
143  public static function newFromParams( array $params, Config $conf = null ) {
144  // Apply default parameters and resolve the logger instance
145  $params += [
146  'logger' => LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' ),
147  'keyspace' => self::getDefaultKeyspace(),
148  'asyncHandler' => [ DeferredUpdates::class, 'addCallableUpdate' ],
149  'reportDupes' => true,
150  ];
151 
152  if ( !isset( $params['stats'] ) ) {
153  $params['stats'] = MediaWikiServices::getInstance()->getStatsdDataFactory();
154  }
155 
156  if ( isset( $params['factory'] ) ) {
157  return call_user_func( $params['factory'], $params );
158  }
159 
160  if ( !isset( $params['class'] ) ) {
161  throw new InvalidArgumentException(
162  'No "factory" nor "class" provided; got "' . print_r( $params, true ) . '"'
163  );
164  }
165 
166  $class = $params['class'];
167  $conf = $conf ?? MediaWikiServices::getInstance()->getMainConfig();
168 
169  // Do b/c logic for SqlBagOStuff
170  if ( is_a( $class, SqlBagOStuff::class, true ) ) {
171  if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
172  $params['servers'] = [ $params['server'] ];
173  unset( $params['server'] );
174  }
175  // In the past it was not required to set 'dbDirectory' in $wgObjectCaches
176  if ( isset( $params['servers'] ) ) {
177  foreach ( $params['servers'] as &$server ) {
178  if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) {
179  $server['dbDirectory'] = $conf->get( 'SQLiteDataDir' );
180  }
181  }
182  } elseif ( !isset( $params['localKeyLB'] ) ) {
183  $params['localKeyLB'] = [
184  'factory' => function () {
185  return MediaWikiServices::getInstance()->getDBLoadBalancer();
186  }
187  ];
188  }
189  }
190 
191  // Do b/c logic for MemcachedBagOStuff
192  if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
193  $params += [
194  'servers' => $conf->get( 'MemCachedServers' ),
195  'persistent' => $conf->get( 'MemCachedPersistent' ),
196  'timeout' => $conf->get( 'MemCachedTimeout' ),
197  ];
198  }
199 
200  return new $class( $params );
201  }
202 
216  public static function newAnything( $params ) {
219  foreach ( $candidates as $candidate ) {
220  if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
221  $cache = self::getInstance( $candidate );
222  // CACHE_ACCEL might default to nothing if no APCu
223  // See includes/ServiceWiring.php
224  if ( !( $cache instanceof EmptyBagOStuff ) ) {
225  return $cache;
226  }
227  }
228  }
229 
230  if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
231  // The LoadBalancer is disabled, probably because
232  // MediaWikiServices::disableStorageBackend was called.
233  $candidate = CACHE_NONE;
234  } else {
235  $candidate = CACHE_DB;
236  }
237 
238  return self::getInstance( $candidate );
239  }
240 
258  public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
259  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
260  if ( $cache instanceof EmptyBagOStuff ) {
261  if ( is_array( $fallback ) ) {
262  $fallback = $fallback['fallback'] ?? CACHE_NONE;
263  }
265  }
266 
267  return $cache;
268  }
269 
276  public static function getLocalClusterInstance() {
277  global $wgMainCacheType;
278 
280  }
281 
285  public static function clear() {
286  self::$instances = [];
287  }
288 
303  public static function makeLocalServerCache() : BagOStuff {
304  $params = [
305  'reportDupes' => false,
306  // Even simple caches must use a keyspace (T247562)
307  'keyspace' => self::getDefaultKeyspace(),
308  ];
309  if ( function_exists( 'apcu_fetch' ) ) {
310  // Make sure the APCu methods actually store anything
311  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
312  return new APCUBagOStuff( $params );
313  }
314  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
315  return new WinCacheBagOStuff( $params );
316  }
317 
318  return new EmptyBagOStuff( $params );
319  }
320 
329  public static function detectLocalServerCache() {
330  wfDeprecated( __METHOD__, '1.35' );
331 
332  if ( function_exists( 'apcu_fetch' ) ) {
333  // Make sure the APCu methods actually store anything
334  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
335  return 'apcu';
336  }
337  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
338  return 'wincache';
339  }
340 
341  return CACHE_NONE;
342  }
343 }
ObjectCache\clear
static clear()
Clear all the cached instances.
Definition: ObjectCache.php:285
ObjectCache\newFromId
static newFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:93
ObjectCache
Functions to get cache objects.
Definition: ObjectCache.php:68
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:84
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:276
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:32
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:166
ObjectCache\makeLocalServerCache
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Definition: ObjectCache.php:303
ObjectCache\detectLocalServerCache
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it.
Definition: ObjectCache.php:329
$fallback
$fallback
Definition: MessagesAb.php:11
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:2455
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:72
ObjectCache\newAnything
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
Definition: ObjectCache.php:216
Config
Interface for configuration instances.
Definition: Config.php:30
ObjectCache\newFromParams
static newFromParams(array $params, Config $conf=null)
Create a new cache object from parameters.
Definition: ObjectCache.php:143
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1033
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgMainCacheType
$wgMainCacheType
Main cache type.
Definition: DefaultSettings.php:2447
$wgCachePrefix
$wgCachePrefix
Overwrite the caching key prefix with custom value.
Definition: DefaultSettings.php:6921
$wgObjectCaches
$wgObjectCaches
Advanced object cache configuration.
Definition: DefaultSettings.php:2495
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:78
$wgParserCacheType
$wgParserCacheType
The cache type for storing article HTML.
Definition: DefaultSettings.php:2463
CACHE_DB
const CACHE_DB
Definition: Defines.php:86
APCUBagOStuff
This is a wrapper for APCU's shared memory functions.
Definition: APCUBagOStuff.php:36
ObjectCache\getDefaultKeyspace
static getDefaultKeyspace()
Get the default keyspace for this wiki.
Definition: ObjectCache.php:120
WinCacheBagOStuff
Wrapper for WinCache object caching functions; identical interface to the APC wrapper.
Definition: WinCacheBagOStuff.php:30
$cache
$cache
Definition: mcc.php:33
ObjectCache\getLocalServerInstance
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
Definition: ObjectCache.php:258
CACHE_NONE
const CACHE_NONE
Definition: Defines.php:85