Merge "$wgUseSquid unused since parent commit (fa1a8d5)"
[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 static $instances = array();
31
32 /**
33 * Get a cached instance of the specified type of cache object.
34 *
35 * @param $id
36 *
37 * @return object
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 $id
60 *
61 * @return ObjectCache
62 */
63 static function newFromId( $id ) {
64 global $wgObjectCaches;
65
66 if ( !isset( $wgObjectCaches[$id] ) ) {
67 throw new MWException( "Invalid object cache type \"$id\" requested. " .
68 "It is not present in \$wgObjectCaches." );
69 }
70
71 return self::newFromParams( $wgObjectCaches[$id] );
72 }
73
74 /**
75 * Create a new cache object from parameters
76 *
77 * @param $params array
78 *
79 * @return ObjectCache
80 */
81 static function newFromParams( $params ) {
82 if ( isset( $params['factory'] ) ) {
83 return call_user_func( $params['factory'], $params );
84 } elseif ( isset( $params['class'] ) ) {
85 $class = $params['class'];
86 return new $class( $params );
87 } else {
88 throw new MWException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " .
89 "factory and class parameters." );
90 }
91 }
92
93 /**
94 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
95 *
96 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
97 * If a caching method is configured for any of the main caches ($wgMainCacheType,
98 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
99 * be an alias to the configured cache choice for that.
100 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
101 * then CACHE_ANYTHING will forward to CACHE_DB.
102 */
103 static function newAnything( $params ) {
104 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
105 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
106 foreach ( $candidates as $candidate ) {
107 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
108 return self::getInstance( $candidate );
109 }
110 }
111 return self::getInstance( CACHE_DB );
112 }
113
114 /**
115 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
116 *
117 * @return ObjectCache
118 */
119 static function newAccelerator( $params ) {
120 if ( function_exists( 'apc_fetch') ) {
121 $id = 'apc';
122 } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
123 $id = 'xcache';
124 } elseif( function_exists( 'wincache_ucache_get' ) ) {
125 $id = 'wincache';
126 } else {
127 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
128 "cache is present. You may want to install APC." );
129 }
130 return self::newFromId( $id );
131 }
132
133 /**
134 * Factory function that creates a memcached client object.
135 *
136 * This always uses the PHP client, since the PECL client has a different
137 * hashing scheme and a different interpretation of the flags bitfield, so
138 * switching between the two clients randomly would be disasterous.
139 *
140 * @param $params array
141 *
142 * @return MemcachedPhpBagOStuff
143 */
144 static function newMemcached( $params ) {
145 return new MemcachedPhpBagOStuff( $params );
146 }
147 }