commit e2ce83041dd6e4942f3a0588fea19576d784f724 Author: moonman Date: Sat Feb 4 07:21:43 2017 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b1dced --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# Redis Cache Plugin for PostActiv + +## About + +WARNING: THIS IS ALPHA CODE, IT IS PRERELEASE AND SHOULD ONLY BE INSTALLED TO +HELP TEST OR YOU ARE WILLING TO TAKE RISKS. + +Caches database objects in Redis. Functions almost identically to the Memcached +plugin for GNU Social except that it uses Redis. The code is derived from that +plugin. + +It might work with GNU Social also. Try it and tell me. + +## Install + +Install phpredis package either through your distribution package manager or +PECL or compile it. + +- Move the project directory to ${POSTACTIV}/plugins +- Add to your config.php: addPlugin('RedisCache'); + +By default it will use localhost and the standard Redis port to connect. You +can also pass a named parameter array to the plugin with "host" and "port" +values to explicitly set this, or only set "host" to a named pipe. + +Example: addPlugin('RedisCache', array('host' => '10.0.0.1', port => 1679)); + +It namespaces its keys using the site shortname, so it should properly +separate caches in a multisite configuration unless you did something stupid +like set multiple sites with the same shortname. + +## Usage + +You don't have to do anything, it will just start caching stuff that goes +through the managed dboject system and hopefully make your site faster. + +## License + +GNU Affero License + +## Thanks + +Evan Prodromou and Craig Andrews, authors of the original Memcached plugin. diff --git a/RedisCachePlugin.php b/RedisCachePlugin.php new file mode 100644 index 0000000..fa15a89 --- /dev/null +++ b/RedisCachePlugin.php @@ -0,0 +1,121 @@ +. + * + * @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; + } +} +