123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- defined('GNUSOCIAL') || die();
- use Predis\Client;
- use Predis\PredisException;
- class RedisCachePlugin extends Plugin
- {
- const PLUGIN_VERSION = '0.1.0';
- public static $cacheInitialized = false;
- public $persistent = null;
-
- public $server = null;
- public $defaultExpiry = 86400;
- protected $client = null;
- public function onInitializePlugin()
- {
- if (self::$cacheInitialized) {
- $this->persistent = true;
- } else {
-
-
-
-
-
-
-
-
- $this->persistent = (php_sapi_name() === 'cli') ? false : true;
- }
- $this->ensureConn();
- self::$cacheInitialized = true;
- return true;
- }
- public function onStartCacheGet($key, &$value)
- {
- try {
- $this->ensureConn();
- $data = $this->client->get($key);
- } catch (PredisException $e) {
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- return true;
- }
- if (is_null($data)) {
-
- return true;
- }
- $ret = unserialize($data);
- if ($ret === false && $data !== 'b:0;') {
- common_log(LOG_ERR, 'RedisCache could not handle: ' . $data);
- return true;
- }
-
-
- $value = $ret;
- return false;
- }
- public function onStartCacheSet($key, $value, $flag, $expiry, &$success)
- {
- $success = false;
- if (is_null($expiry)) {
- $expiry = $this->defaultExpiry;
- }
- try {
- $this->ensureConn();
- $ret = $this->client->setex($key, $expiry, serialize($value));
- } catch (PredisException $e) {
- $ret = false;
- common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
- }
- if (is_bool($ret)
- || is_numeric($ret)) {
- $success = ($ret ? true : false);
- } elseif (is_object($ret) && method_exists($ret, 'getPayload')) {
- $success = ($ret->getPayload() === 'OK');
- }
- return !$success;
- }
- public 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;
- }
- public 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 onStartCacheReconnect(bool &$success): bool
- {
- if (is_null($this->client)) {
-
- return true;
- }
- if ($this->persistent) {
- common_log(LOG_ERR, 'Cannot close persistent Redis connection');
- $success = false;
- } else {
- common_log(LOG_INFO, 'Closing Redis connection');
- $success = $this->client->disconnect();
- $this->client = null;
- }
- return false;
- }
- private function ensureConn(): void
- {
- if (is_null($this->client)) {
- $this->client = new Client(
- $this->server,
- ['persistent' => $this->persistent]
- );
- }
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = [
- 'name' => 'RedisCache',
- 'version' => self::PLUGIN_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;
- }
- }
|