Merge "Instrument edit failures"
[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 * - 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)
35 *
36 * @ingroup Cache
37 */
38 class ObjectCache {
39 /** @var Array Map of (id => BagOStuff) */
40 public static $instances = array();
41
42 /** @var Array Map of (id => WANObjectCache) */
43 public static $wanInstances = array();
44
45 /**
46 * Get a cached instance of the specified type of cache object.
47 *
48 * @param string $id
49 * @return BagOStuff
50 */
51 static function getInstance( $id ) {
52 if ( !isset( self::$instances[$id] ) ) {
53 self::$instances[$id] = self::newFromId( $id );
54 }
55
56 return self::$instances[$id];
57 }
58
59 /**
60 * Get a cached instance of the specified type of cache object.
61 *
62 * @since 1.26
63 * @param string $id
64 * @return WANObjectCache
65 */
66 static function getWANInstance( $id ) {
67 if ( !isset( self::$wanInstances[$id] ) ) {
68 self::$wanInstances[$id] = self::newWANCacheFromId( $id );
69 }
70
71 return self::$wanInstances[$id];
72 }
73
74 /**
75 * Clear all the cached instances.
76 */
77 static function clear() {
78 self::$instances = array();
79 self::$wanInstances = array();
80 }
81
82 /**
83 * Create a new cache object of the specified type.
84 *
85 * @param string $id
86 * @return BagOStuff
87 * @throws MWException
88 */
89 static function newFromId( $id ) {
90 global $wgObjectCaches;
91
92 if ( !isset( $wgObjectCaches[$id] ) ) {
93 throw new MWException( "Invalid object cache type \"$id\" requested. " .
94 "It is not present in \$wgObjectCaches." );
95 }
96
97 return self::newFromParams( $wgObjectCaches[$id] );
98 }
99
100 /**
101 * Create a new cache object from parameters
102 *
103 * @param array $params
104 * @return BagOStuff
105 * @throws MWException
106 */
107 static function newFromParams( $params ) {
108 if ( isset( $params['loggroup'] ) ) {
109 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
110 } else {
111 // For backwards-compatability with custom parameters, lets not
112 // have all logging suddenly disappear
113 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
114 }
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 );
120 } else {
121 throw new MWException( "The definition of cache type \""
122 . print_r( $params, true ) . "\" lacks both "
123 . "factory and class parameters." );
124 }
125 }
126
127 /**
128 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
129 *
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 *
137 * @param array $params
138 * @return BagOStuff
139 */
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 );
146 }
147 }
148 return self::getInstance( CACHE_DB );
149 }
150
151 /**
152 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
153 *
154 * This will look for any APC style server-local cache.
155 * A fallback cache can be specified if none is found.
156 *
157 * @param array $params
158 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
159 * @return BagOStuff
160 * @throws MWException
161 */
162 static function newAccelerator( $params, $fallback = null ) {
163 if ( function_exists( 'apc_fetch' ) ) {
164 $id = 'apc';
165 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
166 $id = 'xcache';
167 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
168 $id = 'wincache';
169 } else {
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.' );
173 }
174 $id = $fallback;
175 }
176 return self::newFromId( $id );
177 }
178
179 /**
180 * Factory function that creates a memcached client object.
181 *
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.
185 *
186 * @param array $params
187 * @return MemcachedPhpBagOStuff
188 */
189 static function newMemcached( $params ) {
190 return new MemcachedPhpBagOStuff( $params );
191 }
192
193 /**
194 * Create a new cache object of the specified type
195 *
196 * @since 1.26
197 * @param string $id
198 * @return WANObjectCache
199 * @throws MWException
200 */
201 static function newWANCacheFromId( $id ) {
202 global $wgWANObjectCaches;
203
204 if ( !isset( $wgWANObjectCaches[$id] ) ) {
205 throw new MWException( "Invalid object cache type \"$id\" requested. " .
206 "It is not present in \$wgWANObjectCaches." );
207 }
208
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'];
214
215 return new $class( $params );
216 }
217
218 /**
219 * Get the main WAN cache object
220 *
221 * @since 1.26
222 * @return WANObjectCache
223 */
224 static function getMainWANInstance() {
225 global $wgMainWANCache;
226
227 return self::getWANInstance( $wgMainWANCache );
228 }
229
230 /**
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.
234 *
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.
241 *
242 * @since 1.26
243 * @return BagOStuff
244 */
245 static function getMainStashInstance() {
246 global $wgMainStash;
247
248 return self::getInstance( $wgMainStash );
249 }
250 }