3 * Functions to get cache objects
9 static $instances = array();
12 * Get a cached instance of the specified type of cache object.
14 static function getInstance( $id ) {
15 if ( isset( self
::$instances[$id] ) ) {
16 return self
::$instances[$id];
19 $object = self
::newFromId( $id );
20 self
::$instances[$id] = $object;
25 * Clear all the cached instances.
27 static function clear() {
28 self
::$instances = array();
32 * Create a new cache object of the specified type.
34 static function newFromId( $id ) {
35 global $wgObjectCaches;
37 if ( !isset( $wgObjectCaches[$id] ) ) {
38 throw new MWException( "Invalid object cache type \"$id\" requested. " .
39 "It is not present in \$wgObjectCaches." );
42 return self
::newFromParams( $wgObjectCaches[$id] );
46 * Create a new cache object from parameters
48 static function newFromParams( $params ) {
49 if ( isset( $params['factory'] ) ) {
50 return call_user_func( $params['factory'], $params );
51 } elseif ( isset( $params['class'] ) ) {
52 $class = $params['class'];
53 return new $class( $params );
55 throw new MWException( "The definition of cache type \"$id\" lacks both " .
56 "factory and class parameters." );
61 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
63 static function newAnything( $params ) {
64 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
65 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
66 foreach ( $candidates as $candidate ) {
67 if ( $candidate !== CACHE_NONE
&& $candidate !== CACHE_ANYTHING
) {
68 return self
::getInstance( $candidate );
71 return self
::getInstance( CACHE_DB
);
75 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
77 static function newAccelerator( $params ) {
78 if ( function_exists( 'eaccelerator_get' ) ) {
80 } elseif ( function_exists( 'apc_fetch') ) {
82 } elseif( function_exists( 'xcache_get' ) ) {
84 } elseif( function_exists( 'wincache_ucache_get' ) ) {
87 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
88 "cache is present. You may want to install APC." );
90 return self
::newFromId( $id );
94 * Factory function that creates a memcached client object.
95 * The idea of this is that it might eventually detect and automatically
96 * support the PECL extension, assuming someone can get it to compile.
98 static function newMemcached( $params ) {
99 return new MemcachedPhpBagOStuff( $params );