One minor followup to r82810
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 /**
3 * Functions to get cache objects
4 *
5 * @file
6 * @ingroup Cache
7 */
8
9 /**
10 * FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
11 * It acts as a memcached server with no RAM, that is, all objects are
12 * cleared the moment they are set. All set operations succeed and all
13 * get operations return null.
14 * @ingroup Cache
15 */
16 class FakeMemCachedClient {
17 function add ($key, $val, $exp = 0) { return true; }
18 function decr ($key, $amt=1) { return null; }
19 function delete ($key, $time = 0) { return false; }
20 function disconnect_all () { }
21 function enable_compress ($enable) { }
22 function forget_dead_hosts () { }
23 function get ($key) { return null; }
24 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
25 function incr ($key, $amt=1) { return null; }
26 function replace ($key, $value, $exp=0) { return false; }
27 function run_command ($sock, $cmd) { return null; }
28 function set ($key, $value, $exp=0){ return true; }
29 function set_compress_threshold ($thresh){ }
30 function set_debug ($dbg) { }
31 function set_servers ($list) { }
32 }
33
34 global $wgCaches;
35 $wgCaches = array();
36
37 /**
38 * Get a cache object.
39 * @param $inputType Integer: cache type, one the the CACHE_* constants.
40 *
41 * @return BagOStuff
42 */
43 function &wfGetCache( $inputType ) {
44 global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
45 $cache = false;
46
47 if ( $inputType == CACHE_ANYTHING ) {
48 reset( $wgCaches );
49 $type = key( $wgCaches );
50 if ( $type === false || $type === CACHE_NONE ) {
51 $type = CACHE_DB;
52 }
53 } else {
54 $type = $inputType;
55 }
56
57 if ( $type == CACHE_MEMCACHED ) {
58 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ) {
59 $wgCaches[CACHE_MEMCACHED] = new MemCachedClientforWiki(
60 array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
61 $wgCaches[CACHE_MEMCACHED]->set_servers( $wgMemCachedServers );
62 $wgCaches[CACHE_MEMCACHED]->set_debug( $wgMemCachedDebug );
63 }
64 $cache =& $wgCaches[CACHE_MEMCACHED];
65 } elseif ( $type == CACHE_ACCEL ) {
66 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
67 if ( function_exists( 'eaccelerator_get' ) ) {
68 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
69 } elseif ( function_exists( 'apc_fetch') ) {
70 $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
71 } elseif( function_exists( 'xcache_get' ) ) {
72 $wgCaches[CACHE_ACCEL] = new XCacheBagOStuff();
73 } elseif( function_exists( 'wincache_ucache_get' ) ) {
74 $wgCaches[CACHE_ACCEL] = new WinCacheBagOStuff();
75 } else {
76 $wgCaches[CACHE_ACCEL] = false;
77 }
78 }
79 if ( $wgCaches[CACHE_ACCEL] !== false ) {
80 $cache =& $wgCaches[CACHE_ACCEL];
81 }
82 } elseif ( $type == CACHE_DBA ) {
83 if ( !array_key_exists( CACHE_DBA, $wgCaches ) ) {
84 $wgCaches[CACHE_DBA] = new DBABagOStuff;
85 }
86 $cache =& $wgCaches[CACHE_DBA];
87 }
88
89 if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
90 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
91 $wgCaches[CACHE_DB] = new SqlBagOStuff('objectcache');
92 }
93 $cache =& $wgCaches[CACHE_DB];
94 }
95
96 if ( $cache === false ) {
97 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
98 $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
99 }
100 $cache =& $wgCaches[CACHE_NONE];
101 }
102
103 return $cache;
104 }
105
106 /** Get the main cache object */
107 function &wfGetMainCache() {
108 global $wgMainCacheType;
109 $ret =& wfGetCache( $wgMainCacheType );
110 return $ret;
111 }
112
113 /** Get the cache object used by the message cache */
114 function &wfGetMessageCacheStorage() {
115 global $wgMessageCacheType;
116 $ret =& wfGetCache( $wgMessageCacheType );
117 return $ret;
118 }
119
120 /** Get the cache object used by the parser cache */
121 function &wfGetParserCacheStorage() {
122 global $wgParserCacheType;
123 $ret =& wfGetCache( $wgParserCacheType );
124 return $ret;
125 }