MediaWiki REL1_37
ObjectCache.php
Go to the documentation of this file.
1<?php
26
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 $params += [ 'writeBatchSize' => $conf->get( 'UpdateRowsPerQuery' ) ];
186 }
187
188 // Do b/c logic for MemcachedBagOStuff
189 if ( is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
190 $params += [
191 'servers' => $conf->get( 'MemCachedServers' ),
192 'persistent' => $conf->get( 'MemCachedPersistent' ),
193 'timeout' => $conf->get( 'MemCachedTimeout' ),
194 ];
195 }
196
197 return new $class( $params );
198 }
199
213 public static function newAnything( $params ) {
216 foreach ( $candidates as $candidate ) {
217 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
218 $cache = self::getInstance( $candidate );
219 // CACHE_ACCEL might default to nothing if no APCu
220 // See includes/ServiceWiring.php
221 if ( !( $cache instanceof EmptyBagOStuff ) ) {
222 return $cache;
223 }
224 }
225 }
226
227 if ( MediaWikiServices::getInstance()->isServiceDisabled( 'DBLoadBalancer' ) ) {
228 // The LoadBalancer is disabled, probably because
229 // MediaWikiServices::disableStorageBackend was called.
230 $candidate = CACHE_NONE;
231 } else {
232 $candidate = CACHE_DB;
233 }
234
235 return self::getInstance( $candidate );
236 }
237
255 public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
256 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
257 if ( $cache instanceof EmptyBagOStuff ) {
258 if ( is_array( $fallback ) ) {
259 $fallback = $fallback['fallback'] ?? CACHE_NONE;
260 }
261 $cache = self::getInstance( $fallback );
262 }
263
264 return $cache;
265 }
266
273 public static function getLocalClusterInstance() {
274 global $wgMainCacheType;
275
276 return self::getInstance( $wgMainCacheType );
277 }
278
282 public static function clear() {
283 self::$instances = [];
284 }
285
300 public static function makeLocalServerCache(): BagOStuff {
301 $params = [
302 'reportDupes' => false,
303 // Even simple caches must use a keyspace (T247562)
304 'keyspace' => self::getDefaultKeyspace(),
305 ];
306 if ( function_exists( 'apcu_fetch' ) ) {
307 // Make sure the APCu methods actually store anything
308 if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
309 return new APCUBagOStuff( $params );
310 }
311 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
312 return new WinCacheBagOStuff( $params );
313 }
314
315 return new EmptyBagOStuff( $params );
316 }
317}
$wgObjectCaches
Advanced object cache configuration.
$wgParserCacheType
The cache type for storing article HTML.
$wgMainCacheType
Main cache type.
$wgCachePrefix
Overwrite the caching key prefix with custom value.
$wgMessageCacheType
The cache type for storing the contents of the MediaWiki namespace.
const CACHE_NONE
Definition Defines.php:86
const CACHE_ANYTHING
Definition Defines.php:85
const CACHE_DB
Definition Defines.php:87
$fallback
This is a wrapper for APCU's shared memory functions.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:86
A BagOStuff object with no objects in it.
Simple store for keeping values in an associative array for the current process.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Functions to get cache objects.
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
static makeLocalServerCache()
Create a new BagOStuff instance for local-server caching.
static newFromId( $id)
Create a new cache object of the specified type.
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
static getDefaultKeyspace()
Get the default keyspace for this wiki.
static clear()
Clear all the cached instances.
static BagOStuff[] $instances
Map of (id => BagOStuff)
static newFromParams(array $params, Config $conf=null)
Create a new cache object from parameters.
static getInstance( $id)
Get a cached instance of the specified type of cache object.
static getLocalClusterInstance()
Get the main cluster-local cache object.
Wrapper for WinCache object caching functions; identical interface to the APC wrapper.
Interface for configuration instances.
Definition Config.php:30
$cache
Definition mcc.php:33