d851f3ab2a47d1946b922fab3a5a49399c9907df
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage 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 * @package MediaWiki
13 * @subpackage Cache
14 */
15 class FakeMemCachedClient {
16 function add ($key, $val, $exp = 0) { return true; }
17 function decr ($key, $amt=1) { return null; }
18 function delete ($key, $time = 0) { return false; }
19 function disconnect_all () { }
20 function enable_compress ($enable) { }
21 function forget_dead_hosts () { }
22 function get ($key) { return null; }
23 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
24 function incr ($key, $amt=1) { return null; }
25 function replace ($key, $value, $exp=0) { return false; }
26 function run_command ($sock, $cmd) { return null; }
27 function set ($key, $value, $exp=0){ return true; }
28 function set_compress_threshold ($thresh){ }
29 function set_debug ($dbg) { }
30 function set_servers ($list) { }
31 }
32
33 global $wgCaches;
34 $wgCaches = array();
35
36 class ObjectCacheManager {
37 /* @static */
38 function getCache( $inputType ) {
39 global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
40 $cache = false;
41
42 if ( $inputType == CACHE_ANYTHING ) {
43 reset( $wgCaches );
44 $type = key( $wgCaches );
45 if ( $type === false || $type === CACHE_NONE ) {
46 $type = CACHE_DB;
47 }
48 } else {
49 $type = $inputType;
50 }
51
52 if ( $type == CACHE_MEMCACHED ) {
53 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ){
54 $wgCaches[CACHE_DB] = new MemCachedClientforWiki(
55 array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
56 $cache =& $wgCaches[CACHE_DB];
57 $cache->set_servers( $wgMemCachedServers );
58 $cache->set_debug( $wgMemCachedDebug );
59 }
60 } elseif ( $type == CACHE_ACCEL ) {
61 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
62 if ( function_exists( 'eaccelerator_get' ) ) {
63 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
64 } elseif ( function_exists( 'apc_fetch') ) {
65 $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
66 } elseif ( function_exists( 'mmcache_get' ) ) {
67 $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
68 } else {
69 $wgCaches[CACHE_ACCEL] = false;
70 }
71 }
72 if ( $wgCaches[CACHE_ACCEL] !== false ) {
73 $cache =& $wgCaches[CACHE_ACCEL];
74 }
75 }
76
77 if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
78 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
79 $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
80 }
81 $cache =& $wgCaches[CACHE_DB];
82 }
83
84 if ( $cache === false ) {
85 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
86 $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
87 }
88 $cache =& $wgCaches[CACHE_NONE];
89 }
90
91 return $cache;
92 }
93
94 /** @static */
95 function &getMainCache() {
96 global $wgMainCacheType;
97 $ret =& ObjectCacheManager::getCache( $wgMainCacheType );
98 return $ret;
99 }
100
101 /** @static */
102 function &getMessageCache() {
103 global $wgMessageCacheType;
104 $ret =& ObjectCacheManager::getCache( $wgMessageCacheType );
105 return $ret;
106 }
107
108 /** @static */
109 function &getParserCache() {
110 global $wgParserCacheType;
111 $ret =& ObjectCacheManager::getCache( $wgParserCacheType );
112 return $ret;
113 }
114
115 }
116
117 class MemCachedClientforWiki extends memcached {
118 function _debugprint( $text ) {
119 wfDebug( "memcached: $text\n" );
120 }
121 }
122
123 ?>