phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

[BUG]: When saving [],0,'0',false in cache null is returned #15125

Closed Jurigag closed 3 years ago

Jurigag commented 4 years ago

Describe the bug When using libmemcached for cache adapter and saving empty array or false or 0 and then getting a key null is returned instead of empty array/0/false.

To Reproduce

Steps to reproduce the behavior:

use Phalcon\Cache;
use Phalcon\Cache\AdapterFactory;
use Phalcon\Storage\SerializerFactory;

$serializerFactory = new SerializerFactory();
$adapterFactory    = new AdapterFactory($serializerFactory);

$options = [
    'defaultSerializer' => 'Php',
    'lifetime'          => 7200
];

$adapter = $adapterFactory->newInstance('libmemcached', $options);

$cache = new Cache($adapter);
$cache->set('test', [], 7200);
var_dump($cache->get('test')); // returns NULL

$cache->set('test', 0, 7200);
var_dump($cache->get('test')); // returns NULL

$cache->set('test', false, 7200);
var_dump($cache->get('test')); // returns NULL

$cache->set('test', '0', 7200);
var_dump($cache->get('test')); // returns NULL

$memcached = new \Memcached();
$memcached->addServer('localhost', 11211);
$memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP);
$memcached->set('test', [], 7200);
var_dump($memcached->get('test')); // returns array(0) {}

$memcached->set('test', 0, 7200);
var_dump($memcached->get('test')); // returns int(0)

$memcached->set('test', '0', 7200);
var_dump($memcached->get('test')); // returns string(1) "0"

Expected behavior Return empty array instead of null

Details

Jurigag commented 4 years ago

As a workaround for now i just override set/get methods and saved other value for this empty values and then return correct one if someone other had similar issue.

Jeckerson commented 4 years ago

DId you try to specify server config?

$options = [
    'defaultSerializer' => 'Json',
    'lifetime'          => 7200,
    'servers'           => [
        0 => [
            'host'   => '10.4.13.100',
            'port'   => 11211,
            'weight' => 1,
        ],
    ],
];
Jurigag commented 4 years ago

Yes, when saving 1 or 2 or any other value(basically non empty value) it works correctly @Jeckerson

zak-wojtek77 commented 4 years ago

I use Phalcon 4.0.6 and also is the same problem. I use cache with file and cache with APCu. In cache file is correct write value "false" in file on the disk, error is in method "get()".

zak-wojtek77 commented 4 years ago

It is problem:

    // phalcon/Storage/Adapter/AbstractAdapter.zep
    protected function getUnserializedData(var content, var defaultValue = null) -> var
    {
        // HERE IS PROBLEM
        if !content {
            return defaultValue;
        }

        // .....
    }

https://github.com/phalcon/cphalcon/blob/master/phalcon/Storage/Adapter/AbstractAdapter.zep line 208

Jeckerson commented 4 years ago

Why save empty value cache when it can be just removed?

zak-wojtek77 commented 4 years ago

Empty value such FALSE, 0, '0', [] is normal value.

Jurigag commented 4 years ago

Exactly empty value is normal value. Honestly I am not sure if code above is issue, but will check it later.

niden commented 3 years ago

Resolved in https://github.com/phalcon/cphalcon/pull/15350