initial commit

This commit is contained in:
moonman 2017-02-04 07:21:43 +01:00
commit e2ce83041d
2 changed files with 164 additions and 0 deletions

43
README.md Normal file
View File

@ -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.

121
RedisCachePlugin.php Normal file
View File

@ -0,0 +1,121 @@
<?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;
}
}