Merge "Removed "empty" cache key from JobQueueDB for simplicity"
[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 * @since 1.26
61 */
62 static function getWANInstance( $id ) {
63 if ( isset( self::$wanInstances[$id] ) ) {
64 return self::$wanInstances[$id];
65 }
66
67 $object = self::newWANCacheFromId( $id );
68 self::$wanInstances[$id] = $object;
69 return $object;
70 }
71
72 /**
73 * Clear all the cached instances.
74 */
75 static function clear() {
76 self::$instances = array();
77 self::$wanInstances = array();
78 }
79
80 /**
81 * Create a new cache object of the specified type.
82 *
83 * @param string $id
84 *
85 * @throws MWException
86 * @return BagOStuff
87 */
88 static function newFromId( $id ) {
89 global $wgObjectCaches;
90
91 if ( !isset( $wgObjectCaches[$id] ) ) {
92 throw new MWException( "Invalid object cache type \"$id\" requested. " .
93 "It is not present in \$wgObjectCaches." );
94 }
95
96 return self::newFromParams( $wgObjectCaches[$id] );
97 }
98
99 /**
100 * Create a new cache object from parameters
101 *
102 * @param array $params
103 *
104 * @throws MWException
105 * @return BagOStuff
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 * @param array $params
137 * @return BagOStuff
138 */
139 static function newAnything( $params ) {
140 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
141 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
142 foreach ( $candidates as $candidate ) {
143 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
144 return self::getInstance( $candidate );
145 }
146 }
147 return self::getInstance( CACHE_DB );
148 }
149
150 /**
151 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
152 *
153 * This will look for any APC style server-local cache.
154 * A fallback cache can be specified if none is found.
155 *
156 * @param array $params
157 * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24)
158 * @throws MWException
159 * @return BagOStuff
160 */
161 static function newAccelerator( $params, $fallback = null ) {
162 if ( function_exists( 'apc_fetch' ) ) {
163 $id = 'apc';
164 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
165 $id = 'xcache';
166 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
167 $id = 'wincache';
168 } else {
169 if ( $fallback !== null ) {
170 return self::newFromId( $fallback );
171 }
172 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
173 "cache is present. You may want to install APC." );
174 }
175 return self::newFromId( $id );
176 }
177
178 /**
179 * Factory function that creates a memcached client object.
180 *
181 * This always uses the PHP client, since the PECL client has a different
182 * hashing scheme and a different interpretation of the flags bitfield, so
183 * switching between the two clients randomly would be disastrous.
184 *
185 * @param array $params
186 *
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 * @param string $id
197 *
198 * @throws MWException
199 * @return WANObjectCache
200 * @since 1.26
201 */
202 static function newWANCacheFromId( $id ) {
203 global $wgWANObjectCaches;
204
205 if ( !isset( $wgWANObjectCaches[$id] ) ) {
206 throw new MWException( "Invalid object cache type \"$id\" requested. " .
207 "It is not present in \$wgWANObjectCaches." );
208 }
209
210 $params = $wgWANObjectCaches[$id];
211 $class = $params['relayerConfig']['class'];
212 $params['relayer'] = new $class( $params['relayerConfig'] );
213 $params['cache'] = self::newFromId( $params['cacheId'] );
214 $class = $params['class'];
215
216 return new $class( $params );
217 }
218
219 /**
220 * Get the main WAN cache object
221 *
222 * @return WANObjectCache
223 * @since 1.26
224 */
225 static function getMainWANInstance() {
226 global $wgMainWANCache;
227
228 return self::getWANInstance( $wgMainWANCache );
229 }
230 }