[CACHING] Imported Chimo's RedisCache plugin and fixed some small parts
This commit is contained in:
parent
4f8ede8239
commit
d8d7abee9f
@ -20,22 +20,29 @@
|
|||||||
*
|
*
|
||||||
* @category Files
|
* @category Files
|
||||||
* @package GNUsocial
|
* @package GNUsocial
|
||||||
* @author Chimo
|
* @author Stéphane Bérubé <chimo@chromic.org>
|
||||||
* @author Miguel Dantas <biodantas@gmail.com>
|
* @author Miguel Dantas <biodantas@gmail.com>
|
||||||
* @copyright 2008-2009, 2019 Free Software Foundation http://fsf.org
|
* @copyright 2018, 2019 Free Software Foundation http://fsf.org
|
||||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||||
* @link https://www.gnu.org/software/social/
|
* @link https://www.gnu.org/software/social/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
defined('GNUSOCIAL') || die();
|
defined('GNUSOCIAL') || die();
|
||||||
|
|
||||||
|
use Predis\Client;
|
||||||
|
use Predis\PredisException;
|
||||||
|
|
||||||
class RedisCachePlugin extends Plugin
|
class RedisCachePlugin extends Plugin
|
||||||
{
|
{
|
||||||
const VERSION = '0.0.1';
|
const PLUGIN_VERSION = '0.0.1';
|
||||||
|
|
||||||
private $client = null;
|
// settings which can be set in config.php with addPlugin('Embed', ['param'=>'value', ...]);
|
||||||
|
public $server = null;
|
||||||
public $defaultExpiry = 86400; // 24h
|
public $defaultExpiry = 86400; // 24h
|
||||||
|
|
||||||
|
|
||||||
|
protected $client = null;
|
||||||
|
|
||||||
function onInitializePlugin()
|
function onInitializePlugin()
|
||||||
{
|
{
|
||||||
$this->_ensureConn();
|
$this->_ensureConn();
|
||||||
@ -46,27 +53,17 @@ class RedisCachePlugin extends Plugin
|
|||||||
private function _ensureConn()
|
private function _ensureConn()
|
||||||
{
|
{
|
||||||
if ($this->client === null) {
|
if ($this->client === null) {
|
||||||
$connection = common_config('rediscache', 'connection');
|
$this->client = new Client($this->server);
|
||||||
|
|
||||||
$this->client = new Predis\Client($connection);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onStartCacheGet(&$key, &$value)
|
function onStartCacheGet(&$key, &$value)
|
||||||
{
|
{
|
||||||
// Temporary work-around upstream bug
|
|
||||||
// see: https://github.com/chimo/gs-rediscache/issues/1
|
|
||||||
if ($key === Cache::key('profileall')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->_ensureConn();
|
$this->_ensureConn();
|
||||||
|
|
||||||
$ret = $this->client->get($key);
|
$ret = $this->client->get($key);
|
||||||
} catch(\Predis\PredisException $Ex) {
|
} catch(PredisException $e) {
|
||||||
common_log(LOG_ERR, get_class($Ex) . ' ' . $Ex->getMessage());
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,8 +71,6 @@ class RedisCachePlugin extends Plugin
|
|||||||
// to indicate we took care of this
|
// to indicate we took care of this
|
||||||
if ($ret !== null) {
|
if ($ret !== null) {
|
||||||
$value = unserialize($ret);
|
$value = unserialize($ret);
|
||||||
|
|
||||||
Event::handle('EndCacheGet', array($key, &$value));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,12 +80,6 @@ class RedisCachePlugin extends Plugin
|
|||||||
|
|
||||||
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
|
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
|
||||||
{
|
{
|
||||||
// Temporary work-around upstream bug
|
|
||||||
// see: https://github.com/chimo/gs-rediscache/issues/1
|
|
||||||
if ($key === Cache::key('profileall')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($expiry === null) {
|
if ($expiry === null) {
|
||||||
$expiry = $this->defaultExpiry;
|
$expiry = $this->defaultExpiry;
|
||||||
}
|
}
|
||||||
@ -99,17 +88,13 @@ class RedisCachePlugin extends Plugin
|
|||||||
$this->_ensureConn();
|
$this->_ensureConn();
|
||||||
|
|
||||||
$ret = $this->client->setex($key, $expiry, serialize($value));
|
$ret = $this->client->setex($key, $expiry, serialize($value));
|
||||||
} catch(\Predis\PredisException $Ex) {
|
} catch(PredisException $e) {
|
||||||
common_log(LOG_ERR, get_class($Ex) . ' ' . $Ex->getMessage());
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ret->getPayload() === "OK") {
|
if ($ret->getPayload() === "OK") {
|
||||||
$success = true;
|
$success = true;
|
||||||
|
|
||||||
Event::handle('EndCacheSet', array($key, $value, $flag, $expiry));
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,33 +103,31 @@ class RedisCachePlugin extends Plugin
|
|||||||
|
|
||||||
function onStartCacheDelete($key)
|
function onStartCacheDelete($key)
|
||||||
{
|
{
|
||||||
try {
|
if ($key === null) {
|
||||||
$this->_ensureConn();
|
return true;
|
||||||
|
|
||||||
$this->client->del($key);
|
|
||||||
} catch(\Predis\PredisException $Ex) {
|
|
||||||
common_log(LOG_ERR, get_class($Ex) . ' ' . $Ex->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let other Caches delete stuff if they want to
|
try {
|
||||||
return true;
|
$this->_ensureConn();
|
||||||
|
$ret = $this->client->del($key);
|
||||||
|
} catch(PredisException $e) {
|
||||||
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let other caches delete stuff if we didn't succeed
|
||||||
|
return $ret === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onStartCacheIncrement(&$key, &$step, &$value)
|
function onStartCacheIncrement(&$key, &$step, &$value)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->_ensureConn();
|
$this->_ensureConn();
|
||||||
|
|
||||||
// TODO: handle when this fails
|
|
||||||
$this->client->incrby($key, $step);
|
$this->client->incrby($key, $step);
|
||||||
} catch(\Predis\PredisException $Ex) {
|
} catch(PredisException $e) {
|
||||||
common_log(LOG_ERR, get_class($Ex) . ' ' . $Ex->getMessage());
|
common_log(LOG_ERR, 'RedisCache encountered exception ' . get_class($e) . ': ' . $e->getMessage());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::handle('EndCacheIncrement', array($key, $step, $value));
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +139,7 @@ class RedisCachePlugin extends Plugin
|
|||||||
'homepage' => 'https://github.com/chimo/gs-rediscache',
|
'homepage' => 'https://github.com/chimo/gs-rediscache',
|
||||||
'description' =>
|
'description' =>
|
||||||
// TRANS: Plugin description.
|
// TRANS: Plugin description.
|
||||||
_m('')); // TODO
|
_m(''));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user