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)
36 * The former should always use strongly consistent stores, so callers don't
37 * have to deal with stale reads. The later may be eventually consistent, but
38 * callers can use BagOStuff:READ_LATEST to see the latest available data.
43 /** @var Array Map of (id => BagOStuff) */
44 public static $instances = array();
46 /** @var Array Map of (id => WANObjectCache) */
47 public static $wanInstances = array();
50 * Get a cached instance of the specified type of cache object.
55 static function getInstance( $id ) {
56 if ( !isset( self
::$instances[$id] ) ) {
57 self
::$instances[$id] = self
::newFromId( $id );
60 return self
::$instances[$id];
64 * Get a cached instance of the specified type of cache object.
68 * @return WANObjectCache
70 static function getWANInstance( $id ) {
71 if ( !isset( self
::$wanInstances[$id] ) ) {
72 self
::$wanInstances[$id] = self
::newWANCacheFromId( $id );
75 return self
::$wanInstances[$id];
79 * Clear all the cached instances.
81 static function clear() {
82 self
::$instances = array();
83 self
::$wanInstances = array();
87 * Create a new cache object of the specified type.
93 static function newFromId( $id ) {
94 global $wgObjectCaches;
96 if ( !isset( $wgObjectCaches[$id] ) ) {
97 throw new MWException( "Invalid object cache type \"$id\" requested. " .
98 "It is not present in \$wgObjectCaches." );
101 return self
::newFromParams( $wgObjectCaches[$id] );
105 * Create a new cache object from parameters
107 * @param array $params
109 * @throws MWException
111 static function newFromParams( $params ) {
112 if ( isset( $params['loggroup'] ) ) {
113 $params['logger'] = LoggerFactory
::getInstance( $params['loggroup'] );
115 // For backwards-compatability with custom parameters, lets not
116 // have all logging suddenly disappear
117 $params['logger'] = LoggerFactory
::getInstance( 'objectcache' );
119 if ( isset( $params['factory'] ) ) {
120 return call_user_func( $params['factory'], $params );
121 } elseif ( isset( $params['class'] ) ) {
122 $class = $params['class'];
123 return new $class( $params );
125 throw new MWException( "The definition of cache type \""
126 . print_r( $params, true ) . "\" lacks both "
127 . "factory and class parameters." );
132 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
134 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
135 * If a caching method is configured for any of the main caches ($wgMainCacheType,
136 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
137 * be an alias to the configured cache choice for that.
138 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
139 * then CACHE_ANYTHING will forward to CACHE_DB.
141 * @param array $params
144 static function newAnything( $params ) {
145 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
146 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
147 foreach ( $candidates as $candidate ) {
148 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
149 return self
::getInstance( $candidate );
152 return self
::getInstance( CACHE_DB
);
156 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
158 * This will look for any APC style server-local cache.
159 * A fallback cache can be specified if none is found.
161 * @param array $params
162 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
164 * @throws MWException
166 static function newAccelerator( $params, $fallback = null ) {
167 if ( function_exists( 'apc_fetch' ) ) {
169 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
171 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
174 if ( $fallback === null ) {
175 throw new MWException( 'CACHE_ACCEL requested but no suitable object ' .
176 'cache is present. You may want to install APC.' );
180 return self
::newFromId( $id );
184 * Factory function that creates a memcached client object.
186 * This always uses the PHP client, since the PECL client has a different
187 * hashing scheme and a different interpretation of the flags bitfield, so
188 * switching between the two clients randomly would be disastrous.
190 * @param array $params
191 * @return MemcachedPhpBagOStuff
193 static function newMemcached( $params ) {
194 return new MemcachedPhpBagOStuff( $params );
198 * Create a new cache object of the specified type
202 * @return WANObjectCache
203 * @throws MWException
205 static function newWANCacheFromId( $id ) {
206 global $wgWANObjectCaches;
208 if ( !isset( $wgWANObjectCaches[$id] ) ) {
209 throw new MWException( "Invalid object cache type \"$id\" requested. " .
210 "It is not present in \$wgWANObjectCaches." );
213 $params = $wgWANObjectCaches[$id];
214 $class = $params['relayerConfig']['class'];
215 $params['relayer'] = new $class( $params['relayerConfig'] );
216 $params['cache'] = self
::newFromId( $params['cacheId'] );
217 $class = $params['class'];
219 return new $class( $params );
223 * Get the main WAN cache object
226 * @return WANObjectCache
228 static function getMainWANInstance() {
229 global $wgMainWANCache;
231 return self
::getWANInstance( $wgMainWANCache );
235 * Stash objects are BagOStuff instances suitable for storing light
236 * weight data that is not canonically stored elsewhere (such as RDBMS).
237 * Stashes should be configured to propagate changes to all data-centers.
239 * Callers should be prepared for:
240 * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
241 * - b) Reads to be eventually consistent, e.g. for get()/getMulti()
242 * In general, this means avoiding updates on idempotent HTTP requests and
243 * avoiding an assumption of perfect serializability (or accepting anomalies).
244 * Reads may be eventually consistent or data might rollback as nodes flap.
245 * Callers can use BagOStuff:READ_LATEST to see the latest available data.
250 static function getMainStashInstance() {
253 return self
::getInstance( $wgMainStash );