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
24 use MediaWiki\Logger\LoggerFactory
;
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)
39 /** @var Array Map of (id => BagOStuff) */
40 public static $instances = array();
42 /** @var Array Map of (id => WANObjectCache) */
43 public static $wanInstances = array();
46 * Get a cached instance of the specified type of cache object.
51 static function getInstance( $id ) {
52 if ( !isset( self
::$instances[$id] ) ) {
53 self
::$instances[$id] = self
::newFromId( $id );
56 return self
::$instances[$id];
60 * Get a cached instance of the specified type of cache object.
64 * @return WANObjectCache
66 static function getWANInstance( $id ) {
67 if ( !isset( self
::$wanInstances[$id] ) ) {
68 self
::$wanInstances[$id] = self
::newWANCacheFromId( $id );
71 return self
::$wanInstances[$id];
75 * Clear all the cached instances.
77 static function clear() {
78 self
::$instances = array();
79 self
::$wanInstances = array();
83 * Create a new cache object of the specified type.
89 static function newFromId( $id ) {
90 global $wgObjectCaches;
92 if ( !isset( $wgObjectCaches[$id] ) ) {
93 throw new MWException( "Invalid object cache type \"$id\" requested. " .
94 "It is not present in \$wgObjectCaches." );
97 return self
::newFromParams( $wgObjectCaches[$id] );
101 * Create a new cache object from parameters
103 * @param array $params
105 * @throws MWException
107 static function newFromParams( $params ) {
108 if ( isset( $params['loggroup'] ) ) {
109 $params['logger'] = LoggerFactory
::getInstance( $params['loggroup'] );
111 // For backwards-compatability with custom parameters, lets not
112 // have all logging suddenly disappear
113 $params['logger'] = LoggerFactory
::getInstance( 'objectcache' );
115 if ( isset( $params['factory'] ) ) {
116 return call_user_func( $params['factory'], $params );
117 } elseif ( isset( $params['class'] ) ) {
118 $class = $params['class'];
119 return new $class( $params );
121 throw new MWException( "The definition of cache type \""
122 . print_r( $params, true ) . "\" lacks both "
123 . "factory and class parameters." );
128 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
130 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
131 * If a caching method is configured for any of the main caches ($wgMainCacheType,
132 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
133 * be an alias to the configured cache choice for that.
134 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
135 * then CACHE_ANYTHING will forward to CACHE_DB.
137 * @param array $params
140 static function newAnything( $params ) {
141 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
142 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
143 foreach ( $candidates as $candidate ) {
144 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
145 return self
::getInstance( $candidate );
148 return self
::getInstance( CACHE_DB
);
152 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
154 * This will look for any APC style server-local cache.
155 * A fallback cache can be specified if none is found.
157 * @param array $params
158 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
160 * @throws MWException
162 static function newAccelerator( $params, $fallback = null ) {
163 if ( function_exists( 'apc_fetch' ) ) {
165 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
167 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
170 if ( $fallback === null ) {
171 throw new MWException( 'CACHE_ACCEL requested but no suitable object ' .
172 'cache is present. You may want to install APC.' );
176 return self
::newFromId( $id );
180 * Factory function that creates a memcached client object.
182 * This always uses the PHP client, since the PECL client has a different
183 * hashing scheme and a different interpretation of the flags bitfield, so
184 * switching between the two clients randomly would be disastrous.
186 * @param array $params
187 * @return MemcachedPhpBagOStuff
189 static function newMemcached( $params ) {
190 return new MemcachedPhpBagOStuff( $params );
194 * Create a new cache object of the specified type
198 * @return WANObjectCache
199 * @throws MWException
201 static function newWANCacheFromId( $id ) {
202 global $wgWANObjectCaches;
204 if ( !isset( $wgWANObjectCaches[$id] ) ) {
205 throw new MWException( "Invalid object cache type \"$id\" requested. " .
206 "It is not present in \$wgWANObjectCaches." );
209 $params = $wgWANObjectCaches[$id];
210 $class = $params['relayerConfig']['class'];
211 $params['relayer'] = new $class( $params['relayerConfig'] );
212 $params['cache'] = self
::newFromId( $params['cacheId'] );
213 $class = $params['class'];
215 return new $class( $params );
219 * Get the main WAN cache object
222 * @return WANObjectCache
224 static function getMainWANInstance() {
225 global $wgMainWANCache;
227 return self
::getWANInstance( $wgMainWANCache );
231 * Stash objects are BagOStuff instances suitable for storing light
232 * weight data that is not canonically stored elsewhere (such as RDBMS).
233 * Stashes should be configured to propagate changes to all data-centers.
235 * Callers should be prepared for:
236 * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
237 * - b) Reads to be eventually consistent, e.g. for get()/getMulti()
238 * In general, this means avoiding updates on idempotent HTTP requests and
239 * avoiding an assumption of perfect serializability (or accepting anomalies).
240 * Reads may be eventually consistent or data might rollback as nodes flap.
245 static function getMainStashInstance() {
248 return self
::getInstance( $wgMainStash );