Skip to content
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

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

Closed
Jurigag opened this issue Jul 31, 2020 · 9 comments · Fixed by #15350
Closed

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

Jurigag opened this issue Jul 31, 2020 · 9 comments · Fixed by #15350
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report

Comments

@Jurigag
Copy link
Contributor

Jurigag commented Jul 31, 2020

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

  • Phalcon version: 4.0.6
  • PHP Version: 7.2, 7.4
  • Operating System: Ubuntu 18.04
  • Installation type: Compiling from source
  • Zephir version (if any): 0.12.19
  • Server: Cli
  • Other related info (Database, table schema):
@Jurigag Jurigag added bug A bug report status: unverified Unverified labels Jul 31, 2020
@Jurigag Jurigag changed the title [BUG]: When saving empty array in cache null is returned [BUG]: When saving empty array,0,false in cache null is returned Jul 31, 2020
@Jurigag Jurigag changed the title [BUG]: When saving empty array,0,false in cache null is returned [BUG]: When saving empty array,0,'0',false in cache null is returned Jul 31, 2020
@Jurigag
Copy link
Contributor Author

Jurigag commented Jul 31, 2020

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.

@Jurigag Jurigag changed the title [BUG]: When saving empty array,0,'0',false in cache null is returned [BUG]: When saving [],0,'0',false in cache null is returned Jul 31, 2020
@Jeckerson
Copy link
Member

DId you try to specify server config?

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

@Jurigag
Copy link
Contributor Author

Jurigag commented Jul 31, 2020

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

@zak-wojtek77
Copy link

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
Copy link

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
Copy link
Member

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

@zak-wojtek77
Copy link

zak-wojtek77 commented Aug 13, 2020

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

@Jurigag
Copy link
Contributor Author

Jurigag commented Aug 14, 2020

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

@zsilbi zsilbi added 5.0 The issues we want to solve in the 5.0 release and removed status: unverified Unverified labels Sep 28, 2020
@zsilbi zsilbi added the transfer label Oct 6, 2020
@niden niden added this to Active Sprint Tasks in Phalcon Roadmap Mar 28, 2021
niden added a commit that referenced this issue Mar 28, 2021
Storage adapters returning wrong data when data is emptry array Fixes #15125
@niden
Copy link
Sponsor Member

niden commented Mar 28, 2021

Resolved in #15350

@niden niden closed this as completed Mar 28, 2021
Phalcon Roadmap automation moved this from Active Sprint Tasks to Implemented Mar 28, 2021
@niden niden moved this from Implemented to Released in Phalcon Roadmap Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report
Projects
Archived in project
Phalcon Roadmap
  
Released
Development

Successfully merging a pull request may close this issue.

6 participants