2019-08-13 09:31:05 +09:00
|
|
|
<?php
|
2020-07-07 02:10:12 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2019-08-13 09:31:05 +09:00
|
|
|
|
|
|
|
/**
|
2020-08-14 04:51:22 +09:00
|
|
|
* Plugin implementing Redis based caching
|
2019-08-13 09:31:05 +09:00
|
|
|
*
|
|
|
|
* @category Files
|
|
|
|
* @package GNUsocial
|
2019-08-13 10:52:35 +09:00
|
|
|
* @author Stéphane Bérubé <chimo@chromic.org>
|
2019-08-13 09:31:05 +09:00
|
|
|
* @author Miguel Dantas <biodantas@gmail.com>
|
2020-07-07 02:10:12 +09:00
|
|
|
* @copyright 2018, 2019 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2019-08-13 09:31:05 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
2019-08-13 10:52:35 +09:00
|
|
|
use Predis\Client;
|
|
|
|
use Predis\PredisException;
|
|
|
|
|
2019-08-13 09:31:05 +09:00
|
|
|
class RedisCachePlugin extends Plugin
|
|
|
|
{
|
2019-12-11 11:20:54 +09:00
|
|
|
const PLUGIN_VERSION = '0.1.0';
|
2019-08-13 09:31:05 +09:00
|
|
|
|
2020-08-14 04:51:22 +09:00
|
|
|
public static $cacheInitialized = false;
|
|
|
|
|
|
|
|
public $persistent = null;
|
|
|
|
|
2019-08-13 10:52:35 +09:00
|
|
|
// settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
|
|
|
|
public $server = null;
|
2019-08-13 09:31:05 +09:00
|
|
|
public $defaultExpiry = 86400; // 24h
|
|
|
|
|
2019-08-13 10:52:35 +09:00
|
|
|
protected $client = null;
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
public function onInitializePlugin()
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
2020-08-14 04:51:22 +09:00
|
|
|
if (self::$cacheInitialized) {
|
|
|
|
$this->persistent = true;
|
|
|
|
} else {
|
|
|
|
// If we're a parent command-line process we need
|
|
|
|
// to be able to close out the connection after
|
|
|
|
// forking, so disable persistence.
|
|
|
|
//
|
|
|
|
// We'll turn it back on again the second time
|
|
|
|
// through which will either be in a child process,
|
|
|
|
// or a single-process script which is switching
|
|
|
|
// configurations.
|
|
|
|
$this->persistent = (php_sapi_name() === 'cli') ? false : true;
|
|
|
|
}
|
2019-08-13 09:31:05 +09:00
|
|
|
|
2020-08-14 04:51:22 +09:00
|
|
|
$this->ensureConn();
|
|
|
|
self::$cacheInitialized = true;
|
2019-08-13 09:31:05 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
public function onStartCacheGet($key, &$value)
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
|
|
|
try {
|
2020-08-14 04:51:22 +09:00
|
|
|
$this->ensureConn();
|
2020-08-10 17:12:31 +09:00
|
|
|
$data = $this->client->get($key);
|
2020-07-07 02:10:12 +09:00
|
|
|
} catch (PredisException $e) {
|
2019-08-13 10:52:35 +09:00
|
|
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
2019-08-13 09:31:05 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:12:31 +09:00
|
|
|
if (is_null($data)) {
|
|
|
|
// Miss, let GS do its thing
|
|
|
|
return true;
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:12:31 +09:00
|
|
|
$ret = unserialize($data);
|
|
|
|
|
|
|
|
if ($ret === false && $data !== 'b:0;') {
|
|
|
|
common_log(LOG_ERR, 'RedisCache could not handle: ' . $data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit, overwrite "value" and return false
|
|
|
|
// to indicate we took care of this
|
|
|
|
$value = $ret;
|
|
|
|
return false;
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
public function onStartCacheSet($key, $value, $flag, $expiry, &$success)
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
2020-07-07 02:10:12 +09:00
|
|
|
$success = false;
|
|
|
|
|
|
|
|
if (is_null($expiry)) {
|
2019-08-13 09:31:05 +09:00
|
|
|
$expiry = $this->defaultExpiry;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-08-14 04:51:22 +09:00
|
|
|
$this->ensureConn();
|
2019-08-13 09:31:05 +09:00
|
|
|
$ret = $this->client->setex($key, $expiry, serialize($value));
|
2020-07-07 02:10:12 +09:00
|
|
|
} catch (PredisException $e) {
|
|
|
|
$ret = false;
|
2019-08-13 10:52:35 +09:00
|
|
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
if (is_bool($ret)
|
|
|
|
|| is_numeric($ret)) {
|
|
|
|
$success = ($ret ? true : false);
|
|
|
|
} elseif (is_object($ret) && method_exists($ret, 'getPayload')) {
|
|
|
|
$success = ($ret->getPayload() === 'OK');
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
return !$success;
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
public function onStartCacheDelete($key)
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
2019-08-13 10:52:35 +09:00
|
|
|
if ($key === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-13 09:31:05 +09:00
|
|
|
try {
|
2020-08-14 04:51:22 +09:00
|
|
|
$this->ensureConn();
|
2019-08-13 10:52:35 +09:00
|
|
|
$ret = $this->client->del($key);
|
2020-07-07 02:10:12 +09:00
|
|
|
} catch (PredisException $e) {
|
2019-08-13 10:52:35 +09:00
|
|
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2019-08-13 10:52:35 +09:00
|
|
|
// Let other caches delete stuff if we didn't succeed
|
2019-08-14 22:53:57 +09:00
|
|
|
return isset($ret) && $ret === 1;
|
2019-08-13 09:31:05 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:10:12 +09:00
|
|
|
public function onStartCacheIncrement($key, $step, $value)
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
|
|
|
try {
|
2020-08-14 04:51:22 +09:00
|
|
|
$this->ensureConn();
|
2019-08-13 09:31:05 +09:00
|
|
|
$this->client->incrby($key, $step);
|
2020-07-07 02:10:12 +09:00
|
|
|
} catch (PredisException $e) {
|
2019-08-13 10:52:35 +09:00
|
|
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
2019-08-13 09:31:05 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-14 04:51:22 +09:00
|
|
|
public function onStartCacheReconnect(bool &$success): bool
|
|
|
|
{
|
|
|
|
if (is_null($this->client)) {
|
|
|
|
// nothing to do
|
|
|
|
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]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 23:03:30 +09:00
|
|
|
public function onPluginVersion(array &$versions): bool
|
2019-08-13 09:31:05 +09:00
|
|
|
{
|
2019-12-11 11:20:54 +09:00
|
|
|
$versions[] = [
|
|
|
|
'name' => 'RedisCache',
|
2020-07-07 02:10:12 +09:00
|
|
|
'version' => self::PLUGIN_VERSION,
|
2019-12-11 11:20:54 +09:00
|
|
|
'author' => 'Stéphane Bérubé (chimo)',
|
|
|
|
'homepage' => 'https://github.com/chimo/gs-rediscache',
|
|
|
|
'description' =>
|
|
|
|
// TRANS: Plugin description.
|
|
|
|
_m('Plugin implementing Redis as a backend for GNU social caching')
|
|
|
|
];
|
2019-08-13 09:31:05 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|