Skip to content

Memcached ignores custom host/port configurations #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zkwbbr opened this issue Feb 24, 2019 · 37 comments · Fixed by #677
Closed

Memcached ignores custom host/port configurations #675

zkwbbr opened this issue Feb 24, 2019 · 37 comments · Fixed by #677

Comments

@zkwbbr
Copy link

zkwbbr commented Feb 24, 2019

Configuration

  • PhpFastCache version: "7.0.4"
  • PhpFastCache API version: "2.0.4"
  • PHP version: "7.3.2"
  • Operating system: "Debian 9 (Docker image: php:7.3-apache-stretch)"

My question
How can I use memcached driver when connecting to a Docker memcached container ?

The following code below doesn't seem to work:

<?php

use Phpfastcache\Helper\Psr16Adapter;
use Phpfastcache\CacheManager;
use Phpfastcache\Drivers;

$ip = '192.168.2.200';
$port = 11211;

$cfg = [
    'host' => $ip,
    'port' => $port
];

$driver = CacheManager::Memcached(new Drivers\Memcached\Config($cfg));
$cache = new Psr16Adapter($driver);
$cache->set('foo', 'bar');
echo $cache->get('foo');

The above code returns:

Fatal error: Uncaught Phpfastcache\Exceptions\PhpfastcacheDriverConnectException: Memcached failed to connect with the following error message: "Memcached seems to not be connected" line 115 in /srv/app/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Memcached/Driver.php


However, when I test the same memcached connection using plain PHP, it works:

$mc = new Memcached();
$mc->addServer($ip, $port);
$mc->set('foo', 'bar');
echo $mc->get('foo');

Any ideas why?

@Geolim4
Copy link
Member

Geolim4 commented Feb 24, 2019

Hello, nope no idea.

It's up to you to find the host & port that allows memcached driver to connect to.

Cheers,
Georges

@zkwbbr
Copy link
Author

zkwbbr commented Feb 24, 2019

@Geolim4 How does PHPFastCache use Memcached, does it use Memcached? If yes, then the code above should work as there's really no big difference.

@zkwbbr
Copy link
Author

zkwbbr commented Feb 24, 2019

@Geolim4 I did some more tests and found that PHPFastCache appears to be not honoring the host and port details at all. I started a local memcached instance then it worked even after entering several fake host and port details.

That said, this issue might not be related to Docker at all but simply due to PHPFastCache ignoring user supplied host and port details. Can you kindly confirm if this is indeed the case?

@Geolim4
Copy link
Member

Geolim4 commented Feb 24, 2019

Phpfastcache does use Memcached:

protected function driverConnect(): bool

Memcacahed is also fully tested:
https://github.com/PHPSocialNetwork/phpfastcache/blob/master/tests/MemcachedAlternativeConfigurationSynax.test.php

So you've probably misconfigured the driver :/

@zkwbbr
Copy link
Author

zkwbbr commented Feb 24, 2019

Not sure if it's related but I tried that test file and replaced line 29 and 30 with:

'host' => '123',
'port' => 123,

which is an invalid host/port and it passed, is that expected?

@Geolim4
Copy link
Member

Geolim4 commented Feb 24, 2019

The host and port are the default memcached ones, but if your memcached server does not listen on that host/port combination it's not the Pfc's fault then :)

@zkwbbr
Copy link
Author

zkwbbr commented Feb 25, 2019

You mean to say it can only listen to localhost?

@zkwbbr
Copy link
Author

zkwbbr commented Feb 25, 2019

Anyway, if what you're saying is true, what do you make of my first example where PFC can't connect but using plain PHP's \Memcached(); can? They're both connecting on the same host/port.

@Geolim4
Copy link
Member

Geolim4 commented Feb 25, 2019

What can I say ? The tests passes :/
Feel free to debug this method:

protected function driverConnect(): bool

Have a look at the config file too:
https://github.com/PHPSocialNetwork/phpfastcache/blob/master/lib/Phpfastcache/Drivers/Memcached/Config.php

And tells me if something can be improved, but actually the tests passes and my local vagrant box is connecting on a custom port too

@zkwbbr
Copy link
Author

zkwbbr commented Feb 25, 2019

I'll see what I can find.

I think the custom port works fine but the issue is using a different host ip other than 127.0.0.1.

Have you tried using a random fake host ip like 1.2.3.4? If I do that it doesn't return an error and continues to work fine when it should at least return a connection error, right?

@Geolim4
Copy link
Member

Geolim4 commented Feb 28, 2019

