MemcachedPlugin.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009, StatusNet, Inc.
  5. *
  6. * Plugin to implement cache interface for memcached
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Craig Andrews <candrews@integralblue.com>
  27. * @copyright 2009 StatusNet, Inc.
  28. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. // This check helps protect against security problems;
  34. // your code file can't be executed directly from the web.
  35. exit(1);
  36. }
  37. /**
  38. * A plugin to use memcached for the cache interface
  39. *
  40. * This used to be encoded as config-variable options in the core code;
  41. * it's now broken out to a separate plugin. The same interface can be
  42. * implemented by other plugins.
  43. *
  44. * @category Cache
  45. * @package StatusNet
  46. * @author Evan Prodromou <evan@status.net>
  47. * @author Craig Andrews <candrews@integralblue.com>
  48. * @copyright 2009 StatusNet, Inc.
  49. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  50. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  51. * @link http://status.net/
  52. */
  53. class MemcachedPlugin extends Plugin
  54. {
  55. static $cacheInitialized = false;
  56. private $_conn = null;
  57. public $servers = array('127.0.0.1;11211');
  58. public $defaultExpiry = 86400; // 24h
  59. /**
  60. * Initialize the plugin
  61. *
  62. * Note that onStartCacheGet() may have been called before this!
  63. *
  64. * @return boolean flag value
  65. */
  66. function onInitializePlugin()
  67. {
  68. $this->_ensureConn();
  69. self::$cacheInitialized = true;
  70. return true;
  71. }
  72. /**
  73. * Get a value associated with a key
  74. *
  75. * The value should have been set previously.
  76. *
  77. * @param string &$key in; Lookup key
  78. * @param mixed &$value out; value associated with key
  79. *
  80. * @return boolean hook success
  81. */
  82. function onStartCacheGet(&$key, &$value)
  83. {
  84. $this->_ensureConn();
  85. $value = $this->_conn->get($key);
  86. Event::handle('EndCacheGet', array($key, &$value));
  87. return false;
  88. }
  89. /**
  90. * Associate a value with a key
  91. *
  92. * @param string &$key in; Key to use for lookups
  93. * @param mixed &$value in; Value to associate
  94. * @param integer &$flag in; Flag empty or Cache::COMPRESSED
  95. * @param integer &$expiry in; Expiry (passed through to Memcache)
  96. * @param boolean &$success out; Whether the set was successful
  97. *
  98. * @return boolean hook success
  99. */
  100. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  101. {
  102. $this->_ensureConn();
  103. if ($expiry === null) {
  104. $expiry = $this->defaultExpiry;
  105. }
  106. $success = $this->_conn->set($key, $value, $expiry);
  107. Event::handle('EndCacheSet', array($key, $value, $flag,
  108. $expiry));
  109. return false;
  110. }
  111. /**
  112. * Atomically increment an existing numeric key value.
  113. * Existing expiration time will not be changed.
  114. *
  115. * @param string &$key in; Key to use for lookups
  116. * @param int &$step in; Amount to increment (default 1)
  117. * @param mixed &$value out; Incremented value, or false if key not set.
  118. *
  119. * @return boolean hook success
  120. */
  121. function onStartCacheIncrement(&$key, &$step, &$value)
  122. {
  123. $this->_ensureConn();
  124. $value = $this->_conn->increment($key, $step);
  125. Event::handle('EndCacheIncrement', array($key, $step, $value));
  126. return false;
  127. }
  128. /**
  129. * Delete a value associated with a key
  130. *
  131. * @param string &$key in; Key to lookup
  132. * @param boolean &$success out; whether it worked
  133. *
  134. * @return boolean hook success
  135. */
  136. function onStartCacheDelete(&$key, &$success)
  137. {
  138. $this->_ensureConn();
  139. $success = $this->_conn->delete($key);
  140. Event::handle('EndCacheDelete', array($key));
  141. return false;
  142. }
  143. function onStartCacheReconnect(&$success)
  144. {
  145. // nothing to do
  146. return true;
  147. }
  148. /**
  149. * Ensure that a connection exists
  150. *
  151. * Checks the instance $_conn variable and connects
  152. * if it is empty.
  153. *
  154. * @return void
  155. */
  156. private function _ensureConn()
  157. {
  158. if (empty($this->_conn)) {
  159. $this->_conn = new Memcached(common_config('site', 'nickname'));
  160. if (!count($this->_conn->getServerList())) {
  161. if (is_array($this->servers)) {
  162. $servers = $this->servers;
  163. } else {
  164. $servers = array($this->servers);
  165. }
  166. foreach ($servers as $server) {
  167. if (strpos($server, ';') !== false) {
  168. list($host, $port) = explode(';', $server);
  169. } else {
  170. $host = $server;
  171. $port = 11211;
  172. }
  173. $this->_conn->addServer($host, $port);
  174. }
  175. // Compress items stored in the cache.
  176. // Allows the cache to store objects larger than 1MB (if they
  177. // compress to less than 1MB), and improves cache memory efficiency.
  178. $this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
  179. }
  180. }
  181. }
  182. /**
  183. * Translate general flags to Memcached-specific flags
  184. * @param int $flag
  185. * @return int
  186. */
  187. protected function flag($flag)
  188. {
  189. //no flags are presently supported
  190. return $flag;
  191. }
  192. function onPluginVersion(&$versions)
  193. {
  194. $versions[] = array('name' => 'Memcached',
  195. 'version' => GNUSOCIAL_VERSION,
  196. 'author' => 'Evan Prodromou, Craig Andrews',
  197. 'homepage' => 'http://status.net/wiki/Plugin:Memcached',
  198. 'rawdescription' =>
  199. // TRANS: Plugin description.
  200. _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
  201. return true;
  202. }
  203. }