[RedisCache] Connections should not be shared by daemon threads

This commit is contained in:
Alexei Sorokin 2020-08-13 22:51:22 +03:00 committed by Diogo Peralta Cordeiro
parent a9ae96fa4c
commit 8fc807343b

View File

@ -15,7 +15,7 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin inplementing Redis based caching
* Plugin implementing Redis based caching
*
* @category Files
* @package GNUsocial
@ -34,6 +34,10 @@ class RedisCachePlugin extends Plugin
{
const PLUGIN_VERSION = '0.1.0';
public static $cacheInitialized = false;
public $persistent = null;
// settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
public $server = null;
public $defaultExpiry = 86400; // 24h
@ -42,22 +46,29 @@ class RedisCachePlugin extends Plugin
public function onInitializePlugin()
{
$this->_ensureConn();
return true;
}
private function _ensureConn()
{
if ($this->client === null) {
$this->client = new Client($this->server);
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;
}
$this->ensureConn();
self::$cacheInitialized = true;
return true;
}
public function onStartCacheGet($key, &$value)
{
try {
$this->_ensureConn();
$this->ensureConn();
$data = $this->client->get($key);
} catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -91,8 +102,7 @@ class RedisCachePlugin extends Plugin
}
try {
$this->_ensureConn();
$this->ensureConn();
$ret = $this->client->setex($key, $expiry, serialize($value));
} catch (PredisException $e) {
$ret = false;
@ -116,7 +126,7 @@ class RedisCachePlugin extends Plugin
}
try {
$this->_ensureConn();
$this->ensureConn();
$ret = $this->client->del($key);
} catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -129,7 +139,7 @@ class RedisCachePlugin extends Plugin
public function onStartCacheIncrement($key, $step, $value)
{
try {
$this->_ensureConn();
$this->ensureConn();
$this->client->incrby($key, $step);
} catch (PredisException $e) {
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -139,6 +149,33 @@ class RedisCachePlugin extends Plugin
return false;
}
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]
);
}
}
public function onPluginVersion(array &$versions): bool
{
$versions[] = [