MediaWiki 1.42.0
ObjectCache.php
Go to the documentation of this file.
1<?php
25
35 public static $instances = [];
36
42
51 public static function getInstance( $id ) {
52 return MediaWikiServices::getInstance()->getObjectCacheFactory()->getInstance( $id );
53 }
54
63 public static function newFromParams( array $params ) {
64 return MediaWikiServices::getInstance()->getObjectCacheFactory()
65 ->newFromParams( $params );
66 }
67
83 public static function newAnything() {
84 return MediaWikiServices::getInstance()->getObjectCacheFactory()
85 ->getInstance( self::getAnythingId() );
86 }
87
94 public static function getAnythingId() {
97 foreach ( $candidates as $candidate ) {
98 if ( $candidate === CACHE_ACCEL ) {
99 // CACHE_ACCEL might default to nothing if no APCu
100 // See includes/ServiceWiring.php
101 $class = self::getLocalServerCacheClass();
102 if ( $class !== EmptyBagOStuff::class ) {
103 return $candidate;
104 }
105 } elseif ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
106 return $candidate;
107 }
108 }
109
110 $services = MediaWikiServices::getInstance();
111
112 if ( $services->isServiceDisabled( 'DBLoadBalancer' ) ) {
113 // The DBLoadBalancer service is disabled, so we can't use the database!
114 $candidate = CACHE_NONE;
115 } elseif ( $services->isStorageDisabled() ) {
116 // Storage services are disabled because MediaWikiServices::disableStorage()
117 // was called. This is typically the case during installation.
118 $candidate = CACHE_NONE;
119 } else {
120 $candidate = CACHE_DB;
121 }
122 return $candidate;
123 }
124
132 public static function getLocalServerInstance( $fallback = CACHE_NONE ) {
133 return MediaWikiServices::getInstance()->getObjectCacheFactory()
134 ->getLocalServerInstance( $fallback );
135 }
136
143 public static function getLocalClusterInstance() {
144 return MediaWikiServices::getInstance()->get( '_LocalClusterCache' );
145 }
146
153 public static function isDatabaseId( $id ) {
154 global $wgObjectCaches;
155
156 // NOTE: Sanity check if $id is set to CACHE_ANYTHING and
157 // everything is going through service wiring. CACHE_ANYTHING
158 // would default to CACHE_DB, let's handle that early for cases
159 // where all cache configs are set to CACHE_ANYTHING (T362686).
160 if ( $id === CACHE_ANYTHING ) {
161 $id = self::getAnythingId();
162 return self::isDatabaseId( $id );
163 }
164
165 if ( !isset( $wgObjectCaches[$id] ) ) {
166 return false;
167 }
168 $cache = $wgObjectCaches[$id];
169 if ( ( $cache['class'] ?? '' ) === SqlBagOStuff::class ) {
170 return true;
171 }
172
173 return false;
174 }
175
181 public static function clear() {
182 MediaWikiServices::getInstance()->getObjectCacheFactory()->clear();
183 }
184
203 public static function makeLocalServerCache( $keyspace ): BagOStuff {
204 $params = [
205 'reportDupes' => false,
206 // Even simple caches must use a keyspace (T247562)
207 'keyspace' => $keyspace,
208 ];
209 $class = self::getLocalServerCacheClass();
210 return new $class( $params );
211 }
212
217 private static function getLocalServerCacheClass() {
218 if ( self::$localServerCacheClass !== null ) {
219 return self::$localServerCacheClass;
220 }
221 if ( function_exists( 'apcu_fetch' ) ) {
222 // Make sure the APCu methods actually store anything
223 if ( PHP_SAPI !== 'cli' || ini_get( 'apc.enable_cli' ) ) {
224 return APCUBagOStuff::class;
225
226 }
227 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
228 return WinCacheBagOStuff::class;
229 }
230
231 return EmptyBagOStuff::class;
232 }
233}
const CACHE_NONE
Definition Defines.php:86
const CACHE_ANYTHING
Definition Defines.php:85
const CACHE_ACCEL
Definition Defines.php:89
const CACHE_DB
Definition Defines.php:87
$fallback
array $params
The job parameters.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:85
Service locator for MediaWiki core services.
static getLocalServerInstance( $fallback=CACHE_NONE)
static getAnythingId()
static newAnything()
Factory function for CACHE_ANYTHING (referenced by configuration)
static isDatabaseId( $id)
Determine whether a config ID would access the database.
static newFromParams(array $params)
static clear()
static BagOStuff[] $instances
static makeLocalServerCache( $keyspace)
Create a new BagOStuff instance for local-server caching.
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.
$wgMessageCacheType
Config variable stub for the MessageCacheType setting, for use by phpdoc and IDEs.