From ecb82a9fa79982616efc57c6d887c5b54bbe6a90 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Wed, 22 Mar 2017 20:44:35 +0100 Subject: [PATCH 1/2] Attempting to fix issue #445 --- examples/memcache.php | 13 ++++++++++--- examples/memcached.php | 13 ++++++++++--- src/phpFastCache/Drivers/Memcache/Driver.php | 16 +++++++++++----- src/phpFastCache/Drivers/Memcached/Driver.php | 16 +++++++++++----- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/examples/memcache.php b/examples/memcache.php index 753322f14..9d3394ead 100644 --- a/examples/memcache.php +++ b/examples/memcache.php @@ -17,14 +17,21 @@ // Include composer autoloader require __DIR__ . '/../vendor/autoload.php'; -$InstanceCache = CacheManager::getInstance('memcache'); +$InstanceCache = CacheManager::getInstance('memcache', ['servers' => [ + [ + 'host' =>'127.0.0.1', + 'port' => 11211, + // 'sasl_user' => false, // optional + // 'sasl_password' => false // optional + ], +]]); + /** * In case you need to enable compress_data option: * $InstanceCache = CacheManager::getInstance('memcache', ['compress_data' => true]); * * In case you need SASL authentication: - * $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']); - * Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: + * Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: * http://php.net/manual/fr/memcached.installation.php */ diff --git a/examples/memcached.php b/examples/memcached.php index f29627208..498eff098 100644 --- a/examples/memcached.php +++ b/examples/memcached.php @@ -17,11 +17,18 @@ // Include composer autoloader require __DIR__ . '/../vendor/autoload.php'; -$InstanceCache = CacheManager::getInstance('memcached'); +$InstanceCache = CacheManager::getInstance(['servers' => [ + [ + 'host' =>'127.0.0.1', + 'port' => 11211, + // 'sasl_user' => false, // optional + // 'sasl_password' => false // optional + ], +]]); + /** * In case you need SASL authentication: - * $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']); - * Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: + * Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: * http://php.net/manual/fr/memcached.installation.php */ diff --git a/src/phpFastCache/Drivers/Memcache/Driver.php b/src/phpFastCache/Drivers/Memcache/Driver.php index 5511a7f8a..de824a452 100644 --- a/src/phpFastCache/Drivers/Memcache/Driver.php +++ b/src/phpFastCache/Drivers/Memcache/Driver.php @@ -17,7 +17,6 @@ use Memcache as MemcacheSoftware; use phpFastCache\Core\DriverAbstract; use phpFastCache\Core\MemcacheDriverCollisionDetectorTrait; -use phpFastCache\Core\StandardPsr6StructureTrait; use phpFastCache\Entities\driverStatistic; use phpFastCache\Exceptions\phpFastCacheDriverCheckException; use phpFastCache\Exceptions\phpFastCacheDriverException; @@ -26,6 +25,7 @@ /** * Class Driver * @package phpFastCache\Drivers + * @property MemcacheSoftware $instance */ class Driver extends DriverAbstract { @@ -49,7 +49,6 @@ public function __construct(array $config = []) if (!$this->driverCheck()) { throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); } else { - $this->instance = new MemcacheSoftware(); $this->driverConnect(); if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) { @@ -128,16 +127,23 @@ protected function driverClear() */ protected function driverConnect() { - $servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []); + $this->instance = new MemcacheSoftware(); + + $servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); if (count($servers) < 1) { $servers = [ - ['127.0.0.1', 11211], + [ + 'host' =>'127.0.0.1', + 'port' => 11211, + 'sasl_user' => false, + 'sasl_password' => false + ], ]; } foreach ($servers as $server) { try { - if (!$this->instance->addserver($server[ 0 ], $server[ 1 ])) { + if (!$this->instance->addServer($server['host'], $server['port'])) { $this->fallback = true; } if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){ diff --git a/src/phpFastCache/Drivers/Memcached/Driver.php b/src/phpFastCache/Drivers/Memcached/Driver.php index 61cb63945..30f152aed 100644 --- a/src/phpFastCache/Drivers/Memcached/Driver.php +++ b/src/phpFastCache/Drivers/Memcached/Driver.php @@ -17,7 +17,6 @@ use Memcached as MemcachedSoftware; use phpFastCache\Core\DriverAbstract; use phpFastCache\Core\MemcacheDriverCollisionDetectorTrait; -use phpFastCache\Core\StandardPsr6StructureTrait; use phpFastCache\Entities\driverStatistic; use phpFastCache\Exceptions\phpFastCacheDriverCheckException; use phpFastCache\Exceptions\phpFastCacheDriverException; @@ -26,6 +25,7 @@ /** * Class Driver * @package phpFastCache\Drivers + * @property MemcachedSoftware $instance */ class Driver extends DriverAbstract { @@ -44,7 +44,6 @@ public function __construct(array $config = []) if (!$this->driverCheck()) { throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); } else { - $this->instance = new MemcachedSoftware(); $this->driverConnect(); } } @@ -127,16 +126,23 @@ protected function driverClear() */ protected function driverConnect() { - $servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []); + $this->instance = new MemcachedSoftware(); + + $servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); if (count($servers) < 1) { $servers = [ - ['127.0.0.1', 11211], + [ + 'host' =>'127.0.0.1', + 'port' => 11211, + 'sasl_user' => false, + 'sasl_password' => false + ], ]; } foreach ($servers as $server) { try { - if (!$this->instance->addServer($server[ 0 ], $server[ 1 ])) { + if (!$this->instance->addServer($server['host'], $server['port'])) { $this->fallback = true; } if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){ From 2e4d211aceb1a98c2421ef4f1caea5f1cd9536da Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Wed, 22 Mar 2017 20:53:09 +0100 Subject: [PATCH 2/2] Attempting to fix issue #445 (with more logic) --- src/phpFastCache/Drivers/Memcache/Driver.php | 8 ++++---- src/phpFastCache/Drivers/Memcached/Driver.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/phpFastCache/Drivers/Memcache/Driver.php b/src/phpFastCache/Drivers/Memcache/Driver.php index de824a452..839efb789 100644 --- a/src/phpFastCache/Drivers/Memcache/Driver.php +++ b/src/phpFastCache/Drivers/Memcache/Driver.php @@ -133,10 +133,10 @@ protected function driverConnect() if (count($servers) < 1) { $servers = [ [ - 'host' =>'127.0.0.1', - 'port' => 11211, - 'sasl_user' => false, - 'sasl_password' => false + 'host' => !empty($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1', + 'port' => !empty($this->config[ 'port' ]) ? $this->config[ 'port' ] : 11211, + 'sasl_user' => !empty($this->config[ 'sasl_user' ]) ? $this->config[ 'sasl_user' ] : false, + 'sasl_password' => !empty($this->config[ 'sasl_password' ]) ? $this->config[ 'sasl_password' ] : false, ], ]; } diff --git a/src/phpFastCache/Drivers/Memcached/Driver.php b/src/phpFastCache/Drivers/Memcached/Driver.php index 30f152aed..258716aa5 100644 --- a/src/phpFastCache/Drivers/Memcached/Driver.php +++ b/src/phpFastCache/Drivers/Memcached/Driver.php @@ -132,10 +132,10 @@ protected function driverConnect() if (count($servers) < 1) { $servers = [ [ - 'host' =>'127.0.0.1', - 'port' => 11211, - 'sasl_user' => false, - 'sasl_password' => false + 'host' => !empty($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1', + 'port' => !empty($this->config[ 'port' ]) ? $this->config[ 'port' ] : 11211, + 'sasl_user' => !empty($this->config[ 'sasl_user' ]) ? $this->config[ 'sasl_user' ] : false, + 'sasl_password' => !empty($this->config[ 'sasl_password' ]) ? $this->config[ 'sasl_password' ] : false, ], ]; }