]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ObjectCache.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / ObjectCache.php
1 <?php
2 /**
3  * @file
4  * @ingroup 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  * @ingroup Cache
13  */
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 /**
36  * Get a cache object.
37  * @param int $inputType cache type, one the the CACHE_* constants. 
38  */
39 function &wfGetCache( $inputType ) {
40         global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
41         $cache = false;
42
43         if ( $inputType == CACHE_ANYTHING ) {
44                 reset( $wgCaches );
45                 $type = key( $wgCaches );
46                 if ( $type === false || $type === CACHE_NONE ) {
47                         $type = CACHE_DB;
48                 }
49         } else {
50                 $type = $inputType;
51         }
52
53         if ( $type == CACHE_MEMCACHED ) {
54                 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ) {
55                         if ( !class_exists( 'MemcachedClientforWiki' ) ) {
56                                 class MemCachedClientforWiki extends memcached {
57                                         function _debugprint( $text ) {
58                                                 wfDebug( "memcached: $text" );
59                                         }
60                                 }
61                         }
62                         $wgCaches[CACHE_MEMCACHED] = new MemCachedClientforWiki(
63                                 array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
64                         $wgCaches[CACHE_MEMCACHED]->set_servers( $wgMemCachedServers );
65                         $wgCaches[CACHE_MEMCACHED]->set_debug( $wgMemCachedDebug );
66                 }
67                 $cache =& $wgCaches[CACHE_MEMCACHED];
68         } elseif ( $type == CACHE_ACCEL ) {
69                 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
70                         if ( function_exists( 'eaccelerator_get' ) ) {
71                                 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
72                         } elseif ( function_exists( 'apc_fetch') ) {
73                                 $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
74                         } elseif( function_exists( 'xcache_get' ) ) {
75                                 $wgCaches[CACHE_ACCEL] = new XCacheBagOStuff();
76                         } elseif ( function_exists( 'mmcache_get' ) ) {
77                                 $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
78                         } else {
79                                 $wgCaches[CACHE_ACCEL] = false;
80                         }
81                 }
82                 if ( $wgCaches[CACHE_ACCEL] !== false ) {
83                         $cache =& $wgCaches[CACHE_ACCEL];
84                 }
85         } elseif ( $type == CACHE_DBA ) {
86                 if ( !array_key_exists( CACHE_DBA, $wgCaches ) ) {
87                         $wgCaches[CACHE_DBA] = new DBABagOStuff;
88                 }
89                 $cache =& $wgCaches[CACHE_DBA];
90         }
91
92         if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
93                 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
94                         $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
95                 }
96                 $cache =& $wgCaches[CACHE_DB];
97         }
98
99         if ( $cache === false ) {
100                 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
101                         $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
102                 }
103                 $cache =& $wgCaches[CACHE_NONE];
104         }
105
106         return $cache;
107 }
108
109 /** Get the main cache object */
110 function &wfGetMainCache() {
111         global $wgMainCacheType;
112         $ret =& wfGetCache( $wgMainCacheType );
113         return $ret;
114 }
115
116 /** Get the cache object used by the message cache */
117 function &wfGetMessageCacheStorage() {
118         global $wgMessageCacheType;
119         $ret =& wfGetCache( $wgMessageCacheType );
120         return $ret;
121 }
122
123 /** Get the cache object used by the parser cache */
124 function &wfGetParserCacheStorage() {
125         global $wgParserCacheType;
126         $ret =& wfGetCache( $wgParserCacheType );
127         return $ret;
128 }