don't try to redeclare classes
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Cache
5 */
6
7 if (!defined('MEDIAWIKI')) die( "Not a valid entry point\n");
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 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 function &wfGetCache( $inputType ) {
36 global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug;
37 $cache = false;
38
39 if ( $inputType == CACHE_ANYTHING ) {
40 reset( $wgCaches );
41 $type = key( $wgCaches );
42 if ( $type === false || $type === CACHE_NONE ) {
43 $type = CACHE_DB;
44 }
45 } else {
46 $type = $inputType;
47 }
48
49 if ( $type == CACHE_MEMCACHED ) {
50 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ){
51 require_once( 'memcached-client.php' );
52
53 if (!class_exists("MemcachedClientforWiki")) {
54 class MemCachedClientforWiki extends memcached {
55 function _debugprint( $text ) {
56 wfDebug( "memcached: $text\n" );
57 }
58 }
59 }
60
61 $wgCaches[CACHE_DB] = new MemCachedClientforWiki(
62 array('persistant' => true, 'compress_threshold' => 1500 ) );
63 $cache =& $wgCaches[CACHE_DB];
64 $cache->set_servers( $wgMemCachedServers );
65 $cache->set_debug( $wgMemCachedDebug );
66 }
67 } elseif ( $type == CACHE_ACCEL ) {
68 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
69 if ( function_exists( 'eaccelerator_get' ) ) {
70 require_once( 'BagOStuff.php' );
71 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
72 } elseif ( function_exists( 'mmcache_get' ) ) {
73 require_once( 'BagOStuff.php' );
74 $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
75 } else {
76 $wgCaches[CACHE_ACCEL] = false;
77 }
78 }
79 if ( $wgCaches[CACHE_ACCEL] !== false ) {
80 $cache =& $wgCaches[CACHE_ACCEL];
81 }
82 }
83
84 if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
85 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
86 require_once( 'BagOStuff.php' );
87 $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
88 }
89 $cache =& $wgCaches[CACHE_DB];
90 }
91
92 if ( $cache === false ) {
93 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
94 $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
95 }
96 $cache =& $wgCaches[CACHE_NONE];
97 }
98
99 return $cache;
100 }
101
102 function &wfGetMainCache() {
103 global $wgMainCacheType;
104 return wfGetCache( $wgMainCacheType );
105 }
106
107 function &wfGetMessageCacheStorage() {
108 global $wgMessageCacheType;
109 return wfGetCache( $wgMessageCacheType );
110 }
111
112 function wfGetParserCacheStorage() {
113 global $wgParserCacheType;
114 return wfGetCache( $wgParserCacheType );
115 }
116
117 ?>