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