MediaWiki  master
ObjectCache.php
Go to the documentation of this file.
1 <?php
26 
64 class ObjectCache {
66  public static $instances = [];
67 
74  public static function getInstance( $id ) {
75  if ( !isset( self::$instances[$id] ) ) {
76  self::$instances[$id] = self::newFromId( $id );
77  }
78 
79  return self::$instances[$id];
80  }
81 
89  private static function newFromId( $id ) {
90  global $wgObjectCaches;
91 
92  if ( !isset( $wgObjectCaches[$id] ) ) {
93  // Always recognize these ones
94  if ( $id === CACHE_NONE ) {
95  return new EmptyBagOStuff();
96  } elseif ( $id === 'hash' ) {
97  return new HashBagOStuff();
98  }
99 
100  throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " .
101  "It is not present in \$wgObjectCaches." );
102  }
103 
104  return self::newFromParams( $wgObjectCaches[$id] );
105  }
106 
116  private static function getDefaultKeyspace() {
117  global $wgCachePrefix;
118 
119  $keyspace = $wgCachePrefix;
120  if ( is_string( $keyspace ) && $keyspace !== '' ) {
121  return $keyspace;
122  }
123 
124  return WikiMap::getCurrentWikiDbDomain()->getId();
125  }
126 
139  public static function newFromParams( array $params, Config $conf = null ) {
140  // Apply default parameters and resolve the logger instance
141  $params += [
142  'logger' => LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' ),
143  'keyspace' => self::getDefaultKeyspace(),
144  'asyncHandler' => [ DeferredUpdates::class, 'addCallableUpdate' ],
145  'reportDupes' => true,
146  ];
147 
148  if ( !isset( $params['stats'] ) ) {
149  $params['stats'] = MediaWikiServices::getInstance()->getStatsdDataFactory();
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' => static 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 }
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:89
ObjectCache
Functions to get cache objects.
Definition: ObjectCache.php:64
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:85
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:180
ObjectCache\makeLocalServerCache
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
Definition: ObjectCache.php:299
$fallback
$fallback
Definition: MessagesAb.php:11
ObjectCache\$instances
static BagOStuff[] $instances
Map of (id => BagOStuff)
Definition: ObjectCache.php:66
$wgMessageCacheType
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
Definition: DefaultSettings.php:2726
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
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:139
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgMainCacheType
$wgMainCacheType
Main cache type.
Definition: DefaultSettings.php:2718
$wgCachePrefix
$wgCachePrefix
Overwrite the caching key prefix with custom value.
Definition: DefaultSettings.php:7545
$wgObjectCaches
$wgObjectCaches
Advanced object cache configuration.
Definition: DefaultSettings.php:2768
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:74
$wgParserCacheType
$wgParserCacheType
The cache type for storing article HTML.
Definition: DefaultSettings.php:2734
CACHE_DB
const CACHE_DB
Definition: Defines.php:87
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:116
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:254
CACHE_NONE
const CACHE_NONE
Definition: Defines.php:86