Skip to content

Commit 8366cfc

Browse files
authored
Merge pull request #446 from Geolim4/final
Attempting to fix issue #445
2 parents 78cae99 + 2e4d211 commit 8366cfc

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

examples/memcache.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717
// Include composer autoloader
1818
require __DIR__ . '/../vendor/autoload.php';
1919

20-
$InstanceCache = CacheManager::getInstance('memcache');
20+
$InstanceCache = CacheManager::getInstance('memcache', ['servers' => [
21+
[
22+
'host' =>'127.0.0.1',
23+
'port' => 11211,
24+
// 'sasl_user' => false, // optional
25+
// 'sasl_password' => false // optional
26+
],
27+
]]);
28+
2129
/**
2230
* In case you need to enable compress_data option:
2331
* $InstanceCache = CacheManager::getInstance('memcache', ['compress_data' => true]);
2432
*
2533
* In case you need SASL authentication:
26-
* $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']);
27-
* Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see:
34+
* Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see:
2835
* http://php.net/manual/fr/memcached.installation.php
2936
*/
3037

examples/memcached.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
// Include composer autoloader
1818
require __DIR__ . '/../vendor/autoload.php';
1919

20-
$InstanceCache = CacheManager::getInstance('memcached');
20+
$InstanceCache = CacheManager::getInstance(['servers' => [
21+
[
22+
'host' =>'127.0.0.1',
23+
'port' => 11211,
24+
// 'sasl_user' => false, // optional
25+
// 'sasl_password' => false // optional
26+
],
27+
]]);
28+
2129
/**
2230
* In case you need SASL authentication:
23-
* $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']);
24-
* Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see:
31+
* Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see:
2532
* http://php.net/manual/fr/memcached.installation.php
2633
*/
2734

src/phpFastCache/Drivers/Memcache/Driver.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Memcache as MemcacheSoftware;
1818
use phpFastCache\Core\DriverAbstract;
1919
use phpFastCache\Core\MemcacheDriverCollisionDetectorTrait;
20-
use phpFastCache\Core\StandardPsr6StructureTrait;
2120
use phpFastCache\Entities\driverStatistic;
2221
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
2322
use phpFastCache\Exceptions\phpFastCacheDriverException;
@@ -26,6 +25,7 @@
2625
/**
2726
* Class Driver
2827
* @package phpFastCache\Drivers
28+
* @property MemcacheSoftware $instance
2929
*/
3030
class Driver extends DriverAbstract
3131
{
@@ -49,7 +49,6 @@ public function __construct(array $config = [])
4949
if (!$this->driverCheck()) {
5050
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
5151
} else {
52-
$this->instance = new MemcacheSoftware();
5352
$this->driverConnect();
5453

5554
if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) {
@@ -128,16 +127,23 @@ protected function driverClear()
128127
*/
129128
protected function driverConnect()
130129
{
131-
$servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []);
130+
$this->instance = new MemcacheSoftware();
131+
132+
$servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []);
132133
if (count($servers) < 1) {
133134
$servers = [
134-
['127.0.0.1', 11211],
135+
[
136+
'host' => !empty($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1',
137+
'port' => !empty($this->config[ 'port' ]) ? $this->config[ 'port' ] : 11211,
138+
'sasl_user' => !empty($this->config[ 'sasl_user' ]) ? $this->config[ 'sasl_user' ] : false,
139+
'sasl_password' => !empty($this->config[ 'sasl_password' ]) ? $this->config[ 'sasl_password' ] : false,
140+
],
135141
];
136142
}
137143

138144
foreach ($servers as $server) {
139145
try {
140-
if (!$this->instance->addserver($server[ 0 ], $server[ 1 ])) {
146+
if (!$this->instance->addServer($server['host'], $server['port'])) {
141147
$this->fallback = true;
142148
}
143149
if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){

src/phpFastCache/Drivers/Memcached/Driver.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Memcached as MemcachedSoftware;
1818
use phpFastCache\Core\DriverAbstract;
1919
use phpFastCache\Core\MemcacheDriverCollisionDetectorTrait;
20-
use phpFastCache\Core\StandardPsr6StructureTrait;
2120
use phpFastCache\Entities\driverStatistic;
2221
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
2322
use phpFastCache\Exceptions\phpFastCacheDriverException;
@@ -26,6 +25,7 @@
2625
/**
2726
* Class Driver
2827
* @package phpFastCache\Drivers
28+
* @property MemcachedSoftware $instance
2929
*/
3030
class Driver extends DriverAbstract
3131
{
@@ -44,7 +44,6 @@ public function __construct(array $config = [])
4444
if (!$this->driverCheck()) {
4545
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
4646
} else {
47-
$this->instance = new MemcachedSoftware();
4847
$this->driverConnect();
4948
}
5049
}
@@ -127,16 +126,23 @@ protected function driverClear()
127126
*/
128127
protected function driverConnect()
129128
{
130-
$servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []);
129+
$this->instance = new MemcachedSoftware();
130+
131+
$servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []);
131132
if (count($servers) < 1) {
132133
$servers = [
133-
['127.0.0.1', 11211],
134+
[
135+
'host' => !empty($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1',
136+
'port' => !empty($this->config[ 'port' ]) ? $this->config[ 'port' ] : 11211,
137+
'sasl_user' => !empty($this->config[ 'sasl_user' ]) ? $this->config[ 'sasl_user' ] : false,
138+
'sasl_password' => !empty($this->config[ 'sasl_password' ]) ? $this->config[ 'sasl_password' ] : false,
139+
],
134140
];
135141
}
136142

137143
foreach ($servers as $server) {
138144
try {
139-
if (!$this->instance->addServer($server[ 0 ], $server[ 1 ])) {
145+
if (!$this->instance->addServer($server['host'], $server['port'])) {
140146
$this->fallback = true;
141147
}
142148
if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){

0 commit comments

Comments
 (0)