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