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