2b26cf4efb58bf4fd0b04de8e66d0bb2b1901ea1
[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 /** @todo document */
37 function &wfGetCache( $inputType ) {
38 global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
39 $cache = false;
40
41 if ( $inputType == CACHE_ANYTHING ) {
42 reset( $wgCaches );
43 $type = key( $wgCaches );
44 if ( $type === false || $type === CACHE_NONE ) {
45 $type = CACHE_DB;
46 }
47 } else {
48 $type = $inputType;
49 }
50
51 if ( $type == CACHE_MEMCACHED ) {
52 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ){
53 require_once( 'memcached-client.php' );
54
55 if (!class_exists("MemcachedClientforWiki")) {
56 class MemCachedClientforWiki extends memcached {
57 function _debugprint( $text ) {
58 wfDebug( "memcached: $text\n" );
59 }
60 }
61 }
62
63 $wgCaches[CACHE_DB] = new MemCachedClientforWiki(
64 array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
65 $cache =& $wgCaches[CACHE_DB];
66 $cache->set_servers( $wgMemCachedServers );
67 $cache->set_debug( $wgMemCachedDebug );
68 }
69 } elseif ( $type == CACHE_ACCEL ) {
70 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
71 if ( function_exists( 'eaccelerator_get' ) ) {
72 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
73 } elseif ( function_exists( 'apc_fetch') ) {
74 $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
75 } elseif ( function_exists( 'mmcache_get' ) ) {
76 $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
77 } else {
78 $wgCaches[CACHE_ACCEL] = false;
79 }
80 }
81 if ( $wgCaches[CACHE_ACCEL] !== false ) {
82 $cache =& $wgCaches[CACHE_ACCEL];
83 }
84 } elseif ( $type == CACHE_DBA ) {
85 if ( !array_key_exists( CACHE_DBA, $wgCaches ) ) {
86 $wgCaches[CACHE_DBA] = new DBABagOStuff;
87 }
88 $cache =& $wgCaches[CACHE_DBA];
89 }
90
91 if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
92 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
93 $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
94 }
95 $cache =& $wgCaches[CACHE_DB];
96 }
97
98 if ( $cache === false ) {
99 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
100 $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
101 }
102 $cache =& $wgCaches[CACHE_NONE];
103 }
104
105 return $cache;
106 }
107
108 function &wfGetMainCache() {
109 global $wgMainCacheType;
110 $ret =& wfGetCache( $wgMainCacheType );
111 return $ret;
112 }
113
114 function &wfGetMessageCacheStorage() {
115 global $wgMessageCacheType;
116 $ret =& wfGetCache( $wgMessageCacheType );
117 return $ret;
118 }
119
120 function &wfGetParserCacheStorage() {
121 global $wgParserCacheType;
122 $ret =& wfGetCache( $wgParserCacheType );
123 return $ret;
124 }
125
126 ?>