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
32 /** @var Array Map of (id => BagOStuff) */
33 public static $instances = array();
34 /** @var Array Map of (id => WANObjectCache) */
35 public static $wanInstances = array();
38 * Get a cached instance of the specified type of cache object.
44 static function getInstance( $id ) {
45 if ( isset( self
::$instances[$id] ) ) {
46 return self
::$instances[$id];
49 $object = self
::newFromId( $id );
50 self
::$instances[$id] = $object;
55 * Get a cached instance of the specified type of cache object.
59 * @return WANObjectCache
62 static function getWANInstance( $id ) {
63 if ( isset( self
::$wanInstances[$id] ) ) {
64 return self
::$wanInstances[$id];
67 $object = self
::newWANCacheFromId( $id );
68 self
::$wanInstances[$id] = $object;
73 * Clear all the cached instances.
75 static function clear() {
76 self
::$instances = array();
77 self
::$wanInstances = array();
81 * Create a new cache object of the specified type.
88 static function newFromId( $id ) {
89 global $wgObjectCaches;
91 if ( !isset( $wgObjectCaches[$id] ) ) {
92 throw new MWException( "Invalid object cache type \"$id\" requested. " .
93 "It is not present in \$wgObjectCaches." );
96 return self
::newFromParams( $wgObjectCaches[$id] );
100 * Create a new cache object from parameters
102 * @param array $params
104 * @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.
136 * @param array $params
139 static function newAnything( $params ) {
140 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
141 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
142 foreach ( $candidates as $candidate ) {
143 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
144 return self
::getInstance( $candidate );
147 return self
::getInstance( CACHE_DB
);
151 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
153 * This will look for any APC style server-local cache.
154 * A fallback cache can be specified if none is found.
156 * @param array $params
157 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
158 * @throws MWException
161 static function newAccelerator( $params, $fallback = null ) {
162 if ( function_exists( 'apc_fetch' ) ) {
164 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
166 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
169 if ( $fallback !== null ) {
170 return self
::newFromId( $fallback );
172 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
173 "cache is present. You may want to install APC." );
175 return self
::newFromId( $id );
179 * Factory function that creates a memcached client object.
181 * This always uses the PHP client, since the PECL client has a different
182 * hashing scheme and a different interpretation of the flags bitfield, so
183 * switching between the two clients randomly would be disastrous.
185 * @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 * @throws MWException
199 * @return WANObjectCache
202 static function newWANCacheFromId( $id ) {
203 global $wgWANObjectCaches;
205 if ( !isset( $wgWANObjectCaches[$id] ) ) {
206 throw new MWException( "Invalid object cache type \"$id\" requested. " .
207 "It is not present in \$wgWANObjectCaches." );
210 $params = $wgWANObjectCaches[$id];
211 $class = $params['relayerConfig']['class'];
212 $params['relayer'] = new $class( $params['relayerConfig'] );
213 $params['cache'] = self
::newFromId( $params['cacheId'] );
214 $class = $params['class'];
216 return new $class( $params );
220 * Get the main WAN cache object
222 * @return WANObjectCache
225 static function getMainWANInstance() {
226 global $wgMainWANCache;
228 return self
::getWANInstance( $wgMainWANCache );