Rename $wgVaryOnXFPForAPI (introduced in r93818) to $wgVaryOnXFP and extend it to...
[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 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 );
81 }
82 }
83 return self::getInstance( CACHE_DB );
84 }
85
86 /**
87 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
88 *
89 * @return ObjectCache
90 */
91 static function newAccelerator( $params ) {
92 if ( function_exists( 'eaccelerator_get' ) ) {
93 $id = 'eaccelerator';
94 } elseif ( function_exists( 'apc_fetch') ) {
95 $id = 'apc';
96 } elseif( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
97 $id = 'xcache';
98 } elseif( function_exists( 'wincache_ucache_get' ) ) {
99 $id = 'wincache';
100 } else {
101 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
102 "cache is present. You may want to install APC." );
103 }
104 return self::newFromId( $id );
105 }
106
107 /**
108 * Factory function that creates a memcached client object.
109 * The idea of this is that it might eventually detect and automatically
110 * support the PECL extension, assuming someone can get it to compile.
111 *
112 * @param $params array
113 *
114 * @return MemcachedPhpBagOStuff
115 */
116 static function newMemcached( $params ) {
117 return new MemcachedPhpBagOStuff( $params );
118 }
119 }