3 * Functions to get cache objects
9 static $instances = array();
12 * Get a cached instance of the specified type of cache object.
18 static function getInstance( $id ) {
19 if ( isset( self
::$instances[$id] ) ) {
20 return self
::$instances[$id];
23 $object = self
::newFromId( $id );
24 self
::$instances[$id] = $object;
29 * Clear all the cached instances.
31 static function clear() {
32 self
::$instances = array();
36 * Create a new cache object of the specified type.
42 static function newFromId( $id ) {
43 global $wgObjectCaches;
45 if ( !isset( $wgObjectCaches[$id] ) ) {
46 throw new MWException( "Invalid object cache type \"$id\" requested. " .
47 "It is not present in \$wgObjectCaches." );
50 return self
::newFromParams( $wgObjectCaches[$id] );
54 * Create a new cache object from parameters
56 * @param $params array
60 static function newFromParams( $params ) {
61 if ( isset( $params['factory'] ) ) {
62 return call_user_func( $params['factory'], $params );
63 } elseif ( isset( $params['class'] ) ) {
64 $class = $params['class'];
65 return new $class( $params );
67 throw new MWException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " .
68 "factory and class parameters." );
73 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
75 * CACHE_ANYTHING means that stuff has to be cached, not caching is not an option.
76 * If a caching method is configured for any of the main caches ($wgMainCacheType,
77 * $wgMessageCacheType, $wgParserCacheType), then CACHE_ANYTHING will effectively
78 * be an alias to the configured cache choice for that.
79 * If no cache choice is configured (by default $wgMainCacheType is CACHE_NONE),
80 * then CACHE_ANYTHING will forward to CACHE_DB.
82 static function newAnything( $params ) {
83 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
84 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
85 foreach ( $candidates as $candidate ) {
86 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
87 return self
::getInstance( $candidate );
90 return self
::getInstance( CACHE_DB
);
94 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
98 static function newAccelerator( $params ) {
99 if ( function_exists( 'apc_fetch') ) {
101 } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
103 } elseif( function_exists( 'wincache_ucache_get' ) ) {
106 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
107 "cache is present. You may want to install APC." );
109 return self
::newFromId( $id );
113 * Factory function that creates a memcached client object.
114 * The idea of this is that it might eventually detect and automatically
115 * support the PECL extension, assuming someone can get it to compile.
117 * @param $params array
119 * @return MemcachedPhpBagOStuff
121 static function newMemcached( $params ) {
122 return new MemcachedPhpBagOStuff( $params );