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] Phalcon 2.1.b: Libmemcached::queryKeys($keyPrefix) - "$keyPrefix" is ignored, all keys are returned. #11024

Closed
temuri416 opened this issue Oct 16, 2015 · 4 comments
Assignees
Labels
bug A bug report status: medium Medium
Milestone

Comments

@temuri416
Copy link
Contributor

Hi,

The code:

class Memc extends Phalcon\Cache\Backend\Libmemcached
{
    function getConn() {
        return $this->_memcache;
    }
}

 $frontCache = new \Phalcon\Cache\Frontend\Data(array(
    "lifetime" => 172800
 ));
 $cache = new Memc($frontCache, array(
     "servers" => array(
         array('host' => 'localhost',
               'port' => 11211,
               'weight' => 1),
     ),
     'statsKey' => '_PHCM',
));

$cache->save('prefix1-myKey', ['a', 'b']);
$cache->save('prefix2-myKey', ['x', 'z']);
$prefix1Keys = $cache->queryKeys('prefix1');
$_PHCM = $cache->getConn()->get('_PHCM');

I expect $prefix1Keys to only contain prefix1-myKey, however it also includes prefix2-myKey.

$_PHCM correctly lists both keys as array keys and TTL as values.

Zephir source of queryKeys() is dead simple and my $_PHCM is equivalent of keys below:

let keys = memcache->get(specialKey);
if typeof keys == "array" {
    let keys = array_keys(keys);
    for key in keys {
        if prefix && !starts_with(key, prefix) {
            unset keys[key];
        }
    }
}

No idea what may be failing there...

Thanks!

@temuri416
Copy link
Contributor Author

@andresgutierrez

I think there's an obvious bug in ZEP code around here:

https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/cache/backend/libmemcached.zep#L347

Once we flipped contents of $_PHCM array, cache keys are values of keys array. So, the for key in keys yields cached key. Thus, unset keys[key]; is wrong.

So, the ZEP code should be changed to:

let keys = memcache->get(specialKey);
if typeof keys == "array" {
    let keys = array_keys(keys);
    for idx, key in keys {
        if prefix && !starts_with(key, prefix) {
            unset keys[idx];
        }
    }
}

If you confirm it, I'll create a pull request.

Thanks!

@temuri416
Copy link
Contributor Author

@sergeyklay any chance you fix this? thanks!

@sergeyklay sergeyklay self-assigned this Dec 15, 2016
@sergeyklay sergeyklay added this to the 3.0.3 milestone Dec 15, 2016
@sergeyklay
Copy link
Contributor

I'll try to sort out

@sergeyklay
Copy link
Contributor

Fixed in the 3.0.x branch.

@sergeyklay sergeyklay assigned sergeyklay and unassigned sergeyklay Dec 24, 2016
@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

3 participants