Added WANObjectCache class
[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 * @ingroup Cache
30 */
31 class ObjectCache {
32 /** @var Array Map of (id => BagOStuff) */
33 public static $instances = array();
34 /** @var Array Map of (id => WANObjectCache) */
35 public static $wanInstances = array();
36
37 /**
38 * Get a cached instance of the specified type of cache object.
39 *
40 * @param string $id
41 *
42 * @return BagOStuff
43 */
44 static function getInstance( $id ) {
45 if ( isset( self::$instances[$id] ) ) {
46 return self::$instances[$id];
47 }
48
49 $object = self::newFromId( $id );
50 self::$instances[$id] = $object;
51 return $object;
52 }
53
54 /**
55 * Get a cached instance of the specified type of cache object.
56 *
57 * @param string $id
58 *
59 * @return WANObjectCache
60 */
61 static function getWANInstance( $id ) {
62 if ( isset( self::$wanInstances[$id] ) ) {
63 return self::$wanInstances[$id];
64 }
65
66 $object = self::newWANCacheFromId( $id );
67 self::$wanInstances[$id] = $object;
68 return $object;
69 }
70
71 /**
72 * Clear all the cached instances.
73 */
74 static function clear() {
75 self::$instances = array();
76 self::$wanInstances = array();
77 }
78
79 /**
80 * Create a new cache object of the specified type.
81 *
82 * @param string $id
83 *
84 * @throws MWException
85 * @return BagOStuff
86 */
87 static function newFromId( $id ) {
88 global $wgObjectCaches;
89
90 if ( !isset( $wgObjectCaches[$id] ) ) {
91 throw new MWException( "Invalid object cache type \"$id\" requested. " .
92 "It is not present in \$wgObjectCaches." );
93 }
94
95 return self::newFromParams( $wgObjectCaches[$id] );
96 }
97
98 /**
99 * Create a new cache object from parameters
100 *
101 * @param array $params
102 *
103 * @throws MWException
104 * @return BagOStuff
105 */
106 static function newFromParams( $params ) {
107 if ( isset( $params['loggroup'] ) ) {
108 $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] );
109 } else {
110 // For backwards-compatability with custom parameters, lets not
111 // have all logging suddenly disappear
112 $params['logger'] = LoggerFactory::getInstance( 'objectcache' );
113 }
114 if ( isset( $params['factory'] ) ) {
115 return call_user_func( $params['factory'], $params );
116 } elseif ( isset( $params['class'] ) ) {
117 $class = $params['class'];
118 return new $class( $params );
119 } else {
120 throw new MWException( "The definition of cache type \""
121 . print_r( $params, true ) . "\" lacks both "
122 . "factory and class parameters." );
123 }
124 }
125
126 /**
127 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
128 *
129 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
130 * If a caching method is configured for any of the main caches ($wgMainCacheType,
131 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
132 * be an alias to the configured cache choice for that.
133 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
134 * then CACHE_ANYTHING will forward to CACHE_DB.
135 * @param array $params
136 * @return BagOStuff
137 */
138 static function newAnything( $params ) {
139 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
140 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
141 foreach ( $candidates as $candidate ) {
142 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
143 return self::getInstance( $candidate );
144 }
145 }
146 return self::getInstance( CACHE_DB );
147 }
148
149 /**
150 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
151 *
152 * This will look for any APC style server-local cache.
153 * A fallback cache can be specified if none is found.
154 *
155 * @param array $params
156 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
157 * @throws MWException
158 * @return BagOStuff
159 */
160 static function newAccelerator( $params, $fallback = null ) {
161 if ( function_exists( 'apc_fetch' ) ) {
162 $id = 'apc';
163 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
164 $id = 'xcache';
165 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
166 $id = 'wincache';
167 } else {
168 if ( $fallback !== null ) {
169 return self::newFromId( $fallback );
170 }
171 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
172 "cache is present. You may want to install APC." );
173 }
174 return self::newFromId( $id );
175 }
176
177 /**
178 * Factory function that creates a memcached client object.
179 *
180 * This always uses the PHP client, since the PECL client has a different
181 * hashing scheme and a different interpretation of the flags bitfield, so
182 * switching between the two clients randomly would be disastrous.
183 *
184 * @param array $params
185 *
186 * @return MemcachedPhpBagOStuff
187 */
188 static function newMemcached( $params ) {
189 return new MemcachedPhpBagOStuff( $params );
190 }
191
192 /**
193 * Create a new cache object of the specified type
194 *
195 * @param string $id
196 *
197 * @throws MWException
198 * @return WANObjectCache
199 */
200 static function newWANCacheFromId( $id ) {
201 global $wgWANObjectCaches;
202
203 if ( !isset( $wgWANObjectCaches[$id] ) ) {
204 throw new MWException( "Invalid object cache type \"$id\" requested. " .
205 "It is not present in \$wgWANObjectCaches." );
206 }
207
208 $params = $wgWANObjectCaches[$id];
209 $class = $params['relayerConfig']['class'];
210 $params['relayer'] = new $class( $params['relayerConfig'] );
211 $params['cache'] = self::newFromId( $params['cacheId'] );
212 $class = $params['class'];
213
214 return new $class( $params );
215 }
216 }