RobiNN1/phpCacheAdmin

Memcached stored as orgin type.

kcsso opened this issue · 2 comments

kcsso commented

phpCacheAdmin Version: v.1.1.0
Cache: Memcahced 1.6.16

PHP Version: PHP 7.4.30 (cli)

Operating System:
CentOS Linux release 7.9.2009 (Core)

Describe the issue

I found the Remark "In Memcache(d) everything is stored as a string." and fixed as string type in the codes.
but when I try below code in php and try to view the keys' value, its show fatal error.

--test.php --
$mcd = new Memcached;
$mcd->addServer('127.0.0.1',11211);
$checks = array(
     123,
     4542.32,
     'a string',
    true,
     array(123, 'string'),
    (object)array('key1' => 'value1'),
 );
 foreach ($checks as $i => $value) {
     $mcd->set("cachetest$i", $value);
 }

I changed some of the code to make sure it was really stored as a string.

    private function getAllKeys($memcached): array {
        static $keys = [];

        foreach ($memcached->getKeys() as $key_data) {
            $keys[] = [
                'key'  => $key_data['key'],
                'ttl'  => $key_data['exp'],
                //'type' => 'string', // In Memcache(d) everything is stored as a string.
                'type' => gettype($memcached->get($key_data['key'])),
            ];
        }

        return $keys;
    }

it appears that it saves the original type.
the result img is https://i.imgur.com/vYFDDi3.png

so when I try to view the value of the "string" types key in the result image, show below error.

/phpCacheAdmin/config.dist.php (Line: 69)
gzuncompress(): data error

and try to view not "string" types key, show below errors

PHP message: PHP Fatal error:  
Uncaught TypeError: Argument 1 passed to RobiNN\Pca\Value::format() must be of the type string, int given, 
called in /phpCacheAdmin/src/Dashboards/Memcached/MemcachedTrait.php on line 197
and defined in /phpCacheAdmin/src/Value.php:25

...
Uncaught TypeError: Argument 1 passed to RobiNN\Pca\Value::format() must be of the type string, object given
...

Did I use memcached to store data(in test.php) in the wrong way?

Yes, you are right, but it is because of the php extension.

$memcached->set('idk', ['idk' => 'a', '222', 111]);

var_dump($memcached->get('idk'));
// array (size=3)
//   'idk' => string 'a' (length=1)
//   0 => string '222' (length=3)
//   1 => int 111

But if you run the command directly via telnet or in my case via a custom runCommand() which does the same thing but in PHP

var_dump($memcached->runCommand('get idk'));
// array (size=2)
//   0 => string 'VALUE idk 1 48' (length=14)
//   1 => string 'a:3:{s:3:"idk";s:1:"a";i:0;s:3:"222";i:1;i:111;}' (length=48)

It serializes an array|object, so it's always a string.

Originally I used gettype() function to determine data type but if you store bool $memcached->set('key', true);, Memcached it store as a string '1'. (Redis does the same.)

The fix would be to serialize the data again or directly encode it to JSON. And convert everything that it returns to a string.

Thanks to this dashboard I hate Memcached more and more because of these inconsistent things. lol

If you have any ideas let me know.

I used runCommand() so i don't have to do anything extra. Should be fixed now.