Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions examples/memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down
13 changes: 10 additions & 3 deletions examples/memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down
16 changes: 11 additions & 5 deletions src/phpFastCache/Drivers/Memcache/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +25,7 @@
/**
* Class Driver
* @package phpFastCache\Drivers
* @property MemcacheSoftware $instance
*/
class Driver extends DriverAbstract
{
Expand All @@ -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) {
Expand Down Expand Up @@ -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' => !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,
],
];
}

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'])){
Expand Down
16 changes: 11 additions & 5 deletions src/phpFastCache/Drivers/Memcached/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +25,7 @@
/**
* Class Driver
* @package phpFastCache\Drivers
* @property MemcachedSoftware $instance
*/
class Driver extends DriverAbstract
{
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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' => !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,
],
];
}

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'])){
Expand Down