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 
142  public static function newFromParams( $params ) {
143  $params['logger'] = $params['logger'] ??
144  LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' );
145  if ( !isset( $params['keyspace'] ) ) {
146  $params['keyspace'] = self::getDefaultKeyspace();
147  }
148  if ( isset( $params['factory'] ) ) {
149  return call_user_func( $params['factory'], $params );
150  } elseif ( isset( $params['class'] ) ) {
151  $class = $params['class'];
152  // Automatically set the 'async' update handler
153  $params['asyncHandler'] = $params['asyncHandler']
154  ?? [ DeferredUpdates::class, 'addCallableUpdate' ];
155  // Enable reportDupes by default
156  $params['reportDupes'] = $params['reportDupes'] ?? true;
157  // Do b/c logic for SqlBagOStuff
158  if ( is_a( $class, SqlBagOStuff::class, true ) ) {
159  if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) {
160  $params['servers'] = [ $params['server'] ];
161  unset( $params['server'] );
162  }
163  // In the past it was not required to set 'dbDirectory' in $wgObjectCaches
164  if ( isset( $params['servers'] ) ) {
165  foreach ( $params['servers'] as &$server ) {
166  if ( $server['type'] === 'sqlite' && !isset( $server['dbDirectory'] ) ) {
167  $server['dbDirectory'] = MediaWikiServices::getInstance()
168  ->getMainConfig()->get( 'SQLiteDataDir' );
169  }
170  }
171  }
172  }
173 
174  // Do b/c logic for MemcachedBagOStuff
175  if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
176  if ( !isset( $params['servers'] ) ) {
177  $params['servers'] = $GLOBALS['wgMemCachedServers'];
178  }
179  if ( !isset( $params['persistent'] ) ) {
180  $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
181  }
182  if ( !isset( $params['timeout'] ) ) {
183  $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
184  }
185  }
186  return new $class( $params );
187  } else {
188  throw new InvalidArgumentException( "The definition of cache type \""
189  . print_r( $params, true ) . "\" lacks both "
190  . "factory and class parameters." );
191  }
192  }
193 
207  public static function newAnything( $params ) {
210  foreach ( $candidates as $candidate ) {
211  if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
212  $cache = self::getInstance( $candidate );
213  // CACHE_ACCEL might default to nothing if no APCu
214  // See includes/ServiceWiring.php
215  if ( !( $cache instanceof EmptyBagOStuff ) ) {
216  return $cache;
217  }
218  }
219  }
220 
221  if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
222  // The LoadBalancer is disabled, probably because
223  // MediaWikiServices::disableStorageBackend was called.
224  $candidate = CACHE_NONE;
225  } else {
226  $candidate = CACHE_DB;
227  }
228 
229  return self::getInstance( $candidate );
230  }
231 
249  public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
250  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
251  if ( $cache instanceof EmptyBagOStuff ) {
252  if ( is_array( $fallback ) ) {
253  $fallback = $fallback['fallback'] ?? CACHE_NONE;
254  }
255  $cache = self::getInstance( $fallback );
256  }
257 
258  return $cache;
259  }
260 
267  public static function getLocalClusterInstance() {
268  global $wgMainCacheType;
269 
270  return self::getInstance( $wgMainCacheType );
271  }
272 
276  public static function clear() {
277  self::$instances = [];
278  }
279 
286  public static function detectLocalServerCache() {
287  if ( function_exists( 'apcu_fetch' ) ) {
288  // Make sure the APCu methods actually store anything
289  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
290  return 'apcu';
291  }
292  } elseif ( function_exists( 'apc_fetch' ) ) {
293  // Make sure the APC methods actually store anything
294  if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
295  return 'apc';
296  }
297  } elseif ( function_exists( 'wincache_ucache_get' ) ) {
298  return 'wincache';
299  }
300 
301  return CACHE_NONE;
302  }
303 }
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:78
static getLocalClusterInstance()
Get the main cluster-local cache object.
static BagOStuff [] $instances
Map of (id => BagOStuff)
Definition: ObjectCache.php:70
$wgParserCacheType
The cache type for storing article HTML.
$wgMainCacheType
Main cache type.
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
$cache
Definition: mcc.php:33
$wgCachePrefix
Overwrite the caching key prefix with custom value.
static getDefaultKeyspace()
Get the default keyspace for this wiki.
$GLOBALS['IP']
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
$fallback
Definition: MessagesAb.php:11
static newFromParams( $params)
Create a new cache object from parameters.
static newFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:93
const CACHE_ANYTHING
Definition: Defines.php:81
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
$wgObjectCaches
Advanced object cache configuration.
static clear()
Clear all the cached instances.
static detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it...
const CACHE_NONE
Definition: Defines.php:82
const CACHE_DB
Definition: Defines.php:83