Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / objectcache / ObjectCache.php
blobf7247f92f581eb91db42ad6832022cdce450f47b
1 <?php
2 /**
3 * Functions to get cache objects.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Functions to get cache objects
27 * @ingroup Cache
29 class ObjectCache {
30 static $instances = array();
32 /**
33 * Get a cached instance of the specified type of cache object.
35 * @param $id string
37 * @return ObjectCache
39 static function getInstance( $id ) {
40 if ( isset( self::$instances[$id] ) ) {
41 return self::$instances[$id];
44 $object = self::newFromId( $id );
45 self::$instances[$id] = $object;
46 return $object;
49 /**
50 * Clear all the cached instances.
52 static function clear() {
53 self::$instances = array();
56 /**
57 * Create a new cache object of the specified type.
59 * @param $id string
61 * @throws MWException
62 * @return ObjectCache
64 static function newFromId( $id ) {
65 global $wgObjectCaches;
67 if ( !isset( $wgObjectCaches[$id] ) ) {
68 throw new MWException( "Invalid object cache type \"$id\" requested. " .
69 "It is not present in \$wgObjectCaches." );
72 return self::newFromParams( $wgObjectCaches[$id] );
75 /**
76 * Create a new cache object from parameters
78 * @param $params array
80 * @throws MWException
81 * @return ObjectCache
83 static function newFromParams( $params ) {
84 if ( isset( $params['factory'] ) ) {
85 return call_user_func( $params['factory'], $params );
86 } elseif ( isset( $params['class'] ) ) {
87 $class = $params['class'];
88 return new $class( $params );
89 } else {
90 throw new MWException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " .
91 "factory and class parameters." );
95 /**
96 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
98 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
99 * If a caching method is configured for any of the main caches ($wgMainCacheType,
100 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
101 * be an alias to the configured cache choice for that.
102 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
103 * then CACHE_ANYTHING will forward to CACHE_DB.
104 * @param $params array
105 * @return ObjectCache
107 static function newAnything( $params ) {
108 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
109 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
110 foreach ( $candidates as $candidate ) {
111 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
112 return self::getInstance( $candidate );
115 return self::getInstance( CACHE_DB );
119 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
121 * @param $params array
122 * @throws MWException
123 * @return ObjectCache
125 static function newAccelerator( $params ) {
126 if ( function_exists( 'apc_fetch' ) ) {
127 $id = 'apc';
128 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
129 $id = 'xcache';
130 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
131 $id = 'wincache';
132 } else {
133 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
134 "cache is present. You may want to install APC." );
136 return self::newFromId( $id );
140 * Factory function that creates a memcached client object.
142 * This always uses the PHP client, since the PECL client has a different
143 * hashing scheme and a different interpretation of the flags bitfield, so
144 * switching between the two clients randomly would be disastrous.
146 * @param $params array
148 * @return MemcachedPhpBagOStuff
150 static function newMemcached( $params ) {
151 return new MemcachedPhpBagOStuff( $params );