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