Merge "Use a fixed marker prefix string in the Parser and MWTidy"
[mediawiki.git] / includes / objectcache / ObjectCache.php
blob7faf4bb6a81eb59b875ee634ad0131cf90c9da88
1 <?php
2 /**
3 * Functions to get cache objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
24 use MediaWiki\Logger\LoggerFactory;
26 /**
27 * Functions to get cache objects
29 * The word "cache" has two main dictionary meanings, and both
30 * are used in this factory class. They are:
31 * - a) A place to store copies or computations on existing data
32 * for higher access speeds (the computer science definition)
33 * - b) A place to store lightweight data that is not canonically
34 * stored anywhere else (e.g. a "hoard" of objects)
36 * @ingroup Cache
38 class ObjectCache {
39 /** @var Array Map of (id => BagOStuff) */
40 public static $instances = array();
41 /** @var Array Map of (id => WANObjectCache) */
42 public static $wanInstances = array();
44 /**
45 * Get a cached instance of the specified type of cache object.
47 * @param string $id
49 * @return BagOStuff
51 static function getInstance( $id ) {
52 if ( isset( self::$instances[$id] ) ) {
53 return self::$instances[$id];
56 $object = self::newFromId( $id );
57 self::$instances[$id] = $object;
58 return $object;
61 /**
62 * Get a cached instance of the specified type of cache object.
64 * @param string $id
66 * @return WANObjectCache
67 * @since 1.26
69 static function getWANInstance( $id ) {
70 if ( isset( self::$wanInstances[$id] ) ) {
71 return self::$wanInstances[$id];
74 $object = self::newWANCacheFromId( $id );
75 self::$wanInstances[$id] = $object;
76 return $object;
79 /**
80 * Clear all the cached instances.
82 static function clear() {
83 self::$instances = array();
84 self::$wanInstances = array();
87 /**
88 * Create a new cache object of the specified type.
90 * @param string $id
92 * @throws MWException
93 * @return BagOStuff
95 static function newFromId( $id ) {
96 global $wgObjectCaches;
98 if ( !isset( $wgObjectCaches[$id] ) ) {
99 throw new MWException( "Invalid object cache type \"$id\" requested. " .
100 "It is not present in \$wgObjectCaches." );
103 return self::newFromParams( $wgObjectCaches[$id] );
107 * Create a new cache object from parameters
109 * @param array $params
111 * @throws MWException
112 * @return BagOStuff
114 static function newFromParams( $params ) {
115 if ( isset( $params['loggroup'] ) ) {
116 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
117 } else {
118 // For backwards-compatability with custom parameters, lets not
119 // have all logging suddenly disappear
120 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
122 if ( isset( $params['factory'] ) ) {
123 return call_user_func( $params['factory'], $params );
124 } elseif ( isset( $params['class'] ) ) {
125 $class = $params['class'];
126 return new $class( $params );
127 } else {
128 throw new MWException( "The definition of cache type \""
129 . print_r( $params, true ) . "\" lacks both "
130 . "factory and class parameters." );
135 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
137 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
138 * If a caching method is configured for any of the main caches ($wgMainCacheType,
139 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
140 * be an alias to the configured cache choice for that.
141 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
142 * then CACHE_ANYTHING will forward to CACHE_DB.
143 * @param array $params
144 * @return BagOStuff
146 static function newAnything( $params ) {
147 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
148 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
149 foreach ( $candidates as $candidate ) {
150 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
151 return self::getInstance( $candidate );
154 return self::getInstance( CACHE_DB );
158 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
160 * This will look for any APC style server-local cache.
161 * A fallback cache can be specified if none is found.
163 * @param array $params
164 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
165 * @throws MWException
166 * @return BagOStuff
168 static function newAccelerator( $params, $fallback = null ) {
169 if ( function_exists( 'apc_fetch' ) ) {
170 $id = 'apc';
171 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
172 $id = 'xcache';
173 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
174 $id = 'wincache';
175 } else {
176 if ( $fallback !== null ) {
177 return self::newFromId( $fallback );
179 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
180 "cache is present. You may want to install APC." );
182 return self::newFromId( $id );
186 * Factory function that creates a memcached client object.
188 * This always uses the PHP client, since the PECL client has a different
189 * hashing scheme and a different interpretation of the flags bitfield, so
190 * switching between the two clients randomly would be disastrous.
192 * @param array $params
194 * @return MemcachedPhpBagOStuff
196 static function newMemcached( $params ) {
197 return new MemcachedPhpBagOStuff( $params );
201 * Create a new cache object of the specified type
203 * @param string $id
205 * @throws MWException
206 * @return WANObjectCache
207 * @since 1.26
209 static function newWANCacheFromId( $id ) {
210 global $wgWANObjectCaches;
212 if ( !isset( $wgWANObjectCaches[$id] ) ) {
213 throw new MWException( "Invalid object cache type \"$id\" requested. " .
214 "It is not present in \$wgWANObjectCaches." );
217 $params = $wgWANObjectCaches[$id];
218 $class = $params['relayerConfig']['class'];
219 $params['relayer'] = new $class( $params['relayerConfig'] );
220 $params['cache'] = self::newFromId( $params['cacheId'] );
221 $class = $params['class'];
223 return new $class( $params );
227 * Get the main WAN cache object
229 * @return WANObjectCache
230 * @since 1.26
232 static function getMainWANInstance() {
233 global $wgMainWANCache;
235 return self::getWANInstance( $wgMainWANCache );
239 * Stash objects are BagOStuff instances suitable for storing light
240 * weight data that is not canonically stored elsewhere (such as RDBMS).
241 * Stashes should be configured to propagate changes to all data-centers.
243 * Callers should be prepared for:
244 * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
245 * - b) Reads to be eventually consistent, e.g. for get()/getMulti()
246 * In general, this means avoiding updates on idempotent HTTP requests and
247 * avoiding an assumption of perfect serializability (or accepting anomalies).
248 * Reads may be eventually consistent or data might rollback as nodes flap.
251 * @return BagOStuff
252 * @since 1.26
254 static function getMainStashInstance() {
255 global $wgMainStash;
257 return self::getInstance( $wgMainStash );