Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Memcached get returning 0 always

I have memcache (installed on php5) and memcached (installed on php7.2 via libmemcached) both connecting to the same memcached daemon/server.

Memcache::get works perfectly fine and fetches the data as per my expectation. But when I do Memcached::get, it always returns 0.

I have checked that I have compression off when using both extensions. I also tried toggling between Memcached::OPT_BINARY_PROTOCOL for memcached and it still produces same null result.

Interestingly, when I add a key/value pair using memcached extension and retrieve using the same key, I get proper/correct value that I added.

I am now clueless what could be the reason that it's not working for data already stored in memcached server.

EDIT 1 : I did telnet to my memcached server, and checked that it actually has the value. Also, I checked the result code returned by Memcached::getResultCode is not any kind of failure.

EDIT 2 : I may have narrowed it down further. I noticed that when I save ["key1" => "value1"] from memcache-php5 script, it stores and retrieves data correctly. But when I try to retrieve that same data with memcached-php7.1 script, it returns 0.

After that I removed the data with key "key1" from memcached server using telnet. And then I saved ["key1" => "value1"] using memcached-php7.1 script and it can retrieve that data correctly. But when trying to retrieve it using memcache-php5 script, it returns kind of the serialized data "a:1:{s:4:\"key1\";s:6:\"value1\";}" (this is json_encoded output)

So in order to upgrade, I may have to delete/flush everything and recreate entries in memcached server using memcached extension.

P.S. : I know the differences between both these php extensions. I have read all the comments on this question and it's not a duplicate of mine.

like image 675
Abubakkar Avatar asked Feb 20 '18 11:02

Abubakkar


1 Answers

As you already know, memcache and memcached are two different extensions. Even though, they're are used for the same purpose - connecting to a memcache server - each one of them serialize data differently.

That means you can't safely switch between them, without a proper cache flush in the server or independent server instances.

<?php
$memcache = new Memcache;
$memcacheD = new Memcached;
$memcache->addServer($host);
$memcacheD->addServers($servers);

$checks = array(
    123,
    4542.32,
    'a string',
    true,
    array(123, 'string'),
    (object)array('key1' => 'value1'),
);
foreach ($checks as $i => $value) {
    print "Checking WRITE with Memcache\n";
    $key = 'cachetest' . $i;
    $memcache->set($key, $value);
    usleep(100);
    $val = $memcache->get($key);
    $valD = $memcacheD->get($key);
    if ($val !== $valD) {
        print "Not compatible!";
        var_dump(compact('val', 'valD'));
    }

    print "Checking WRITE with MemcacheD\n";
    $key = 'cachetest' . $i;
    $memcacheD->set($key, $value);
    usleep(100);
    $val = $memcache->get($key);
    $valD = $memcacheD->get($key);
    if ($val !== $valD) {
        print "Not compatible!";
        var_dump(compact('val', 'valD'));
    }
}
like image 153
Diogo Sgrillo Avatar answered Sep 29 '22 11:09

Diogo Sgrillo