Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[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 /** @var Array Map of (id => WANObjectCache) */
42 public static $wanInstances = array();
43
44 /**
45 * Get a cached instance of the specified type of cache object.
46 *
47 * @param string $id
48 *
49 * @return BagOStuff
50 */
51 static function getInstance( $id ) {
52 if ( isset( self::$instances[$id] ) ) {
53 return self::$instances[$id];
54 }
55
56 $object = self::newFromId( $id );
57 self::$instances[$id] = $object;
58 return $object;
59 }
60
61 /**
62 * Get a cached instance of the specified type of cache object.
63 *
64 * @param string $id
65 *
66 * @return WANObjectCache
67 * @since 1.26
68 */
69 static function getWANInstance( $id ) {
70 if ( isset( self::$wanInstances[$id] ) ) {
71 return self::$wanInstances[$id];
72 }
73
74 $object = self::newWANCacheFromId( $id );
75 self::$wanInstances[$id] = $object;
76 return $object;
77 }
78
79 /**
80 * Clear all the cached instances.
81 */
82 static function clear() {
83 self::$instances = array();
84 self::$wanInstances = array();
85 }
86
87 /**
88 * Create a new cache object of the specified type.
89 *
90 * @param string $id
91 *
92 * @throws MWException
93 * @return BagOStuff
94 */
95 static function newFromId( $id ) {
96 global $wgObjectCaches;
97
98 if ( !isset( $wgObjectCaches[$id] ) ) {
99 throw new MWException( "Invalid object cache type \"$id\" requested. " .
100 "It is not present in \$wgObjectCaches." );
101 }
102
103 return self::newFromParams( $wgObjectCaches[$id] );
104 }
105
106 /**
107 * Create a new cache object from parameters
108 *
109 * @param array $params
110 *
111 * @throws MWException
112 * @return BagOStuff
113 */
114 static function newFromParams( $params ) {
115 if ( isset( $params['loggroup'] ) ) {
116 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
117 } else {
118 // For backwards-compatability with custom parameters, lets not
119 // have all logging suddenly disappear
120 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
121 }
122 if ( isset( $params['factory'] ) ) {
123 return call_user_func( $params['factory'], $params );
124 } elseif ( isset( $params['class'] ) ) {
125 $class = $params['class'];
126 return new $class( $params );
127 } else {
128 throw new MWException( "The definition of cache type \""
129 . print_r( $params, true ) . "\" lacks both "
130 . "factory and class parameters." );
131 }
132 }
133
134 /**
135 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
136 *
137 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
138 * If a caching method is configured for any of the main caches ($wgMainCacheType,
139 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
140 * be an alias to the configured cache choice for that.
141 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
142 * then CACHE_ANYTHING will forward to CACHE_DB.
143 * @param array $params
144 * @return BagOStuff
145 */
146 static function newAnything( $params ) {
147 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
148 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
149 foreach ( $candidates as $candidate ) {
150 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
151 return self::getInstance( $candidate );
152 }
153 }
154 return self::getInstance( CACHE_DB );
155 }
156
157 /**
158 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
159 *
160 * This will look for any APC style server-local cache.
161 * A fallback cache can be specified if none is found.
162 *
163 * @param array $params
164 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
165 * @throws MWException
166 * @return BagOStuff
167 */
168 static function newAccelerator( $params, $fallback = null ) {
169 if ( function_exists( 'apc_fetch' ) ) {
170 $id = 'apc';
171 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
172 $id = 'xcache';
173 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
174 $id = 'wincache';
175 } else {
176 if ( $fallback !== null ) {
177 return self::newFromId( $fallback );
178 }
179 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
180 "cache is present. You may want to install APC." );
181 }
182 return self::newFromId( $id );
183 }
184
185 /**
186 * Factory function that creates a memcached client object.
187 *
188 * This always uses the PHP client, since the PECL client has a different
189 * hashing scheme and a different interpretation of the flags bitfield, so
190 * switching between the two clients randomly would be disastrous.
191 *
192 * @param array $params
193 *
194 * @return MemcachedPhpBagOStuff
195 */
196 static function newMemcached( $params ) {
197 return new MemcachedPhpBagOStuff( $params );
198 }
199
200 /**
201 * Create a new cache object of the specified type
202 *
203 * @param string $id
204 *
205 * @throws MWException
206 * @return WANObjectCache
207 * @since 1.26
208 */
209 static function newWANCacheFromId( $id ) {
210 global $wgWANObjectCaches;
211
212 if ( !isset( $wgWANObjectCaches[$id] ) ) {
213 throw new MWException( "Invalid object cache type \"$id\" requested. " .
214 "It is not present in \$wgWANObjectCaches." );
215 }
216
217 $params = $wgWANObjectCaches[$id];
218 $class = $params['relayerConfig']['class'];
219 $params['relayer'] = new $class( $params['relayerConfig'] );
220 $params['cache'] = self::newFromId( $params['cacheId'] );
221 $class = $params['class'];
222
223 return new $class( $params );
224 }
225
226 /**
227 * Get the main WAN cache object
228 *
229 * @return WANObjectCache
230 * @since 1.26
231 */
232 static function getMainWANInstance() {
233 global $wgMainWANCache;
234
235 return self::getWANInstance( $wgMainWANCache );
236 }
237
238 /**
239 * Stash objects are BagOStuff instances suitable for storing light
240 * weight data that is not canonically stored elsewhere (such as RDBMS).
241 * Stashes should be configured to propagate changes to all data-centers.
242 *
243 * Callers should be prepared for:
244 * - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
245 * - b) Reads to be eventually consistent, e.g. for get()/getMulti()
246 * In general, this means avoiding updates on idempotent HTTP requests and
247 * avoiding an assumption of perfect serializability (or accepting anomalies).
248 * Reads may be eventually consistent or data might rollback as nodes flap.
249 *
250 *
251 * @return BagOStuff
252 * @since 1.26
253 */
254 static function getMainStashInstance() {
255 global $wgMainStash;
256
257 return self::getInstance( $wgMainStash );
258 }
259 }