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
61 static function getWANInstance( $id ) {
62 if ( isset( self
::$wanInstances[$id] ) ) {
63 return self
::$wanInstances[$id];
66 $object = self
::newWANCacheFromId( $id );
67 self
::$wanInstances[$id] = $object;
72 * Clear all the cached instances.
74 static function clear() {
75 self
::$instances = array();
76 self
::$wanInstances = array();
80 * Create a new cache object of the specified type.
87 static function newFromId( $id ) {
88 global $wgObjectCaches;
90 if ( !isset( $wgObjectCaches[$id] ) ) {
91 throw new MWException( "Invalid object cache type \"$id\" requested. " .
92 "It is not present in \$wgObjectCaches." );
95 return self
::newFromParams( $wgObjectCaches[$id] );
99 * Create a new cache object from parameters
101 * @param array $params
103 * @throws MWException
106 static function newFromParams( $params ) {
107 if ( isset( $params['loggroup'] ) ) {
108 $params['logger'] = LoggerFactory
::getInstance( $params['loggroup'] );
110 // For backwards-compatability with custom parameters, lets not
111 // have all logging suddenly disappear
112 $params['logger'] = LoggerFactory
::getInstance( 'objectcache' );
114 if ( isset( $params['factory'] ) ) {
115 return call_user_func( $params['factory'], $params );
116 } elseif ( isset( $params['class'] ) ) {
117 $class = $params['class'];
118 return new $class( $params );
120 throw new MWException( "The definition of cache type \""
121 . print_r( $params, true ) . "\" lacks both "
122 . "factory and class parameters." );
127 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
129 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
130 * If a caching method is configured for any of the main caches ($wgMainCacheType,
131 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
132 * be an alias to the configured cache choice for that.
133 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
134 * then CACHE_ANYTHING will forward to CACHE_DB.
135 * @param array $params
138 static function newAnything( $params ) {
139 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
140 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
141 foreach ( $candidates as $candidate ) {
142 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
143 return self
::getInstance( $candidate );
146 return self
::getInstance( CACHE_DB
);
150 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
152 * This will look for any APC style server-local cache.
153 * A fallback cache can be specified if none is found.
155 * @param array $params
156 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
157 * @throws MWException
160 static function newAccelerator( $params, $fallback = null ) {
161 if ( function_exists( 'apc_fetch' ) ) {
163 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
165 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
168 if ( $fallback !== null ) {
169 return self
::newFromId( $fallback );
171 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
172 "cache is present. You may want to install APC." );
174 return self
::newFromId( $id );
178 * Factory function that creates a memcached client object.
180 * This always uses the PHP client, since the PECL client has a different
181 * hashing scheme and a different interpretation of the flags bitfield, so
182 * switching between the two clients randomly would be disastrous.
184 * @param array $params
186 * @return MemcachedPhpBagOStuff
188 static function newMemcached( $params ) {
189 return new MemcachedPhpBagOStuff( $params );
193 * Create a new cache object of the specified type
197 * @throws MWException
198 * @return WANObjectCache
200 static function newWANCacheFromId( $id ) {
201 global $wgWANObjectCaches;
203 if ( !isset( $wgWANObjectCaches[$id] ) ) {
204 throw new MWException( "Invalid object cache type \"$id\" requested. " .
205 "It is not present in \$wgWANObjectCaches." );
208 $params = $wgWANObjectCaches[$id];
209 $class = $params['relayerConfig']['class'];
210 $params['relayer'] = new $class( $params['relayerConfig'] );
211 $params['cache'] = self
::newFromId( $params['cacheId'] );
212 $class = $params['class'];
214 return new $class( $params );