Can you try with $mc->addServer($ip, $port); instead of setHost() ?

@zkwbbr
Copy link
Author

zkwbbr commented Mar 2, 2019

You mean in plain PHP memcached? Yes that's what I'm doing in my example, I don't think there's a setHost() method.

@Geolim4
Copy link
Member

Geolim4 commented Mar 2, 2019 via email

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

How do I do that? This is what I'm using:

$cfg = [
    'host' => $ip,
    'port' => $port
];

$driver = CacheManager::Memcached(new Drivers\Memcached\Config($cfg));
$cache = new Psr16Adapter($driver);

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Are you using an IDE or a text-plain editor ?
If so I can understand why you haven't notice those method in rivers\Memcached\Config()

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Sorry I mean setServers()

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

Wait, can you let me know how to properly use the PSR16 adapter with memcached? I feel like I'm missing something there. Here's what I'm currently doing:

$servers = [[
    'host' => $ip,
    'port' => $port
    ]];

$conf = new Drivers\Memcached\Config;
$conf->setServers($servers);
$cache = new Psr16Adapter('memcached', $conf);
$cache->set('foo', 'bar');
echo $cache->get('foo');

This returns an error:

Fatal error: Uncaught Phpfastcache\Exceptions\PhpfastcacheCoreException: The driverRead method returned an unexpected variable type: string in /srv/app/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php:93

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

The memcached backend returned a string which is not expected at all :/

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Phpfastcache stores values in a certain way and expected to retrieve them in the same way.

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

The error happens just right after doing $cache->set('foo', 'bar');
What do you make of it?

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Are you sure that you've not set data previously outside of phpfastcache api ?

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

I did, I think that solves it.
Case closed I guess. Thank you!

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

It still bugs me why setHost() and setPort does not works on you

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

I have to dig it

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

Yes, doing this doesn't work:

$conf = new Drivers\Memcached\Config;
$conf->setHost($ip);
$conf->setPort($port);

Also, my original first example post only works if localhost (127.0.0.1) memcached is active, it can't "see" a different IP.

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

I think I know why:

I use getHost() and getPort() if $servers is empty.
This cannot work since it's already pre-filled here:

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

This can be confirmed by testing that:

Replace your old code:

$conf = new Drivers\Memcached\Config;
$conf->setHost($ip);
$conf->setPort($port);

with that:

$conf = new Drivers\Memcached\Config;
$conf->serServers([]);
$conf->setHost($ip);
$conf->setPort($port);

Tell me if it's working

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

$conf = new Drivers\Memcached\Config;
$conf->setServers([]);
$conf->setHost($ip);
$conf->setPort($port);

That works!

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Mhhh okay, got it, I'll push a fix later, my tests are using setServers() so I was confused why it would not work for you. Sorry for being a little rude :)

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

No worries, thank you.

@Geolim4 Geolim4 changed the title How to use memcached driver when connecting to a Docker memcached container? Memcached ignores custom host/port configurations Mar 3, 2019
Geolim4 added a commit to Geolim4/phpfastcache that referenced this issue Mar 3, 2019
@Geolim4 Geolim4 mentioned this issue Mar 3, 2019
6 tasks
Geolim4 added a commit that referenced this issue Mar 3, 2019
@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

I'm gonna push a new release by the end of week, thanks !

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Have fun :)

https://github.com/PHPSocialNetwork/phpfastcache/releases/tag/7.0.5

@zkwbbr
Copy link
Author

zkwbbr commented Mar 3, 2019

Sorry, I tried the new update, but I'm now facing a new issue.
When using $conf->setServers($servers); alone, it errors:

Fatal error: Uncaught Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException: Missing keys for memcached server: saslPassword in /srv/app/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Memcached/Config.php:112

It wasn't like this before the update, sorry about it :)

@Geolim4
Copy link
Member

Geolim4 commented Mar 3, 2019

Yeah because these keys are still required, even if you set them to null ;)

@zkwbbr
Copy link
Author

zkwbbr commented Mar 4, 2019

Sorry but I believe they're not required by memcached?

To clarify:

This works without setting saslUser and saslPassword:

$conf->setHost($ip);
$conf->setPort($port);

But this doesn't work (as of 7.0.5):

$servers = [[
    'host' => $ip,
    'port' => $port,
    ]];
$conf->setServers($servers);

@Geolim4
Copy link
Member

Geolim4 commented Mar 4, 2019

It's not about Memcached by itself, but about the config validation of PFC.
If you just use one server use setHost() and setPort() which does not require SASL specifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants