[PLUGINS] Updated and reviewed the Memcached plugin

This commit is contained in:
Miguel Dantas 2019-08-15 16:05:33 +01:00 committed by Diogo Peralta Cordeiro
parent f689df3e68
commit ac97efbf21
2 changed files with 109 additions and 71 deletions

View File

@ -1,13 +1,15 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc.
* GNU social - a federating social network
*
* Plugin to implement cache interface for memcached
* A plugin to use memcached for the interface with memcache
*
* PHP version 5
* This used to be encoded as config-variable options in the core code;
* it's now broken out to a separate plugin. The same interface can be
* implemented by other plugins.
*
* This program is free software: you can redistribute it and/or modify
* LICENCE: 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.
@ -21,48 +23,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Cache
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @author Craig Andrews <candrews@integralblue.com>
* @author Miguel Dantas <biodantas@gmail.com>
* @copyright 2009 StatusNet, Inc.
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
* @copyright 2009, 2019 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
defined('GNUSOCIAL') || die();
/**
* A plugin to use memcached for the cache interface
*
* This used to be encoded as config-variable options in the core code;
* it's now broken out to a separate plugin. The same interface can be
* implemented by other plugins.
*
* @category Cache
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Craig Andrews <candrews@integralblue.com>
* @copyright 2009 StatusNet, Inc.
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class MemcachedPlugin extends Plugin
{
const PLUGIN_VERSION = '2.0.0';
const PLUGIN_VERSION = '2.1.0';
static $cacheInitialized = false;
private $_conn = null;
public $servers = array('127.0.0.1;11211');
public $servers = ['127.0.0.1'];
public $defaultExpiry = 86400; // 24h
private $_conn = null;
/**
* Initialize the plugin
*
@ -72,8 +55,12 @@ class MemcachedPlugin extends Plugin
*/
function onInitializePlugin()
{
try {
$this->_ensureConn();
self::$cacheInitialized = true;
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
}
return true;
}
@ -89,10 +76,20 @@ class MemcachedPlugin extends Plugin
*/
function onStartCacheGet(&$key, &$value)
{
try {
$this->_ensureConn();
$value = $this->_conn->get($key);
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
if ($value === false) {
// If not found, let other plugins handle it
return $this->_conn->getResultCode() === Memcached::RES_NOTFOUND;
} else {
return false;
}
}
/**
* Associate a value with a key
@ -107,12 +104,17 @@ class MemcachedPlugin extends Plugin
*/
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
{
$this->_ensureConn();
if ($expiry === null) {
$expiry = $this->defaultExpiry;
}
try {
$this->_ensureConn();
$success = $this->_conn->set($key, $value, $expiry);
return false;
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
return !$success;
}
/**
@ -127,10 +129,20 @@ class MemcachedPlugin extends Plugin
*/
function onStartCacheIncrement(&$key, &$step, &$value)
{
try {
$this->_ensureConn();
$value = $this->_conn->increment($key, $step);
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
if ($value === false) {
// If not found, let other plugins handle it
return $this->_conn->getResultCode() === Memcached::RES_NOTFOUND;
} else {
return false;
}
}
/**
* Delete a value associated with a key
@ -142,14 +154,23 @@ class MemcachedPlugin extends Plugin
*/
function onStartCacheDelete(&$key, &$success)
{
try {
$this->_ensureConn();
$success = $this->_conn->delete($key);
return false;
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
return !$success;
}
function onStartCacheReconnect(&$success)
{
// nothing to do
try {
$this->_ensureConn();
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
}
return true;
}
@ -170,13 +191,13 @@ class MemcachedPlugin extends Plugin
if (is_array($this->servers)) {
$servers = $this->servers;
} else {
$servers = array($this->servers);
$servers = [$this->servers];
}
foreach ($servers as $server) {
if (strpos($server, ';') !== false) {
list($host, $port) = explode(';', $server);
if (is_array($server) && count($server) === 2) {
list($host, $port) = $server;
} else {
$host = $server;
$host = is_array($server) ? $server[0] : $server;
$port = 11211;
}

View File

@ -2,20 +2,37 @@ The Memcached plugin implements cache interface for memcached.
See: http://memcached.org/
The difference between the former `MemcachePlugin` and `MemcachedPlugin` is that they use,
respectively, `memcache` and `memcached` as the underlying php libraries. These are similar
libraries, made by different authors with slightly different cacpabilities, therefore `memcached`
was selected.
Installation
============
add "addPlugin('Memcached');"
to the bottom of your config.php
Tell your `config.php` to use this plugin (replace `tcp://localhost:6379` with the address/port
of your Memcache backend server):
`addPlugin('Memcached');`
Note that this typically requires configuring the `memcached` extension for `php`, which is
typically available as a binary package from your system's package manager. In Debian based systems
and on Arch based systems, this is likely available under the `php-memcached` package. Note the
final `d`. On Arch, uncomment the line in `/etc/php/conf.d/memcached.ini` and restart your webserver
and/or php-fpm or similar.
Settings
========
servers: Array of memcached servers addresses
servers: Array of memcached servers addresses. Each server address should be either a string with
address or an array where the first element is a string with the address an optional second element
with the port.
defaultExpiry: How long before cache expires (in seconds)
Example
=======
addPlugin('Memcached', array(
'servers' => array('127.0.0.1;11211'),
'defaultExpiry' => 86400 // 24h
));
addPlugin('Memcached', [ 'servers' => ['127.0.0.1', 11211],
'defaultExpiry' => 86400 // 24h
]);