70 public static $instances = [];
85 if ( !isset( self::$instances[$id] ) ) {
86 self::$instances[$id] = self::newFromId( $id );
89 return self::$instances[$id];
99 private static function newFromId( $id ) {
110 throw new InvalidArgumentException(
"Invalid object cache type \"$id\" requested. " .
111 "It is not present in \$wgObjectCaches." );
126 private static function getDefaultKeyspace() {
130 if ( is_string( $keyspace ) && $keyspace !==
'' ) {
134 return WikiMap::getCurrentWikiDbDomain()->getId();
149 $services ??= MediaWikiServices::getInstance();
150 $conf = $services->getMainConfig();
154 'logger' => LoggerFactory::getInstance(
$params[
'loggroup'] ??
'objectcache' ),
155 'keyspace' => self::getDefaultKeyspace(),
156 'asyncHandler' => [ DeferredUpdates::class,
'addCallableUpdate' ],
157 'reportDupes' =>
true,
158 'stats' => $services->getStatsFactory(),
161 if ( isset(
$params[
'factory'] ) ) {
164 return call_user_func(
$params[
'factory'], ...$args );
167 if ( !isset(
$params[
'class'] ) ) {
168 throw new InvalidArgumentException(
169 'No "factory" nor "class" provided; got "' . print_r(
$params,
true ) .
'"'
176 if ( is_a( $class, SqlBagOStuff::class,
true ) ) {
177 if ( isset(
$params[
'globalKeyLB'] ) ) {
178 throw new InvalidArgumentException(
179 'globalKeyLB in $wgObjectCaches is no longer supported' );
181 if ( isset(
$params[
'server'] ) && !isset(
$params[
'servers'] ) ) {
185 if ( isset(
$params[
'servers'] ) ) {
187 foreach (
$params[
'servers'] as &$server ) {
188 if ( $server[
'type'] ===
'sqlite' && !isset( $server[
'dbDirectory'] ) ) {
189 $server[
'dbDirectory'] = $conf->get( MainConfigNames::SQLiteDataDir );
192 } elseif ( isset(
$params[
'cluster'] ) ) {
194 $params[
'loadBalancerCallback'] =
static function () use ( $services, $cluster ) {
195 return $services->getDBLoadBalancerFactory()->getExternalLB( $cluster );
197 $params += [
'dbDomain' => false ];
199 $params[
'loadBalancerCallback'] =
static function () use ( $services ) {
200 return $services->getDBLoadBalancer();
202 $params += [
'dbDomain' => false ];
204 $params += [
'writeBatchSize' => $conf->get( MainConfigNames::UpdateRowsPerQuery ) ];
208 if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
210 'servers' => $conf->get( MainConfigNames::MemCachedServers ),
211 'persistent' => $conf->get( MainConfigNames::MemCachedPersistent ),
212 'timeout' => $conf->get( MainConfigNames::MemCachedTimeout ),
217 if ( is_a( $class, MultiWriteBagOStuff::class,
true ) ) {
220 '@phan-var array{caches:array[]} $params';
221 foreach (
$params[
'caches'] ?? [] as $i => $cacheInfo ) {
224 $params[
'caches'][$i] = self::newFromParams( $cacheInfo, $services );
227 if ( is_a( $class, RESTBagOStuff::class,
true ) ) {
228 $params[
'telemetry'] = Telemetry::getInstance();
248 return self::getInstance( self::getAnythingId() );
255 private static function getAnythingId() {
258 foreach ( $candidates as $candidate ) {
262 $class = self::getLocalServerCacheClass();
263 if ( $class !== EmptyBagOStuff::class ) {
271 $services = MediaWikiServices::getInstance();
273 if ( $services->isServiceDisabled(
'DBLoadBalancer' ) ) {
276 } elseif ( $services->isStorageDisabled() ) {
304 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
322 return MediaWikiServices::getInstance()->get(
'_LocalClusterCache' );
337 if ( ( $cache[
'class'] ??
'' ) === SqlBagOStuff::class ) {
340 if ( ( $cache[
'factory'] ??
'' ) ===
'ObjectCache::newAnything' ) {
341 $id = self::getAnythingId();
342 return self::isDatabaseId( $id );
351 self::$instances = [];
370 'reportDupes' => false,
372 'keyspace' => self::getDefaultKeyspace(),
374 $class = self::getLocalServerCacheClass();
382 private static function getLocalServerCacheClass() {
383 if ( self::$localServerCacheClass !==
null ) {
384 return self::$localServerCacheClass;
386 if ( function_exists(
'apcu_fetch' ) ) {
388 if ( PHP_SAPI !==
'cli' || ini_get(
'apc.enable_cli' ) ) {
389 return APCUBagOStuff::class;
392 } elseif ( function_exists(
'wincache_ucache_get' ) ) {
393 return WinCacheBagOStuff::class;
396 return EmptyBagOStuff::class;
array $params
The job parameters.
Class representing a cache/ephemeral data store.
A BagOStuff object with no objects in it.
Simple store for keeping values in an associative array for the current process.
A class containing constants representing the names of configuration variables.
Functions to get cache objects.
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from configuration)
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
static isDatabaseId( $id)
Determine whether a config ID would access the database.
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced by configuration)
static clear()
Clear all the cached instances.
static newFromParams(array $params, MediaWikiServices $services=null)
Create a new cache object from parameters.
static BagOStuff[] $instances
Map of (id => BagOStuff)
static getInstance( $id)
Get a cached instance of the specified type of cache object.
static string $localServerCacheClass
static getLocalClusterInstance()
Get the main cluster-local cache object.
$wgObjectCaches
Config variable stub for the ObjectCaches setting, for use by phpdoc and IDEs.
$wgParserCacheType
Config variable stub for the ParserCacheType setting, for use by phpdoc and IDEs.
$wgMainCacheType
Config variable stub for the MainCacheType setting, for use by phpdoc and IDEs.
$wgCachePrefix
Config variable stub for the CachePrefix setting, for use by phpdoc and IDEs.
$wgMessageCacheType
Config variable stub for the MessageCacheType setting, for use by phpdoc and IDEs.