From 059b1f63f8067c54bf9c16cba9710605eb7c9f89 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 4 Mar 2018 23:28:49 -0500 Subject: [PATCH] Add support for multiple Memcached servers - Updated htconfig documentation --- doc/htconfig.md | 3 +-- src/Core/Cache.php | 5 ++--- src/Core/Cache/MemcachedCacheDriver.php | 8 +++++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/htconfig.md b/doc/htconfig.md index 0355c3df27..b6fd66bc26 100644 --- a/doc/htconfig.md +++ b/doc/htconfig.md @@ -62,8 +62,7 @@ Example: To set the automatic database cleanup process add this line to your .ht * **cache_driver** ([database]|memcache|memcached) - Whether to use Memcache or Memcached to store temporary cache * **memcache_host** - Host name of the memcache daemon. Default is '127.0.0.1'. * **memcache_port** - Port number of the memcache daemon. Default is 11211. -* **memcached_host** - Host name of the memcached daemon. Default is '127.0.0.1'. -* **memcached_port** - Port number of the memcached daemon. Default is 11211. +* **memcached_hosts** - Array of Memcached servers info [host, port(, weight)]. Default is [['127.0.0.1', 11211]] * **no_count** (Boolean) - Don't do count calculations (currently only when showing albums) * **no_oembed** (Boolean) - Don't use OEmbed to fetch more information about a link. * **no_smilies** (Boolean) - Don't show smilies. diff --git a/src/Core/Cache.php b/src/Core/Cache.php index aeda29e849..a1b1ecb9c1 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -36,10 +36,9 @@ class Cache extends \Friendica\BaseObject self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port); break; case 'memcached': - $memcached_host = Config::get('system', 'memcached_host', '127.0.0.1'); - $memcached_port = Config::get('system', 'memcached_port', 11211); + $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]); - self::$driver = new Cache\MemcachedCacheDriver($memcached_host, $memcached_port); + self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts); break; default: self::$driver = new Cache\DatabaseCacheDriver(); diff --git a/src/Core/Cache/MemcachedCacheDriver.php b/src/Core/Cache/MemcachedCacheDriver.php index 9101c79195..8f1752cbed 100644 --- a/src/Core/Cache/MemcachedCacheDriver.php +++ b/src/Core/Cache/MemcachedCacheDriver.php @@ -17,7 +17,7 @@ class MemcachedCacheDriver extends BaseObject implements ICacheDriver */ private $memcached; - public function __construct($memcached_host, $memcached_port) + public function __construct(array $memcached_hosts) { if (!class_exists('Memcached', false)) { throw new \Exception('Memcached class isn\'t available'); @@ -25,8 +25,10 @@ class MemcachedCacheDriver extends BaseObject implements ICacheDriver $this->memcached = new \Memcached(); - if (!$this->memcached->addServer($memcached_host, $memcached_port)) { - throw new \Exception('Expected Memcached server at ' . $memcached_host . ':' . $memcached_port . ' isn\'t available'); + $this->memcached->addServers($memcached_hosts); + + if (count($this->memcached->getServerList()) == 0) { + throw new \Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true)); } }