MediaWiki  REL1_24
ObjectCache.php
Go to the documentation of this file.
00001 <?php
00029 class ObjectCache {
00030     public static $instances = array();
00031 
00039     static function getInstance( $id ) {
00040         if ( isset( self::$instances[$id] ) ) {
00041             return self::$instances[$id];
00042         }
00043 
00044         $object = self::newFromId( $id );
00045         self::$instances[$id] = $object;
00046         return $object;
00047     }
00048 
00052     static function clear() {
00053         self::$instances = array();
00054     }
00055 
00064     static function newFromId( $id ) {
00065         global $wgObjectCaches;
00066 
00067         if ( !isset( $wgObjectCaches[$id] ) ) {
00068             throw new MWException( "Invalid object cache type \"$id\" requested. " .
00069                 "It is not present in \$wgObjectCaches." );
00070         }
00071 
00072         return self::newFromParams( $wgObjectCaches[$id] );
00073     }
00074 
00083     static function newFromParams( $params ) {
00084         if ( isset( $params['factory'] ) ) {
00085             return call_user_func( $params['factory'], $params );
00086         } elseif ( isset( $params['class'] ) ) {
00087             $class = $params['class'];
00088             return new $class( $params );
00089         } else {
00090             throw new MWException( "The definition of cache type \""
00091                 . print_r( $params, true ) . "\" lacks both "
00092                 . "factory and class parameters." );
00093         }
00094     }
00095 
00108     static function newAnything( $params ) {
00109         global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
00110         $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
00111         foreach ( $candidates as $candidate ) {
00112             if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
00113                 return self::getInstance( $candidate );
00114             }
00115         }
00116         return self::getInstance( CACHE_DB );
00117     }
00118 
00130     static function newAccelerator( $params, $fallback = null ) {
00131         if ( function_exists( 'apc_fetch' ) ) {
00132             $id = 'apc';
00133         } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
00134             $id = 'xcache';
00135         } elseif ( function_exists( 'wincache_ucache_get' ) ) {
00136             $id = 'wincache';
00137         } else {
00138             if ( $fallback ) {
00139                 return self::newFromId( $fallback );
00140             }
00141             throw new MWException( "CACHE_ACCEL requested but no suitable object " .
00142                 "cache is present. You may want to install APC." );
00143         }
00144         return self::newFromId( $id );
00145     }
00146 
00158     static function newMemcached( $params ) {
00159         return new MemcachedPhpBagOStuff( $params );
00160     }
00161 }