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