Follow up r83140. Add a clear() method to ObjectCache so that it can be used by ForkC...
[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 * Clear all the cached instances.
26 */
27 static function clear() {
28 self::$instances = array();
29 }
30
31 /**
32 * Create a new cache object of the specified type.
33 */
34 static function newFromId( $id ) {
35 global $wgObjectCaches;
36
37 if ( !isset( $wgObjectCaches[$id] ) ) {
38 throw new MWException( "Invalid object cache type \"$id\" requested. " .
39 "It is not present in \$wgObjectCaches." );
40 }
41
42 return self::newFromParams( $wgObjectCaches[$id] );
43 }
44
45 /**
46 * Create a new cache object from parameters
47 */
48 static function newFromParams( $params ) {
49 if ( isset( $params['factory'] ) ) {
50 return call_user_func( $params['factory'], $params );
51 } elseif ( isset( $params['class'] ) ) {
52 $class = $params['class'];
53 return new $class( $params );
54 } else {
55 throw new MWException( "The definition of cache type \"$id\" lacks both " .
56 "factory and class parameters." );
57 }
58 }
59
60 /**
61 * Factory function referenced from DefaultSettings.php for CACHE_ANYTHING
62 */
63 static function newAnything( $params ) {
64 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
65 $candidates = array( $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType );
66 foreach ( $candidates as $candidate ) {
67 if ( $candidate !== CACHE_NONE && $candidate !== CACHE_ANYTHING ) {
68 return self::getInstance( $candidate );
69 }
70 }
71 return self::getInstance( CACHE_DB );
72 }
73
74 /**
75 * Factory function referenced from DefaultSettings.php for CACHE_ACCEL.
76 */
77 static function newAccelerator( $params ) {
78 if ( function_exists( 'eaccelerator_get' ) ) {
79 $id = 'eaccelerator';
80 } elseif ( function_exists( 'apc_fetch') ) {
81 $id = 'apc';
82 } elseif( function_exists( 'xcache_get' ) ) {
83 $id = 'xcache';
84 } elseif( function_exists( 'wincache_ucache_get' ) ) {
85 $id = 'wincache';
86 } else {
87 throw new MWException( "CACHE_ACCEL requested but no suitable object " .
88 "cache is present. You may want to install APC." );
89 }
90 return self::newFromId( $id );
91 }
92
93 /**
94 * Factory function that creates a memcached client object.
95 * The idea of this is that it might eventually detect and automatically
96 * support the PECL extension, assuming someone can get it to compile.
97 */
98 static function newMemcached( $params ) {
99 return new MemcachedPhpBagOStuff( $params );
100 }
101 }