Merge "ResourcesOOUI: Remove now-unnecessary selector"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
1 <?php
2 /**
3 * Functions to get cache objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 use MediaWiki\Logger\LoggerFactory;
25
26 /**
27 * Functions to get cache objects
28 *
29 * The word "cache" has two main dictionary meanings, and both
30 * are used in this factory class. They are:
31 *
32 * - a) Cache (the computer science definition).
33 * A place to store copies or computations on existing data for
34 * higher access speeds.
35 * - b) Storage.
36 * A place to store lightweight data that is not canonically
37 * stored anywhere else (e.g. a "hoard" of objects).
38 *
39 * The former should always use strongly consistent stores, so callers don't
40 * have to deal with stale reads. The later may be eventually consistent, but
41 * callers can use BagOStuff:READ_LATEST to see the latest available data.
42 *
43 * Primary entry points:
44 *
45 * - ObjectCache::newAccelerator( $fallbackType )
46 * Purpose: Cache.
47 * Stored only on the individual web server.
48 * Not associated with other servers.
49 *
50 * - wfGetMainCache()
51 * Purpose: Cache.
52 * Stored centrally within the local data-center.
53 * Not replicated to other DCs.
54 * Also known as $wgMemc. Configured by $wgMainCacheType.
55 *
56 * - ObjectCache::getMainWANInstance()
57 * Purpose: Cache.
58 * Stored in the local data-center's main cache (uses different cache keys).
59 * Delete events are broadcasted to other DCs. See WANObjectCache for details.
60 *
61 * - ObjectCache::getMainStashInstance()
62 * Purpose: Storage.
63 * Stored centrally within the local data-center.
64 * Changes are replicated to other DCs (eventually consistent).
65 * To retrieve the latest value (e.g. not from a slave), use BagOStuff:READ_LATEST.
66 *
67 * - wfGetCache( $cacheType )
68 * Get a specific cache type by key in $wgObjectCaches.
69 *
70 * @ingroup Cache
71 */
72 class ObjectCache {
73 /** @var Array Map of (id => BagOStuff) */
74 public static $instances = array();
75
76 /** @var Array Map of (id => WANObjectCache) */
77 public static $wanInstances = array();
78
79 /**
80 * Get a cached instance of the specified type of cache object.
81 *
82 * @param string $id A key in $wgObjectCaches.
83 * @return BagOStuff
84 */
85 public static function getInstance( $id ) {
86 if ( !isset( self::$instances[$id] ) ) {
87 self::$instances[$id] = self::newFromId( $id );
88 }
89
90 return self::$instances[$id];
91 }
92
93 /**
94 * Get a cached instance of the specified type of WAN cache object.
95 *
96 * @since 1.26
97 * @param string $id A key in $wgWANObjectCaches.
98 * @return WANObjectCache
99 */
100 public static function getWANInstance( $id ) {
101 if ( !isset( self::$wanInstances[$id] ) ) {
102 self::$wanInstances[$id] = self::newWANCacheFromId( $id );
103 }
104
105 return self::$wanInstances[$id];
106 }
107
108 /**
109 * Create a new cache object of the specified type.
110 *
111 * @param string $id A key in $wgObjectCaches.
112 * @return BagOStuff
113 * @throws MWException
114 */
115 public static function newFromId( $id ) {
116 global $wgObjectCaches;
117
118 if ( !isset( $wgObjectCaches[$id] ) ) {
119 throw new MWException( "Invalid object cache type \"$id\" requested. " .
120 "It is not present in \$wgObjectCaches." );
121 }
122
123 return self::newFromParams( $wgObjectCaches[$id] );
124 }
125
126 /**
127 * Create a new cache object from parameters.
128 *
129 * @param array $params Must have 'factory' or 'class' property.
130 * - factory: Callback passed $params that returns BagOStuff.
131 * - class: BagOStuff subclass constructed with $params.
132 * - loggroup: Alias to set 'logger' key with LoggerFactory group.
133 * - .. Other parameters passed to factory or class.
134 * @return BagOStuff
135 * @throws MWException
136 */
137 public static function newFromParams( $params ) {
138 if ( isset( $params['loggroup'] ) ) {
139 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
140 } else {
141 // For backwards-compatability with custom parameters, lets not
142 // have all logging suddenly disappear
143 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
144 }
145 if ( isset( $params['factory'] ) ) {
146 return call_user_func( $params['factory'], $params );
147 } elseif ( isset( $params['class'] ) ) {
148 $class = $params['class'];
149 return new $class( $params );
150 } else {
151 throw new MWException( "The definition of cache type \""
152 . print_r( $params, true ) . "\" lacks both "
153 . "factory and class parameters." );
154 }
155 }
156
157 /**
158 * Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
159 *
160 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
161 * If a caching method is configured for any of the main caches ($wgMainCacheType,
162 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
163 * be an alias to the configured cache choice for that.
164 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
165 * then CACHE_ANYTHING will forward to CACHE_DB.
166 *
167 * @param array $params
168 * @return BagOStuff
169 */
170 public static function newAnything( $params ) {
171 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
172 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
173 foreach ( $candidates as $candidate ) {
174 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
175 return self::getInstance( $candidate );
176 }
177 }
178 return self::getInstance( CACHE_DB );
179 }
180
181 /**
182 * Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
183 *
184 * This will look for any APC style server-local cache.
185 * A fallback cache can be specified if none is found.
186 *
187 * @param array $params [optional]
188 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
189 * @return BagOStuff
190 * @throws MWException
191 */
192 public static function newAccelerator( $params = array(), $fallback = null ) {
193 if ( !is_array( $params ) && $fallback === null ) {
194 $fallback = $params;
195 }
196 if ( function_exists( 'apc_fetch' ) ) {
197 $id = 'apc';
198 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
199 $id = 'xcache';
200 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
201 $id = 'wincache';
202 } else {
203 if ( $fallback === null ) {
204 throw new MWException( 'CACHE_ACCEL requested but no suitable object ' .
205 'cache is present. You may want to install APC.' );
206 }
207 $id = $fallback;
208 }
209 return self::newFromId( $id );
210 }
211
212 /**
213 * Factory function that creates a memcached client object.
214 *
215 * This always uses the PHP client, since the PECL client has a different
216 * hashing scheme and a different interpretation of the flags bitfield, so
217 * switching between the two clients randomly would be disastrous.
218 *
219 * @param array $params
220 * @return MemcachedPhpBagOStuff
221 */
222 public static function newMemcached( $params ) {
223 return new MemcachedPhpBagOStuff( $params );
224 }
225
226 /**
227 * Create a new cache object of the specified type.
228 *
229 * @since 1.26
230 * @param string $id A key in $wgWANObjectCaches.
231 * @return WANObjectCache
232 * @throws MWException
233 */
234 public static function newWANCacheFromId( $id ) {
235 global $wgWANObjectCaches;
236
237 if ( !isset( $wgWANObjectCaches[$id] ) ) {
238 throw new MWException( "Invalid object cache type \"$id\" requested. " .
239 "It is not present in \$wgWANObjectCaches." );
240 }
241
242 $params = $wgWANObjectCaches[$id];
243 $class = $params['relayerConfig']['class'];
244 $params['relayer'] = new $class( $params['relayerConfig'] );
245 $params['cache'] = self::newFromId( $params['cacheId'] );
246 $class = $params['class'];
247
248 return new $class( $params );
249 }
250
251 /**
252 * Get the main WAN cache object.
253 *
254 * @since 1.26
255 * @return WANObjectCache
256 */
257 public static function getMainWANInstance() {
258 global $wgMainWANCache;
259
260 return self::getWANInstance( $wgMainWANCache );
261 }
262
263 /**
264 * Get the cache object for the main stash.
265 *
266 * Stash objects are BagOStuff instances suitable for storing light
267 * weight data that is not canonically stored elsewhere (such as RDBMS).
268 * Stashes should be configured to propagate changes to all data-centers.
269 *
270 * Callers should be prepared for:
271 * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
272 * - b) Reads to be eventually consistent, e.g. for get()/getMulti()
273 * In general, this means avoiding updates on idempotent HTTP requests and
274 * avoiding an assumption of perfect serializability (or accepting anomalies).
275 * Reads may be eventually consistent or data might rollback as nodes flap.
276 * Callers can use BagOStuff:READ_LATEST to see the latest available data.
277 *
278 * @return BagOStuff
279 * @since 1.26
280 */
281 public static function getMainStashInstance() {
282 global $wgMainStash;
283
284 return self::getInstance( $wgMainStash );
285 }
286
287 /**
288 * Clear all the cached instances.
289 */
290 public static function clear() {
291 self::$instances = array();
292 self::$wanInstances = array();
293 }
294 }