Skip to content

Instantly share code, notes, and snippets.

@Geolim4
Last active January 22, 2018 15:24
Show Gist options
  • Save Geolim4/fdcb754755e0c3d2b919adeb78f44e53 to your computer and use it in GitHub Desktop.
Save Geolim4/fdcb754755e0c3d2b919adeb78f44e53 to your computer and use it in GitHub Desktop.
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;
}
}
@pioc92
Copy link

pioc92 commented Jan 21, 2018

Hey, there is just an error at line 40, not } needed.
Sorry i don't know github to correct it myself.
I tried it, it's ok for me.
Thank you!

@Geolim4
Copy link
Author

Geolim4 commented Jan 21, 2018

If I remove that bracket, it throw a syntax error, are you sure ?

@Geolim4
Copy link
Author

Geolim4 commented Jan 21, 2018

Did you applied the patch using git ?

@Geolim4
Copy link
Author

Geolim4 commented Jan 21, 2018

I updated the patch tell me if it's working.
Just provide the "path" option, I removed the "UnixSocket" option.
I the path is provided the code now considers it as an Unix Socket.

@pioc92
Copy link

pioc92 commented Jan 22, 2018

I tried the redis driver and i think there is a mistake
$path = isset($this->config[ 'path' ]) ? (int)$this->config[ 'path' ] : false;

i think it's better to do $path = isset($this->config[ 'path' ]) ? $this->config[ 'path' ] : false; to keep the config['path'] string socket.

And after, if you give 'path' name, how to do a fallback with 'Files' Driver like

$InstanceCache = CacheManager::getInstance('redis', [
  'path' => '/var/run/redis/redis.sock',
  'fallback' => 'Files',
  'path' => dirname(__FILE__) . '/cache2/annuel'
]);

Sorry but i don't use git at all at this time but i will do it for sure ;-)

@Geolim4
Copy link
Author

Geolim4 commented Jan 22, 2018

You right I wrongly type-casted the $path config.
The Fallback syntax is correct, but it's not always a "safe" value since Redis can throw different type of exceptions and only the connection exception will be catched.

@Geolim4
Copy link
Author

Geolim4 commented Jan 22, 2018

However be carefull that you wrongly declared the $path config twice !!
A fallback driver has no (for now) configuration possible, it use the default one.

@pioc92
Copy link

pioc92 commented Jan 22, 2018

I confirm your redis patch is good with the good type-casted $path config.
I will try your memcached driver now with my host.
thanks.

@Geolim4
Copy link
Author

Geolim4 commented Jan 22, 2018

Try Predis too please :)

@pioc92
Copy link

pioc92 commented Jan 22, 2018

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>&nbsp;--&nbsp;<a href="./' . basename(__FILE__) . '">Reload</a>';

no cache, i always have "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE //"

@Geolim4
Copy link
Author

Geolim4 commented Jan 22, 2018

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