Last active
January 22, 2018 15:24
-
-
Save Geolim4/fdcb754755e0c3d2b919adeb78f44e53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: src/phpFastCache/Drivers/Memcache/Driver.php | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/phpFastCache/Drivers/Memcache/Driver.php (revision cd3bf582ea58c90e9610a92ed85dd32b0f455301) | |
+++ src/phpFastCache/Drivers/Memcache/Driver.php (date 1516568443195) | |
@@ -143,6 +143,7 @@ | |
$servers = [ | |
[ | |
'host' => '127.0.0.1', | |
+ 'path' => false, | |
'port' => 11211, | |
'sasl_user' => false, | |
'sasl_password' => false, | |
@@ -152,9 +153,15 @@ | |
foreach ($servers as $server) { | |
try { | |
- if (!$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { | |
+ /** | |
+ * If path is provided we consider it as an UNIX Socket | |
+ */ | |
+ if(!empty($server[ 'path' ]) && !$this->instance->addServer($server[ 'path' ], 0)){ | |
+ $this->fallback = true; | |
+ }else if (!empty($server[ 'host' ]) && !$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { | |
$this->fallback = true; | |
} | |
+ | |
if (!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password' ])) { | |
$this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password' ]); | |
} | |
Index: src/phpFastCache/Drivers/Memcached/Driver.php | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/phpFastCache/Drivers/Memcached/Driver.php (revision cd3bf582ea58c90e9610a92ed85dd32b0f455301) | |
+++ src/phpFastCache/Drivers/Memcached/Driver.php (date 1516568397313) | |
@@ -137,6 +137,7 @@ | |
$servers = [ | |
[ | |
'host' => '127.0.0.1', | |
+ 'path' => false, | |
'port' => 11211, | |
'sasl_user' => false, | |
'sasl_password' => false, | |
@@ -146,9 +147,15 @@ | |
foreach ($servers as $server) { | |
try { | |
- if (!$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { | |
+ /** | |
+ * If path is provided we consider it as an UNIX Socket | |
+ */ | |
+ if(!empty($server[ 'path' ]) && !$this->instance->addServer($server[ 'path' ], 0)){ | |
+ $this->fallback = true; | |
+ }else if (!empty($server[ 'host' ]) && !$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { | |
$this->fallback = true; | |
} | |
+ | |
if (!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password' ])) { | |
$this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password' ]); | |
} | |
Index: src/phpFastCache/Drivers/Predis/Driver.php | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/phpFastCache/Drivers/Predis/Driver.php (revision cd3bf582ea58c90e9610a92ed85dd32b0f455301) | |
+++ src/phpFastCache/Drivers/Predis/Driver.php (date 1516568076812) | |
@@ -133,19 +133,34 @@ | |
*/ | |
protected function driverConnect() | |
{ | |
- $config = isset($this->config[ 'predis' ]) ? $this->config[ 'predis' ] : []; | |
+ /** Backward compatibility */ | |
+ $config = isset($this->config[ 'predis' ]) ? $this->config[ 'predis' ] : $this->config; | |
+ $path = isset($config[ 'path' ]) ? (string) $config[ 'path' ] : false; | |
- $this->instance = new PredisClient(array_merge([ | |
+ $defaultConfig = [ | |
'host' => '127.0.0.1', | |
'port' => 6379, | |
'password' => null, | |
'database' => null, | |
- ], $config)); | |
+ ]; | |
+ $config = array_merge($defaultConfig, $config); | |
+ | |
+ /** | |
+ * If path is provided we consider it as an UNIX Socket | |
+ */ | |
+ if($path){ | |
+ $this->instance = new PredisClient([ | |
+ 'scheme' => 'unix', | |
+ 'path' => $path | |
+ ]); | |
+ }else{ | |
+ $this->instance = new PredisClient($config); | |
+ } | |
try { | |
$this->instance->connect(); | |
} catch (PredisConnectionException $e) { | |
- throw new phpFastCacheDriverException('Failed to connect to predis server', 0, $e); | |
+ throw new phpFastCacheDriverException('Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', 0, $e); | |
} | |
return true; | |
Index: src/phpFastCache/Drivers/Redis/Driver.php | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/phpFastCache/Drivers/Redis/Driver.php (revision cd3bf582ea58c90e9610a92ed85dd32b0f455301) | |
+++ src/phpFastCache/Drivers/Redis/Driver.php (date 1516568072685) | |
@@ -135,23 +135,32 @@ | |
$this->instance = $this->instance ?: new RedisClient(); | |
$host = isset($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1'; | |
+ $path = isset($this->config[ 'path' ]) ? (string) $this->config[ 'path' ] : false; | |
$port = isset($this->config[ 'port' ]) ? (int)$this->config[ 'port' ] : '6379'; | |
$password = isset($this->config[ 'password' ]) ? $this->config[ 'password' ] : ''; | |
- $database = isset($this->config[ 'database' ]) ? $this->config[ 'database' ] : ''; | |
+ $database = isset($this->config[ 'database' ]) ? $this->config[ 'database' ] : false; | |
$timeout = isset($this->config[ 'timeout' ]) ? $this->config[ 'timeout' ] : ''; | |
- if (!$this->instance->connect($host, (int)$port, (int)$timeout)) { | |
- return false; | |
- } else { | |
+ /** | |
+ * If path is provided we consider it as an UNIX Socket | |
+ */ | |
+ if($path){ | |
+ $isConnected = $this->instance->connect($path); | |
+ }else{ | |
+ $isConnected = $this->instance->connect($host, (int)$port, (int)$timeout); | |
+ } | |
+ | |
+ if (!$isConnected && $path) { | |
+ return false; | |
+ } else if(!$path) { | |
if ($password && !$this->instance->auth($password)) { | |
return false; | |
} | |
- if ($database) { | |
- $this->instance->select((int)$database); | |
- } | |
- | |
- return true; | |
- } | |
+ } | |
+ if ($database !== false) { | |
+ $this->instance->select((int)$database); | |
+ } | |
+ return true; | |
} | |
} | |
No errors for memcached driver but cache saving don't work. I tried with my host example
$m->addServer('//var/run/memcached/memcached.sock', 0);
if(!$m->get('inter')){
$m->set('inter', time(),15);
}
else
{
echo $m->get('inter');
}
and the cache works.
With phpfastcache example
$InstanceCache = CacheManager::getInstance('memcached', [ 'path' => '/var/run/memcached/memcached.sock']);
$key = "product_page";
$CachedString = $InstanceCache->getItem($key);
if (is_null($CachedString->get())) {
$CachedString->set("Memcached Cache --> Cache Enabled --> Well done !")->expiresAfter(600);
$InstanceCache->save($CachedString);
echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // ";
echo $CachedString->get();
} else {
echo "READ FROM CACHE // ";
echo $CachedString->get();
}
echo '<br /><br /><a href="/">Back to index</a> -- <a href="./' . basename(__FILE__) . '">Reload</a>';
no cache, i always have "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE //"
Come back discussing onto the issue please :)
I made also a Pull request to fix memcached: PHPSocialNetwork/phpfastcache#566
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try Predis too please :)