Simplify $assoc check
[mediawiki.git] / includes / objectcache / ObjectCache.php
blob77ca837142935934772370b5ea994395dccdad29
1 <?php
2 /**
3 * Functions to get cache objects
5 * @file
6 * @ingroup Cache
7 */
8 class ObjectCache {
9 static $instances = array();
11 /**
12 * Get a cached instance of the specified type of cache object.
14 * @param $id
16 * @return 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;
25 return $object;
28 /**
29 * Clear all the cached instances.
31 static function clear() {
32 self::$instances = array();
35 /**
36 * Create a new cache object of the specified type.
38 * @param $id
40 * @return ObjectCache
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] );
53 /**
54 * Create a new cache object from parameters
56 * @param $params array
58 * @return ObjectCache
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 );
66 } else {
67 throw new MWException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " .
68 "factory and class parameters." );
72 /**
73 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
75 static function newAnything( $params ) {
76 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
77 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
78 foreach ( $candidates as $candidate ) {
79 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
80 return self::getInstance( $candidate );
83 return self::getInstance( CACHE_DB );
86 /**
87 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
89 * @return ObjectCache
91 static function newAccelerator( $params ) {
92 if ( function_exists( 'apc_fetch') ) {
93 $id = 'apc';
94 } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
95 $id = 'xcache';
96 } elseif( function_exists( 'wincache_ucache_get' ) ) {
97 $id = 'wincache';
98 } else {
99 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
100 "cache is present. You may want to install APC." );
102 return self::newFromId( $id );
106 * Factory function that creates a memcached client object.
107 * The idea of this is that it might eventually detect and automatically
108 * support the PECL extension, assuming someone can get it to compile.
110 * @param $params array
112 * @return MemcachedPhpBagOStuff
114 static function newMemcached( $params ) {
115 return new MemcachedPhpBagOStuff( $params );