Merge "Fatal error: Call to a member function getExtension() on a non-object in...
[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 * @param $id
15 *
16 * @return object
17 */
18 static function getInstance( $id ) {
19 if ( isset( self::$instances[$id] ) ) {
20 return self::$instances[$id];
21 }
22
23 $object = self::newFromId( $id );
24 self::$instances[$id] = $object;
25 return $object;
26 }
27
28 /**
29 * Clear all the cached instances.
30 */
31 static function clear() {
32 self::$instances = array();
33 }
34
35 /**
36 * Create a new cache object of the specified type.
37 *
38 * @param $id
39 *
40 * @return ObjectCache
41 */
42 static function newFromId( $id ) {
43 global $wgObjectCaches;
44
45 if ( !isset( $wgObjectCaches[$id] ) ) {
46 throw new MWException( "Invalid object cache type \"$id\" requested. " .
47 "It is not present in \$wgObjectCaches." );
48 }
49
50 return self::newFromParams( $wgObjectCaches[$id] );
51 }
52
53 /**
54 * Create a new cache object from parameters
55 *
56 * @param $params array
57 *
58 * @return ObjectCache
59 */
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." );
69 }
70 }
71
72 /**
73 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
74 *
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.
81 */
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 );
88 }
89 }
90 return self::getInstance( CACHE_DB );
91 }
92
93 /**
94 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
95 *
96 * @return ObjectCache
97 */
98 static function newAccelerator( $params ) {
99 if ( function_exists( 'apc_fetch') ) {
100 $id = 'apc';
101 } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
102 $id = 'xcache';
103 } elseif( function_exists( 'wincache_ucache_get' ) ) {
104 $id = 'wincache';
105 } else {
106 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
107 "cache is present. You may want to install APC." );
108 }
109 return self::newFromId( $id );
110 }
111
112 /**
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.
116 *
117 * @param $params array
118 *
119 * @return MemcachedPhpBagOStuff
120 */
121 static function newMemcached( $params ) {
122 return new MemcachedPhpBagOStuff( $params );
123 }
124 }