zrangebyscore redis in perl

时间:2013-08-26 07:51:25

标签: perl redis

我在perl scrpit中使用Redis.pm并尝试执行下一个命令:

zrevrangebyscore <key> <highscore> 0 WITHSCORES LIMIT 0 1

在带有redis文档的设备中,我接着写下它并且工作正常

my $data = { $redis->zrevrangebyscore($rkey, $ipl, 0, 'WITHSCORES') };

但是当我尝试在perl命令中替换'limit ...'时:

my $data = { $redis->zrevrangebyscore($rkey, $ipl, 0, 'WITHSCORES','LIMIT 0 1') };

我收到了错误

[zrevrangebyscore] ERR syntax error,  at /usr/local/lib/perl5/site_perl/5.14/Redis.pm line 163
    Redis::__ANON__(undef, 'ERR syntax error') called at /usr/local/lib/perl5/site_perl/5.14/Redis.pm line 195
    Redis::wait_one_response('Redis=HASH(0x801075300)') called at /usr/local/lib/perl5/site_perl/5.14/Redis.pm line 183
    Redis::wait_all_responses('Redis=HASH(0x801075300)') called at /usr/local/lib/perl5/site_perl/5.14/Redis.pm line 172

如何在Redis.pm中传递给arg'LIMIT 0 1'?

2 个答案:

答案 0 :(得分:0)

如果您只是想要遍历已排序的集合,并始终获取最高条目,请使用

zrange

zrange documentation

而不是zrevrangebyscore。

my $start = -1; #-1 is last element = the element with the highest score
my $stop = -1;
while (my $data = $redis->zrange($rkey, $start--, $stop--, 'WITHSCORES')) {
   #fetch the ultimate element, then the penultimate, etc....
};

答案 1 :(得分:0)

答案是:

my $data = { $redis->zrevrangebyscore($rkey, $ipl, 0, 'WITHSCORES', qw{LIMIT 0 1})};

可能对某人有用。谢谢!