* When CACHE_ANYTHING is requested, return the cached instance, don't construct a...
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
1 <?php
2 /**
3 * Functions to get cache objects
4 *
5 * @file
6 * @ingroup Cache
7 */
8 class ObjectCache {
9 static $instances = array();
10
11 /**
12 * Get a cached instance of the specified type of cache object.
13 */
14 static function getInstance( $id ) {
15 if ( isset( self::$instances[$id] ) ) {
16 return self::$instances[$id];
17 }
18
19 $object = self::newFromId( $id );
20 self::$instances[$id] = $object;
21 return $object;
22 }
23
24 /**
25 * Create a new cache object of the specified type.
26 */
27 static function newFromId( $id ) {
28 global $wgObjectCaches;
29
30 if ( !isset( $wgObjectCaches[$id] ) ) {
31 throw new MWException( "Invalid object cache type \"$id\" requested. " .
32 "It is not present in \$wgObjectCaches." );
33 }
34
35 return self::newFromParams( $wgObjectCaches[$id] );
36 }
37
38 /**
39 * Create a new cache object from parameters
40 */
41 static function newFromParams( $params ) {
42 if ( isset( $params['factory'] ) ) {
43 return call_user_func( $params['factory'], $params );
44 } elseif ( isset( $params['class'] ) ) {
45 $class = $params['class'];
46 return new $class( $params );
47 } else {
48 throw new MWException( "The definition of cache type \"$id\" lacks both " .
49 "factory and class parameters." );
50 }
51 }
52
53 /**
54 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
55 */
56 static function newAnything( $params ) {
57 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
58 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
59 foreach ( $candidates as $candidate ) {
60 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
61 return self::getInstance( $candidate );
62 }
63 }
64 return self::getInstance( CACHE_DB );
65 }
66
67 /**
68 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
69 */
70 static function newAccelerator( $params ) {
71 if ( function_exists( 'eaccelerator_get' ) ) {
72 $id = 'eaccelerator';
73 } elseif ( function_exists( 'apc_fetch') ) {
74 $id = 'apc';
75 } elseif( function_exists( 'xcache_get' ) ) {
76 $id = 'xcache';
77 } elseif( function_exists( 'wincache_ucache_get' ) ) {
78 $id = 'wincache';
79 } else {
80 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
81 "cache is present. You may want to install APC." );
82 }
83 return self::newFromId( $id );
84 }
85
86 /**
87 * Factory function that creates a memcached client object.
88 * The idea of this is that it might eventually detect and automatically
89 * support the PECL extension, assuming someone can get it to compile.
90 */
91 static function newMemcached( $params ) {
92 return new MemcachedPhpBagOStuff( $params );
93 }
94 }