[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 <?php
/** /**
* StatusNet - the distributed open-source microblogging tool * GNU social - a federating social network
* Copyright (C) 2009, StatusNet, Inc.
* *
* 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 * 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 * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
@ -21,48 +23,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category Cache * @category Cache
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Craig Andrews <candrews@integralblue.com> * @author Craig Andrews <candrews@integralblue.com>
* @author Miguel Dantas <biodantas@gmail.com>
* @copyright 2009 StatusNet, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { defined('GNUSOCIAL') || die();
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/**
* 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 class MemcachedPlugin extends Plugin
{ {
const PLUGIN_VERSION = '2.0.0'; const PLUGIN_VERSION = '2.1.0';
static $cacheInitialized = false; static $cacheInitialized = false;
private $_conn = null; public $servers = ['127.0.0.1'];
public $servers = array('127.0.0.1;11211');
public $defaultExpiry = 86400; // 24h public $defaultExpiry = 86400; // 24h
private $_conn = null;
/** /**
* Initialize the plugin * Initialize the plugin
* *
@ -72,8 +55,12 @@ class MemcachedPlugin extends Plugin
*/ */
function onInitializePlugin() function onInitializePlugin()
{ {
$this->_ensureConn(); try {
self::$cacheInitialized = true; $this->_ensureConn();
self::$cacheInitialized = true;
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
}
return true; return true;
} }
@ -89,9 +76,19 @@ class MemcachedPlugin extends Plugin
*/ */
function onStartCacheGet(&$key, &$value) function onStartCacheGet(&$key, &$value)
{ {
$this->_ensureConn(); try {
$value = $this->_conn->get($key); $this->_ensureConn();
return false; $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;
}
} }
/** /**
@ -107,12 +104,17 @@ class MemcachedPlugin extends Plugin
*/ */
function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success) function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
{ {
$this->_ensureConn();
if ($expiry === null) { if ($expiry === null) {
$expiry = $this->defaultExpiry; $expiry = $this->defaultExpiry;
} }
$success = $this->_conn->set($key, $value, $expiry); try {
return false; $this->_ensureConn();
$success = $this->_conn->set($key, $value, $expiry);
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
return !$success;
} }
/** /**
@ -127,9 +129,19 @@ class MemcachedPlugin extends Plugin
*/ */
function onStartCacheIncrement(&$key, &$step, &$value) function onStartCacheIncrement(&$key, &$step, &$value)
{ {
$this->_ensureConn(); try {
$value = $this->_conn->increment($key, $step); $this->_ensureConn();
return false; $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;
}
} }
/** /**
@ -142,14 +154,23 @@ class MemcachedPlugin extends Plugin
*/ */
function onStartCacheDelete(&$key, &$success) function onStartCacheDelete(&$key, &$success)
{ {
$this->_ensureConn(); try {
$success = $this->_conn->delete($key); $this->_ensureConn();
return false; $success = $this->_conn->delete($key);
} catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
return true;
}
return !$success;
} }
function onStartCacheReconnect(&$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; return true;
} }
@ -167,28 +188,28 @@ class MemcachedPlugin extends Plugin
$this->_conn = new Memcached(common_config('site', 'nickname')); $this->_conn = new Memcached(common_config('site', 'nickname'));
if (!count($this->_conn->getServerList())) { if (!count($this->_conn->getServerList())) {
if (is_array($this->servers)) { if (is_array($this->servers)) {
$servers = $this->servers; $servers = $this->servers;
} else {
$servers = array($this->servers);
}
foreach ($servers as $server) {
if (strpos($server, ';') !== false) {
list($host, $port) = explode(';', $server);
} else { } else {
$host = $server; $servers = [$this->servers];
$port = 11211; }
foreach ($servers as $server) {
if (is_array($server) && count($server) === 2) {
list($host, $port) = $server;
} else {
$host = is_array($server) ? $server[0] : $server;
$port = 11211;
}
$this->_conn->addServer($host, $port);
} }
$this->_conn->addServer($host, $port); // Compress items stored in the cache.
}
// Compress items stored in the cache. // Allows the cache to store objects larger than 1MB (if they
// compress to less than 1MB), and improves cache memory efficiency.
// Allows the cache to store objects larger than 1MB (if they $this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
// compress to less than 1MB), and improves cache memory efficiency.
$this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
} }
} }
} }

View File

@ -2,20 +2,37 @@ The Memcached plugin implements cache interface for memcached.
See: http://memcached.org/ 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 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 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) defaultExpiry: How long before cache expires (in seconds)
Example 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
]);