php 如何使用php redis获取redis中的所有键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21719590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How get all keys in redis using php redis?
提问by open source guy
I am using https://github.com/nicolasff/phpredisextension to access redis. I want to get all keys in redis from php code. I tried following code
我正在使用https://github.com/nicolasff/phpredis扩展来访问 redis。我想从 php 代码中获取 redis 中的所有键。我试过以下代码
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$allKeys = $redis->keys('*');
print_r($allKeys); // nothing here
But following command in shell giving results
但是在shell中遵循命令给出结果
127.0.0.1:6379> KEYS *
"kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1
cb1d6g3d3bvqetjfmkmaurmpp3"
I am able to set key and data in following manner from php script
我可以从 php 脚本以下列方式设置密钥和数据
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set(session_id(), json_encode(array('uname'=>'messi fan')));
How get KEYS *from redis using phpredis ?
如何KEYS *使用 phpredis 从 redis 获取?
回答by Agis
There is nothing wrong with your code. You are doing it correctly: $redis->keys('*')retrieves all the keys.
您的代码没有任何问题。您做得对:$redis->keys('*')检索所有密钥。
The result:
结果:
"kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1
cb1d6g3d3bvqetjfmkmaurmpp3"
is in fact the key that you set when you did:
实际上是您在执行时设置的键:
$redis->set(session_id(), json_encode(array('uname'=>'messi fan')));
So session_id()returned the value:
所以session_id()返回值:
kq92p7b5tf63tmk12v54373e03 hs7ep4lc2m6ci5kk5dosgpelg4
pt7lfejenqbmmovjpmp9aojuf0 2p05gf20or6r5ee5i7sts90kn1
cb1d6g3d3bvqetjfmkmaurmpp3
therefore this became the name of the key that you set.
因此这成为您设置的密钥的名称。
回答by Siva
$redis = new Redis();
$redis->connect('xxxxxx', 6379); // use your Host from Redis desktop Manager - connection
$redis->auth('xxxxxx'); // use your Auth from Redis desktop Manager - connection
$allKeys = $redis->keys('*');
print_r($allKeys); // nothing here
回答by Jignesh Patel
Try this
尝试这个
$redis->get('key');

