123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class InProcessCachePlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
- private $_items = array();
- private $_hits = array();
- private $active;
-
- function __construct()
- {
- parent::__construct();
- $this->active = (PHP_SAPI != 'cli');
- }
-
- function onStartCacheGet(&$key, &$value)
- {
- if ($this->active && array_key_exists($key, $this->_items)) {
- $value = $this->_items[$key];
- if (array_key_exists($key, $this->_hits)) {
- $this->_hits[$key]++;
- } else {
- $this->_hits[$key] = 1;
- }
- return false;
- }
- return true;
- }
-
- function onEndCacheGet($key, &$value)
- {
- if ($this->active && (!array_key_exists($key, $this->_items) ||
- $this->_items[$key] != $value)) {
- $this->_items[$key] = $value;
- }
- return true;
- }
-
- function onEndCacheSet($key, $value, $flag, $expiry)
- {
- if ($this->active) {
- $this->_items[$key] = $value;
- }
- return true;
- }
-
- function onStartCacheDelete(&$key, &$success)
- {
- if ($this->active && array_key_exists($key, $this->_items)) {
- unset($this->_items[$key]);
- }
- return true;
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $url = GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/InProcessCache';
- $versions[] = array('name' => 'InProcessCache',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Evan Prodromou',
- 'homepage' => $url,
- 'description' =>
-
- _m('Additional in-process cache for plugins.'));
- return true;
- }
-
- function cleanup()
- {
- if ($this->active && common_config('inprocess', 'stats')) {
- $this->log(LOG_INFO, "cache size: " .
- count($this->_items));
- $sum = 0;
- foreach ($this->_hits as $hitcount) {
- $sum += $hitcount;
- }
- $this->log(LOG_INFO, $sum . " hits on " .
- count($this->_hits) . " keys");
- }
- return true;
- }
- }
|