]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ObjectCache.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / ObjectCache.php
1 <?php
2 /**
3  * Functions to get cache objects
4  *
5  * @file
6  * @ingroup Cache
7  */
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  * @ingroup Cache
15  */
16 class FakeMemCachedClient {
17         function add ($key, $val, $exp = 0) { return true; }
18         function decr ($key, $amt=1) { return null; }
19         function delete ($key, $time = 0) { return false; }
20         function disconnect_all () { }
21         function enable_compress ($enable) { }
22         function forget_dead_hosts () { }
23         function get ($key) { return null; }
24         function get_multi ($keys) { return array_pad(array(), count($keys), null); }
25         function incr ($key, $amt=1) { return null; }
26         function replace ($key, $value, $exp=0) { return false; }
27         function run_command ($sock, $cmd) { return null; }
28         function set ($key, $value, $exp=0){ return true; }
29         function set_compress_threshold ($thresh){ }
30         function set_debug ($dbg) { }
31         function set_servers ($list) { }
32 }
33
34 global $wgCaches;
35 $wgCaches = array();
36
37 /**
38  * Get a cache object.
39  * @param $inputType Integer: cache type, one the the CACHE_* constants. 
40  */
41 function &wfGetCache( $inputType ) {
42         global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
43         $cache = false;
44
45         if ( $inputType == CACHE_ANYTHING ) {
46                 reset( $wgCaches );
47                 $type = key( $wgCaches );
48                 if ( $type === false || $type === CACHE_NONE ) {
49                         $type = CACHE_DB;
50                 }
51         } else {
52                 $type = $inputType;
53         }
54
55         if ( $type == CACHE_MEMCACHED ) {
56                 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ) {
57                         $wgCaches[CACHE_MEMCACHED] = new MemCachedClientforWiki(
58                                 array('persistant' => $wgMemCachedPersistent, 'compress_threshold' => 1500 ) );
59                         $wgCaches[CACHE_MEMCACHED]->set_servers( $wgMemCachedServers );
60                         $wgCaches[CACHE_MEMCACHED]->set_debug( $wgMemCachedDebug );
61                 }
62                 $cache =& $wgCaches[CACHE_MEMCACHED];
63         } elseif ( $type == CACHE_ACCEL ) {
64                 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
65                         if ( function_exists( 'eaccelerator_get' ) ) {
66                                 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
67                         } elseif ( function_exists( 'apc_fetch') ) {
68                                 $wgCaches[CACHE_ACCEL] = new APCBagOStuff;
69                         } elseif( function_exists( 'xcache_get' ) ) {
70                                 $wgCaches[CACHE_ACCEL] = new XCacheBagOStuff();
71                         } elseif( function_exists( 'wincache_ucache_get' ) ) {
72                                 $wgCaches[CACHE_ACCEL] = new WinCacheBagOStuff();
73                         } else {
74                                 $wgCaches[CACHE_ACCEL] = false;
75                         }
76                 }
77                 if ( $wgCaches[CACHE_ACCEL] !== false ) {
78                         $cache =& $wgCaches[CACHE_ACCEL];
79                 }
80         } elseif ( $type == CACHE_DBA ) {
81                 if ( !array_key_exists( CACHE_DBA, $wgCaches ) ) {
82                         $wgCaches[CACHE_DBA] = new DBABagOStuff;
83                 }
84                 $cache =& $wgCaches[CACHE_DBA];
85         }
86
87         if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
88                 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
89                         $wgCaches[CACHE_DB] = new SqlBagOStuff('objectcache');
90                 }
91                 $cache =& $wgCaches[CACHE_DB];
92         }
93
94         if ( $cache === false ) {
95                 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
96                         $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
97                 }
98                 $cache =& $wgCaches[CACHE_NONE];
99         }
100
101         return $cache;
102 }
103
104 /** Get the main cache object */
105 function &wfGetMainCache() {
106         global $wgMainCacheType;
107         $ret =& wfGetCache( $wgMainCacheType );
108         return $ret;
109 }
110
111 /** Get the cache object used by the message cache */
112 function &wfGetMessageCacheStorage() {
113         global $wgMessageCacheType;
114         $ret =& wfGetCache( $wgMessageCacheType );
115         return $ret;
116 }
117
118 /** Get the cache object used by the parser cache */
119 function &wfGetParserCacheStorage() {
120         global $wgParserCacheType;
121         $ret =& wfGetCache( $wgParserCacheType );
122         return $ret;
123 }