InProcessCachePlugin.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. const PLUGIN_VERSION = '2.0.0';
  57. private $_items = array();
  58. private $_hits = array();
  59. private $active;
  60. /**
  61. * Constructor checks if it's safe to use the in-process cache.
  62. * On CLI scripts, we'll disable ourselves to avoid data corruption
  63. * due to keeping stale data around.
  64. *
  65. * On web requests we'll roll the dice; they're short-lived so have
  66. * less chance of stale data. Race conditions are still possible,
  67. * so beware!
  68. */
  69. function __construct()
  70. {
  71. parent::__construct();
  72. $this->active = (PHP_SAPI != 'cli');
  73. }
  74. /**
  75. * Get an item from the cache
  76. *
  77. * Called before other cache systems are called (iif this
  78. * plugin was loaded correctly, see class comment). If we
  79. * have the data, return it, and don't hit the other cache
  80. * systems.
  81. *
  82. * @param string &$key Key to fetch
  83. * @param mixed &$value Resulting value or false for miss
  84. *
  85. * @return boolean false if found, else true
  86. */
  87. function onStartCacheGet(&$key, &$value)
  88. {
  89. if ($this->active && array_key_exists($key, $this->_items)) {
  90. $value = $this->_items[$key];
  91. if (array_key_exists($key, $this->_hits)) {
  92. $this->_hits[$key]++;
  93. } else {
  94. $this->_hits[$key] = 1;
  95. }
  96. Event::handle('EndCacheGet', array($key, &$value));
  97. return false;
  98. }
  99. return true;
  100. }
  101. /**
  102. * Called at the end of a cache get
  103. *
  104. * If we don't already have the data, we cache it. This
  105. * keeps us from having to call the external cache if the
  106. * key is requested again.
  107. *
  108. * @param string $key Key to fetch
  109. * @param mixed &$value Resulting value or false for miss
  110. *
  111. * @return boolean hook value, true
  112. */
  113. function onEndCacheGet($key, &$value)
  114. {
  115. if ($this->active && (!array_key_exists($key, $this->_items) ||
  116. $this->_items[$key] != $value)) {
  117. $this->_items[$key] = $value;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Called at the end of setting a cache element
  123. *
  124. * Always set the cache element; may overwrite existing
  125. * data.
  126. *
  127. * @param string $key Key to fetch
  128. * @param mixed $value Resulting value or false for miss
  129. * @param integer $flag ignored
  130. * @param integer $expiry ignored
  131. *
  132. * @return boolean true
  133. */
  134. function onEndCacheSet($key, $value, $flag, $expiry)
  135. {
  136. if ($this->active) {
  137. $this->_items[$key] = $value;
  138. }
  139. return true;
  140. }
  141. /**
  142. * Called at the end of deleting a cache element
  143. *
  144. * If stuff's deleted from the other cache, we
  145. * delete it too.
  146. *
  147. * @param string &$key Key to delete
  148. * @param boolean &$success Success flag; ignored
  149. *
  150. * @return boolean true
  151. */
  152. function onStartCacheDelete(&$key, &$success)
  153. {
  154. if ($this->active && array_key_exists($key, $this->_items)) {
  155. unset($this->_items[$key]);
  156. }
  157. return true;
  158. }
  159. /**
  160. * Version info
  161. *
  162. * @param array &$versions Array of version blocks
  163. *
  164. * @return boolean true
  165. */
  166. function onPluginVersion(array &$versions)
  167. {
  168. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/InProcessCache';
  169. $versions[] = array('name' => 'InProcessCache',
  170. 'version' => self::PLUGIN_VERSION,
  171. 'author' => 'Evan Prodromou',
  172. 'homepage' => $url,
  173. 'description' =>
  174. // TRANS: Plugin dscription.
  175. _m('Additional in-process cache for plugins.'));
  176. return true;
  177. }
  178. /**
  179. * Cleanup function; called at end of process
  180. *
  181. * If the inprocess/stats config value is true, we dump
  182. * stats to the log file
  183. *
  184. * @return boolean true
  185. */
  186. function cleanup()
  187. {
  188. if ($this->active && common_config('inprocess', 'stats')) {
  189. $this->log(LOG_INFO, "cache size: " .
  190. count($this->_items));
  191. $sum = 0;
  192. foreach ($this->_hits as $hitcount) {
  193. $sum += $hitcount;
  194. }
  195. $this->log(LOG_INFO, $sum . " hits on " .
  196. count($this->_hits) . " keys");
  197. }
  198. return true;
  199. }
  200. }