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['factory'] ) ) {
153  return call_user_func( $params['factory'], $params );
154  }
155 
156  if ( !isset( $params['class'] ) ) {
157  throw new InvalidArgumentException(
158  'No "factory" nor "class" provided; got "' . print_r( $params, true ) . '"'
159  );
160  }
161 
162  $class = $params['class'];
163  $conf = $conf ?? MediaWikiServices::getInstance()->getMainConfig();
164 
165  // Do b/c logic for SqlBagOStuff
166  if ( is_a( $class, SqlBagOStuff::class, true ) ) {
167  if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
168  $params['servers'] = [ $params['server'] ];
169  unset( $params['server'] );
170  }
171  // In the past it was not required to set 'dbDirectory' in $wgObjectCaches
172  if ( isset( $params['servers'] ) ) {
173  foreach ( $params['servers'] as &$server ) {
174  if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) {
175  $server['dbDirectory'] = $conf->get( 'SQLiteDataDir' );
176  }
177  }
178  } elseif ( !isset( $params['localKeyLB'] ) ) {
179  $params['localKeyLB'] = [
180  'factory' => function () {
181  return MediaWikiServices::getInstance()->getDBLoadBalancer();
182  }
183  ];
184  }
185  }
186 
187  // Do b/c logic for MemcachedBagOStuff
188  if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
189  $params += [
190  'servers' => $conf->get( 'MemCachedServers' ),
191  'persistent' => $conf->get( 'MemCachedPersistent' ),
192  'timeout' => $conf->get( 'MemCachedTimeout' ),
193  ];
194  }
195 
196  return new $class( $params );
197  }
198 
212  public static function newAnything( $params ) {
215  foreach ( $candidates as $candidate ) {
216  if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
217  $cache = self::getInstance( $candidate );
218  // CACHE_ACCEL might default to nothing if no APCu
219  // See includes/ServiceWiring.php
220  if ( !( $cache instanceof EmptyBagOStuff ) ) {
221  return $cache;
222  }
223  }
224  }
225 
226  if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
227  // The LoadBalancer is disabled, probably because
228  // MediaWikiServices::disableStorageBackend was called.
229  $candidate = CACHE_NONE;
230  } else {
231  $candidate = CACHE_DB;
232  }
233 
234  return self::getInstance( $candidate );
235  }
236 
254  public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
255  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
256  if ( $cache instanceof EmptyBagOStuff ) {
257  if ( is_array( $fallback ) ) {
258  $fallback = $fallback['fallback'] ?? CACHE_NONE;
259  }
261  }
262 
263  return $cache;
264  }
265 
272  public static function getLocalClusterInstance() {
273  global $wgMainCacheType;
274 
276  }
277 
281  public static function clear() {
282  self::$instances = [];
283  }
284 
299  public static function makeLocalServerCache() : BagOStuff {
300  $params = [
301  'reportDupes' => false,
302  // Even simple caches must use a keyspace (T247562)
303  'keyspace' => self::getDefaultKeyspace(),
304  ];
305  if ( function_exists( 'apcu_fetch' ) ) {
306  // Make sure the APCu methods actually store anything
307  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
308  return new APCUBagOStuff( $params );
309  }
310  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
311  return new WinCacheBagOStuff( $params );
312  }
313 
314  return new EmptyBagOStuff( $params );
315  }
316 
325  public static function detectLocalServerCache() {
326  wfDeprecated( __METHOD__, '1.35' );
327 
328  if ( function_exists( 'apcu_fetch' ) ) {
329  // Make sure the APCu methods actually store anything
330  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
331  return 'apcu';
332  }
333  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
334  return 'wincache';
335  }
336 
337  return CACHE_NONE;
338  }
339 }
ObjectCache\clear
static clear()
Clear all the cached instances.
Definition: ObjectCache.php:281
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
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:272
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:152
ObjectCache\makeLocalServerCache
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Definition: ObjectCache.php:299
ObjectCache\detectLocalServerCache
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it.
Definition: ObjectCache.php:325
CACHE_NONE
const CACHE_NONE
Definition: Defines.php:91
$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:2461
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:71
ObjectCache\newAnything
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
Definition: ObjectCache.php:212
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:1026
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgMainCacheType
$wgMainCacheType
Main cache type.
Definition: DefaultSettings.php:2453
$wgCachePrefix
$wgCachePrefix
Overwrite the caching key prefix with custom value.
Definition: DefaultSettings.php:6918
$wgObjectCaches
$wgObjectCaches
Advanced object cache configuration.
Definition: DefaultSettings.php:2501
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:2469
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:90
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
CACHE_DB
const CACHE_DB
Definition: Defines.php:92
ObjectCache\getLocalServerInstance
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
Definition: ObjectCache.php:254