/co.llide/forum/inc/cachehandlers/memcache.php

https://github.com/GradientStudios/Gradient-Studios-Website · PHP · 152 lines · 81 code · 22 blank · 49 comment · 7 complexity · 516c46428e907fddc452100c90057d77 MD5 · raw file

  1. <?php
  2. /**
  3. * MyBB 1.6
  4. * Copyright 2010 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://mybb.com
  7. * License: http://mybb.com/about/license
  8. *
  9. * $Id$
  10. */
  11. /**
  12. * Memcache Cache Handler
  13. */
  14. class memcacheCacheHandler
  15. {
  16. /**
  17. * The memcache server resource
  18. */
  19. public $memcache;
  20. /**
  21. * Unique identifier representing this copy of MyBB
  22. */
  23. public $unique_id;
  24. function memcacheCacheHandler($silent=false)
  25. {
  26. global $mybb;
  27. if(!function_exists("memcache_connect"))
  28. {
  29. // Check if our DB engine is loaded
  30. if(!extension_loaded("Memcache"))
  31. {
  32. // Throw our super awesome cache loading error
  33. $mybb->trigger_generic_error("memcache_load_error");
  34. die;
  35. }
  36. }
  37. }
  38. /**
  39. * Connect and initialize this handler.
  40. *
  41. * @return boolean True if successful, false on failure
  42. */
  43. function connect()
  44. {
  45. global $mybb, $error_handler;
  46. $this->memcache = new Memcache;
  47. if($mybb->config['memcache']['host'])
  48. {
  49. $mybb->config['memcache'][0] = $mybb->config['memcache'];
  50. unset($mybb->config['memcache']['host']);
  51. unset($mybb->config['memcache']['port']);
  52. }
  53. foreach($mybb->config['memcache'] as $memcache)
  54. {
  55. if(!$memcache['host'])
  56. {
  57. $message = "Please configure the memcache settings in inc/config.php before attempting to use this cache handler";
  58. $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  59. die;
  60. }
  61. if(!isset($memcache['port']))
  62. {
  63. $memcache['port'] = "11211";
  64. }
  65. $this->memcache->addServer($memcache['host'], $memcache['port']);
  66. if(!$this->memcache)
  67. {
  68. $message = "Unable to connect to the memcache server on {$memcache['memcache_host']}:{$memcache['memcache_port']}. Are you sure it is running?";
  69. $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  70. die;
  71. }
  72. }
  73. // Set a unique identifier for all queries in case other forums are using the same memcache server
  74. $this->unique_id = md5(MYBB_ROOT);
  75. return true;
  76. }
  77. /**
  78. * Retrieve an item from the cache.
  79. *
  80. * @param string The name of the cache
  81. * @param boolean True if we should do a hard refresh
  82. * @return mixed Cache data if successful, false if failure
  83. */
  84. function fetch($name, $hard_refresh=false)
  85. {
  86. $data = $this->memcache->get($this->unique_id."_".$name);
  87. if($data === false)
  88. {
  89. return false;
  90. }
  91. else
  92. {
  93. return $data;
  94. }
  95. }
  96. /**
  97. * Write an item to the cache.
  98. *
  99. * @param string The name of the cache
  100. * @param mixed The data to write to the cache item
  101. * @return boolean True on success, false on failure
  102. */
  103. function put($name, $contents)
  104. {
  105. return $this->memcache->set($this->unique_id."_".$name, $contents);
  106. }
  107. /**
  108. * Delete a cache
  109. *
  110. * @param string The name of the cache
  111. * @return boolean True on success, false on failure
  112. */
  113. function delete($name)
  114. {
  115. return $this->memcache->delete($this->unique_id."_".$name);
  116. }
  117. /**
  118. * Disconnect from the cache
  119. */
  120. function disconnect()
  121. {
  122. @$this->memcache->close();
  123. }
  124. function size_of($name)
  125. {
  126. global $lang;
  127. return $lang->na;
  128. }
  129. }
  130. ?>