InProcessCachePlugin.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Extra level of caching, in memory
  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. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Extra level of caching
  37. *
  38. * This plugin adds an extra level of in-process caching to any regular
  39. * cache system like APC, XCache, or Memcache.
  40. *
  41. * Note that since most caching plugins return false for StartCache*
  42. * methods, you should add this plugin before them, i.e.
  43. *
  44. * addPlugin('InProcessCache');
  45. * addPlugin('XCache');
  46. *
  47. * @category Cache
  48. * @package StatusNet
  49. * @author Evan Prodromou <evan@status.net>
  50. * @copyright 2010 StatusNet, Inc.
  51. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  52. * @link http://status.net/
  53. */
  54. class InProcessCachePlugin extends Plugin
  55. {
  56. private $_items = array();
  57. private $_hits = array();
  58. private $active;
  59. /**
  60. * Constructor checks if it's safe to use the in-process cache.
  61. * On CLI scripts, we'll disable ourselves to avoid data corruption
  62. * due to keeping stale data around.
  63. *
  64. * On web requests we'll roll the dice; they're short-lived so have
  65. * less chance of stale data. Race conditions are still possible,
  66. * so beware!
  67. */
  68. function __construct()
  69. {
  70. parent::__construct();
  71. $this->active = (PHP_SAPI != 'cli');
  72. }
  73. /**
  74. * Get an item from the cache
  75. *
  76. * Called before other cache systems are called (iif this
  77. * plugin was loaded correctly, see class comment). If we
  78. * have the data, return it, and don't hit the other cache
  79. * systems.
  80. *
  81. * @param string &$key Key to fetch
  82. * @param mixed &$value Resulting value or false for miss
  83. *
  84. * @return boolean false if found, else true
  85. */
  86. function onStartCacheGet(&$key, &$value)
  87. {
  88. if ($this->active && array_key_exists($key, $this->_items)) {
  89. $value = $this->_items[$key];
  90. if (array_key_exists($key, $this->_hits)) {
  91. $this->_hits[$key]++;
  92. } else {
  93. $this->_hits[$key] = 1;
  94. }
  95. Event::handle('EndCacheGet', array($key, &$value));
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Called at the end of a cache get
  102. *
  103. * If we don't already have the data, we cache it. This
  104. * keeps us from having to call the external cache if the
  105. * key is requested again.
  106. *
  107. * @param string $key Key to fetch
  108. * @param mixed &$value Resulting value or false for miss
  109. *
  110. * @return boolean hook value, true
  111. */
  112. function onEndCacheGet($key, &$value)
  113. {
  114. if ($this->active && (!array_key_exists($key, $this->_items) ||
  115. $this->_items[$key] != $value)) {
  116. $this->_items[$key] = $value;
  117. }
  118. return true;
  119. }
  120. /**
  121. * Called at the end of setting a cache element
  122. *
  123. * Always set the cache element; may overwrite existing
  124. * data.
  125. *
  126. * @param string $key Key to fetch
  127. * @param mixed $value Resulting value or false for miss
  128. * @param integer $flag ignored
  129. * @param integer $expiry ignored
  130. *
  131. * @return boolean true
  132. */
  133. function onEndCacheSet($key, $value, $flag, $expiry)
  134. {
  135. if ($this->active) {
  136. $this->_items[$key] = $value;
  137. }
  138. return true;
  139. }
  140. /**
  141. * Called at the end of deleting a cache element
  142. *
  143. * If stuff's deleted from the other cache, we
  144. * delete it too.
  145. *
  146. * @param string &$key Key to delete
  147. * @param boolean &$success Success flag; ignored
  148. *
  149. * @return boolean true
  150. */
  151. function onStartCacheDelete(&$key, &$success)
  152. {
  153. if ($this->active && array_key_exists($key, $this->_items)) {
  154. unset($this->_items[$key]);
  155. }
  156. return true;
  157. }
  158. /**
  159. * Version info
  160. *
  161. * @param array &$versions Array of version blocks
  162. *
  163. * @return boolean true
  164. */
  165. function onPluginVersion(array &$versions)
  166. {
  167. $url = 'http://status.net/wiki/Plugin:InProcessCache';
  168. $versions[] = array('name' => 'InProcessCache',
  169. 'version' => GNUSOCIAL_VERSION,
  170. 'author' => 'Evan Prodromou',
  171. 'homepage' => $url,
  172. 'description' =>
  173. // TRANS: Plugin dscription.
  174. _m('Additional in-process cache for plugins.'));
  175. return true;
  176. }
  177. /**
  178. * Cleanup function; called at end of process
  179. *
  180. * If the inprocess/stats config value is true, we dump
  181. * stats to the log file
  182. *
  183. * @return boolean true
  184. */
  185. function cleanup()
  186. {
  187. if ($this->active && common_config('inprocess', 'stats')) {
  188. $this->log(LOG_INFO, "cache size: " .
  189. count($this->_items));
  190. $sum = 0;
  191. foreach ($this->_hits as $hitcount) {
  192. $sum += $hitcount;
  193. }
  194. $this->log(LOG_INFO, $sum . " hits on " .
  195. count($this->_hits) . " keys");
  196. }
  197. return true;
  198. }
  199. }