[Memcached] Merged useful features from Memcache

This commit is contained in:
Miguel Dantas 2019-08-15 16:17:49 +01:00 committed by Diogo Cordeiro
parent 32812c9482
commit afbbbbd4f2

View File

@ -55,6 +55,20 @@ class MemcachedPlugin extends Plugin
*/ */
function onInitializePlugin() function onInitializePlugin()
{ {
if (self::$cacheInitialized) {
$this->persistent = true;
} else {
// If we're a parent command-line process we need
// to be able to close out the connection after
// forking, so disable persistence.
//
// We'll turn it back on again the second time
// through which will either be in a child process,
// or a single-process script which is switching
// configurations.
$this->persistent = (php_sapi_name() == 'cli') ? false : true;
}
try { try {
$this->_ensureConn(); $this->_ensureConn();
self::$cacheInitialized = true; self::$cacheInitialized = true;
@ -96,7 +110,7 @@ class MemcachedPlugin extends Plugin
* *
* @param string &$key in; Key to use for lookups * @param string &$key in; Key to use for lookups
* @param mixed &$value in; Value to associate * @param mixed &$value in; Value to associate
* @param integer &$flag in; Flag empty or Cache::COMPRESSED * @param integer &$flag in; Flag empty or Memcached::OPT_COMPRESSION (translated by the `flag` method)
* @param integer &$expiry in; Expiry (passed through to Memcache) * @param integer &$expiry in; Expiry (passed through to Memcache)
* @param boolean &$success out; Whether the set was successful * @param boolean &$success out; Whether the set was successful
* *
@ -109,6 +123,9 @@ class MemcachedPlugin extends Plugin
} }
try { try {
$this->_ensureConn(); $this->_ensureConn();
if (!empty($flag)) {
$this->_conn->setOption(Memcached::OPT_COMPRESSION, $flag);
}
$success = $this->_conn->set($key, $value, $expiry); $success = $this->_conn->set($key, $value, $expiry);
} catch (MemcachedException $e) { } catch (MemcachedException $e) {
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage()); common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
@ -166,12 +183,19 @@ class MemcachedPlugin extends Plugin
function onStartCacheReconnect(&$success) function onStartCacheReconnect(&$success)
{ {
try { if (empty($this->_conn)) {
$this->_ensureConn(); // nothing to do
} catch (MemcachedException $e) { return true;
common_log(LOG_ERR, 'Memcached encountered exception ' . get_class($e) . ': ' . $e->getMessage());
} }
return true; if ($this->persistent) {
common_log(LOG_ERR, "Cannot close persistent memcached connection");
$success = false;
} else {
common_log(LOG_INFO, "Closing memcached connection");
$success = $this->_conn->close();
$this->_conn = null;
}
return false;
} }
/** /**
@ -221,8 +245,11 @@ class MemcachedPlugin extends Plugin
*/ */
protected function flag($flag) protected function flag($flag)
{ {
//no flags are presently supported $out = 0;
return $flag; if ($flag & Cache::COMPRESSED == Cache::COMPRESSED) {
$out |= Memcached::OPT_COMPRESSION;
}
return $out;
} }
function onPluginVersion(array &$versions) function onPluginVersion(array &$versions)
@ -231,7 +258,7 @@ class MemcachedPlugin extends Plugin
'version' => self::PLUGIN_VERSION, 'version' => self::PLUGIN_VERSION,
'author' => 'Evan Prodromou, Craig Andrews', 'author' => 'Evan Prodromou, Craig Andrews',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Memcached', 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Memcached',
'rawdescription' => 'description' =>
// TRANS: Plugin description. // TRANS: Plugin description.
_m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.')); _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
return true; return true;