RedisCache/RedisCachePlugin.php

122 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2017-02-04 06:21:43 +00:00
<?php
/**
* Plugin to implement cache interface forRedis
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Cache
* @package PostActiv
* @author Moon Man <shitposterclub@gmail.com>
* @copyright 2017 Shitposter Club
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://shitposter.club/
*/
if (!defined('STATUSNET')) {
exit(1);
}
class RedisCachePlugin extends Plugin
{
static $cacheInitialized = false;
private $_conn = null;
public $namespace = null;
public $host = null;
public $port = null;
public $defaultExpiry = 86400; // 24hr
function onInitializePlugin()
{
$this->_ensureConn();
self::$cacheInitialized = true;
return true;
}
function onStartCacheGet(&$key, &$value)
{
$this->_ensureConn();
$value = $this->_conn->get($key);
Event::handle('EndCacheGet', array($key, &$value));
return false;
}
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
{
$this->_ensureConn();
if ($expiry === null) {
$expiry = $this->defaultExpiry;
}
$success = $this->_conn->set($key, $value, $expiry);
Event::handle('EndCacheSet', array($key, $value, $flag,
$expiry));
return false;
}
function onStartCacheIncrement(&$key, &$step, &$value)
{
$this->_ensureConn();
$value = $this->_conn->incrBy($key, $step);
Event::handle('EndCacheIncrement', array($key, $step, $value));
return false;
}
function onStartCacheDelete(&$key, &$success)
{
$this->_ensureConn();
$success = $this->_conn->delete($key);
Event::handle('EndCacheDelete', array($key));
return false;
}
function onStartCacheReconnect(&$success)
{
// nothing to do
return true;
}
private function _ensureConn()
{
if (empty($this->namespace)) {
$this->namespace = common_config('site', 'nickname') . 'rcp:';
}
if (empty($this->_conn)) {
$this->_conn = new Redis();
}
$this->_conn->pconnect($this->host, $this->port);
$this->_conn->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$this->_conn->setOption(Redis::OPT_PREFIX, $this->namespace);
}
protected function flag($flag)
{
//no flags are presently supported
return $flag;
}
function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Redis Cache',
'version' => '0.0.1',
'author' => 'MoonMan',
'homepage' => 'https://gitgud.io/ShitposterClub/RedisCache/',
'rawdescription' =>
_m('Use Redis to cache query results.'));
return true;
}
}