123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- defined('GNUSOCIAL') || die();
- use Predis\Client;
- use Predis\PredisException;
- class RedisCachePlugin extends Plugin
- {
- const PLUGIN_VERSION = '0.1.0';
-
- public $server = null;
- public $defaultExpiry = 86400;
- protected $client = null;
- function onInitializePlugin()
- {
- $this->_ensureConn();
- return true;
- }
- private function _ensureConn()
- {
- if ($this->client === null) {
- $this->client = new Client($this->server);
- }
- }
- function onStartCacheGet(&$key, &$value)
- {
- try {
- $this->_ensureConn();
- $ret = $this->client->get($key);
- } catch(PredisException $e) {
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- return true;
- }
-
-
- if ($ret !== null) {
- $value = unserialize($ret);
- return false;
- }
-
- return true;
- }
- function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
- {
- if ($expiry === null) {
- $expiry = $this->defaultExpiry;
- }
- try {
- $this->_ensureConn();
- $ret = $this->client->setex($key, $expiry, serialize($value));
- } catch(PredisException $e) {
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- return true;
- }
- if (is_int($ret) || (!is_null($ret) && $ret->getPayload() === "OK")) {
- $success = true;
- return false;
- }
- return true;
- }
- function onStartCacheDelete($key)
- {
- if ($key === null) {
- return true;
- }
- try {
- $this->_ensureConn();
- $ret = $this->client->del($key);
- } catch(PredisException $e) {
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- }
-
- return isset($ret) && $ret === 1;
- }
- function onStartCacheIncrement(&$key, &$step, &$value)
- {
- try {
- $this->_ensureConn();
- $this->client->incrby($key, $step);
- } catch(PredisException $e) {
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- return true;
- }
- return false;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = [
- 'name' => 'RedisCache',
- 'version' => self::VERSION,
- 'author' => 'Stéphane Bérubé (chimo)',
- 'homepage' => 'https://github.com/chimo/gs-rediscache',
- 'description' =>
-
- _m('Plugin implementing Redis as a backend for GNU social caching')
- ];
- return true;
- }
- }
|