. * * @category Cache * @package PostActiv * @author Moon Man * @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; } }