83 if ( !isset( self::$instances[$id] ) ) {
84 self::$instances[$id] = self::newFromId( $id );
87 return self::$instances[$id];
97 private static function newFromId( $id ) {
108 throw new InvalidArgumentException(
"Invalid object cache type \"$id\" requested. " .
109 "It is not present in \$wgObjectCaches." );
124 private static function getDefaultKeyspace() {
128 if ( is_string( $keyspace ) && $keyspace !==
'' ) {
132 return WikiMap::getCurrentWikiDbDomain()->getId();
147 $services ??= MediaWikiServices::getInstance();
148 $conf = $services->getMainConfig();
152 'logger' => LoggerFactory::getInstance( $params[
'loggroup'] ??
'objectcache' ),
153 'keyspace' => self::getDefaultKeyspace(),
154 'asyncHandler' => [ DeferredUpdates::class,
'addCallableUpdate' ],
155 'reportDupes' =>
true,
156 'stats' => $services->getStatsdDataFactory(),
159 if ( isset( $params[
'factory'] ) ) {
160 $args = $params[
'args'] ?? [ $params ];
162 return call_user_func( $params[
'factory'], ...$args );
165 if ( !isset( $params[
'class'] ) ) {
166 throw new InvalidArgumentException(
167 'No "factory" nor "class" provided; got "' . print_r( $params,
true ) .
'"'
171 $class = $params[
'class'];
174 if ( is_a( $class, SqlBagOStuff::class,
true ) ) {
175 if ( isset( $params[
'globalKeyLB'] ) ) {
176 throw new InvalidArgumentException(
177 'globalKeyLB in $wgObjectCaches is no longer supported' );
179 if ( isset( $params[
'server'] ) && !isset( $params[
'servers'] ) ) {
180 $params[
'servers'] = [ $params[
'server'] ];
181 unset( $params[
'server'] );
183 if ( isset( $params[
'servers'] ) ) {
185 foreach ( $params[
'servers'] as &$server ) {
186 if ( $server[
'type'] ===
'sqlite' && !isset( $server[
'dbDirectory'] ) ) {
187 $server[
'dbDirectory'] = $conf->get( MainConfigNames::SQLiteDataDir );
190 } elseif ( isset( $params[
'cluster'] ) ) {
191 $cluster = $params[
'cluster'];
192 $params[
'loadBalancerCallback'] =
static function () use ( $services, $cluster ) {
193 return $services->getDBLoadBalancerFactory()->getExternalLB( $cluster );
195 $params += [
'dbDomain' => false ];
197 $params[
'loadBalancerCallback'] =
static function () use ( $services ) {
198 return $services->getDBLoadBalancer();
200 $params += [
'dbDomain' => false ];
202 $params += [
'writeBatchSize' => $conf->get( MainConfigNames::UpdateRowsPerQuery ) ];
206 if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
208 'servers' => $conf->get( MainConfigNames::MemCachedServers ),
209 'persistent' => $conf->get( MainConfigNames::MemCachedPersistent ),
210 'timeout' => $conf->get( MainConfigNames::MemCachedTimeout ),
215 if ( is_a( $class, MultiWriteBagOStuff::class,
true ) ) {
218 '@phan-var array{caches:array[]} $params';
219 foreach ( $params[
'caches'] ?? [] as $i => $cacheInfo ) {
226 return new $class( $params );
250 private static function getAnythingId() {
253 foreach ( $candidates as $candidate ) {
257 $class = self::getLocalServerCacheClass();
258 if ( $class !== EmptyBagOStuff::class ) {
266 $services = MediaWikiServices::getInstance();
268 if ( $services->isServiceDisabled(
'DBLoadBalancer' ) ) {
271 } elseif ( $services->isStorageDisabled() ) {
299 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
317 return MediaWikiServices::getInstance()->get(
'_LocalClusterCache' );
332 if ( ( $cache[
'class'] ??
'' ) === SqlBagOStuff::class ) {
336 if ( $id ===
'db-replicated' ) {
339 if ( ( $cache[
'factory'] ??
'' ) ===
'ObjectCache::newAnything' ) {
340 $id = self::getAnythingId();
350 self::$instances = [];
369 'reportDupes' => false,
371 'keyspace' => self::getDefaultKeyspace(),
373 $class = self::getLocalServerCacheClass();
374 return new $class( $params );
381 private static function getLocalServerCacheClass() {
382 if ( self::$localServerCacheClass !==
null ) {
383 return self::$localServerCacheClass;
385 if ( function_exists(
'apcu_fetch' ) ) {
387 if ( PHP_SAPI !==
'cli' || ini_get(
'apc.enable_cli' ) ) {
388 return APCUBagOStuff::class;
391 } elseif ( function_exists(
'wincache_ucache_get' ) ) {
392 return WinCacheBagOStuff::class;
395 return EmptyBagOStuff::class;
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.