MediaWiki REL1_35
ObjectCache.php
Go to the documentation of this file.
1<?php
26
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
143 public static function newFromParams( array $params, Config $conf = null ) {
144 // Apply default parameters and resolve the logger instance
145 $params += [
146 'logger' => LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' ),
147 'keyspace' => self::getDefaultKeyspace(),
148 'asyncHandler' => [ DeferredUpdates::class, 'addCallableUpdate' ],
149 'reportDupes' => true,
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' => 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 }
260 $cache = self::getInstance( $fallback );
261 }
262
263 return $cache;
264 }
265
272 public static function getLocalClusterInstance() {
273 global $wgMainCacheType;
274
275 return self::getInstance( $wgMainCacheType );
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
325 public static function detectLocalServerCache() {
326 wfDeprecated( __METHOD__, '1.35' );
327
328 if ( function_exists( 'apcu_fetch' ) ) {
329 // Make sure the APCu methods actually store anything
330 if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
331 return 'apcu';
332 }
333 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
334 return 'wincache';
335 }
336
337 return CACHE_NONE;
338 }
339}
$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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
$fallback
This is a wrapper for APCU's shared memory functions.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:71
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 detectLocalServerCache()
Detects which local server cache library is present and returns a configuration for it.
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.
const CACHE_NONE
Definition Defines.php:92
const CACHE_ANYTHING
Definition Defines.php:91
const CACHE_DB
Definition Defines.php:93
Interface for configuration instances.
Definition Config.php:30
$cache
Definition mcc.php:33