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