ObjectCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Cache
  5. */
  6. /**
  7. * FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
  8. * It acts as a memcached server with no RAM, that is, all objects are
  9. * cleared the moment they are set. All set operations succeed and all
  10. * get operations return null.
  11. * @ingroup Cache
  12. */
  13. class FakeMemCachedClient {
  14. function add ($key, $val, $exp = 0) { return true; }
  15. function decr ($key, $amt=1) { return null; }
  16. function delete ($key, $time = 0) { return false; }
  17. function disconnect_all () { }
  18. function enable_compress ($enable) { }
  19. function forget_dead_hosts () { }
  20. function get ($key) { return null; }
  21. function get_multi ($keys) { return array_pad(array(), count($keys), null); }
  22. function incr ($key, $amt=1) { return null; }
  23. function replace ($key, $value, $exp=0) { return false; }
  24. function run_command ($sock, $cmd) { return null; }
  25. function set ($key, $value, $exp=0){ return true; }
  26. function set_compress_threshold ($thresh){ }
  27. function set_debug ($dbg) { }
  28. function set_servers ($list) { }
  29. }
  30. global $wgCaches;
  31. $wgCaches = array();
  32. /**
  33. * Get a cache object.
  34. * @param int $inputType cache type, one the the CACHE_* constants.
  35. */
  36. function &wfGetCache( $inputType ) {
  37. global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug, $wgMemCachedPersistent;
  38. $cache = false;
  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. if ( $type == CACHE_MEMCACHED ) {
  49. if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ) {
  50. if ( !class_exists( 'MemcachedClientforWiki' ) ) {
  51. class MemCachedClientforWiki extends memcached {
  52. function _debugprint( $text ) {
  53. wfDebug( "memcached: $text" );
  54. }
  55. }
  56. }
  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( 'mmcache_get' ) ) {
  72. $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
  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. if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
  87. if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
  88. $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
  89. }
  90. $cache =& $wgCaches[CACHE_DB];
  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. return $cache;
  99. }
  100. /** Get the main cache object */
  101. function &wfGetMainCache() {
  102. global $wgMainCacheType;
  103. $ret =& wfGetCache( $wgMainCacheType );
  104. return $ret;
  105. }
  106. /** Get the cache object used by the message cache */
  107. function &wfGetMessageCacheStorage() {
  108. global $wgMessageCacheType;
  109. $ret =& wfGetCache( $wgMessageCacheType );
  110. return $ret;
  111. }
  112. /** Get the cache object used by the parser cache */
  113. function &wfGetParserCacheStorage() {
  114. global $wgParserCacheType;
  115. $ret =& wfGetCache( $wgParserCacheType );
  116. return $ret;
  117. }