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

keys() returning keys with prefix #16

Closed
tijmenb opened this issue May 10, 2011 · 2 comments
Closed

keys() returning keys with prefix #16

tijmenb opened this issue May 10, 2011 · 2 comments

Comments

@tijmenb
Copy link

tijmenb commented May 10, 2011

I don't know if this is intended behavior, but it got me puzzled for a while:

$redis = new Redis();
$redis->connect('127.0.0.1');
$redis->setOption(Redis::OPT_PREFIX, 'somefix:');

$redis->set('foo', 'bar');

foreach ($redis->keys("*") as $key)
{
    echo $redis->get($key); // won't work, as it's trying to get somefix:somefix:foo

    $key = str_replace('somefix:', '', $key);
    echo $redis->get($key); // will work, returns bar.
}
@nicolasff
Copy link
Member

Hello,

This is on purpose, you can use "prefix:_" if you want to select those. The thing is, if you have 2 apps talking to Redis and 1 sets the key "users" whilst the other one uses "prefix:users", running keys('_') will match both. What would it mean to get "users" twice in your return array?

So either prefix your pattern in your call to keys("*") or strip them afterwards. In any case, calling this method is usually not recommended, as it has to go through all of your keys, whatever the pattern may be.

@tijmenb
Copy link
Author

tijmenb commented May 11, 2011

I used the prefix function to separate testing data from other data, and I expected keys() to return the keys without the prefix, so I could easily call del() on them after a test. But on second look it seems better to use select() and flushdb() for that.

Thanks for your reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants