Py学习  »  Redis

如何在Redis的zrank中以相同的分数获得相同的排名?

Ankit Gajra • 5 年前 • 1159 次点击  

如果我有5个成员的分数如下

a - 1
b - 2
c - 3
d - 3
e - 5

zrank of c返回2,zrank of d返回3

有没有办法用同样的分数获得同样的排名?
示例:zrank c=2,d=2,e=3
如果是,那么如何在SpringDataRedis中实现它?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/37886
 
1159 次点击  
文章 [ 4 ]  |  最新文章 5 年前
Itamar Haber
Reply   •   1 楼
Itamar Haber    6 年前

for_stack
Reply   •   3 楼
for_stack    6 年前

  1. ZADD mem_2_score 1 a 2 b 3 c 3 d 5 e
  2. ZADD score_2_rank 1 1 2 2 3 3 5 5

  1. ZSCORE mem_2_score c 3
  2. ZRANK score_2_rank 3 2

Itamar Haber
Reply   •   4 楼
Itamar Haber    6 年前

local hscores_key = KEYS[1]
local user = ARGV[1]
local increment = ARGV[2]
local new_score = redis.call('HINCRBY', hscores_key, user, increment)

local old_score = new_score - increment
local hcounts_key = KEYS[2]
local old_count = redis.call('HINCRBY', hcounts_key, old_score, -1)
local new_count = redis.call('HINCRBY', hcounts_key, new_score, 1)

local zdranks_key = KEYS[3]
if new_count == 1 then
  redis.call('ZADD', zdranks_key, new_score, new_score)
end
if old_count == 0 then
  redis.call('ZREM', zdranks_key, old_score)
end

local hscores_key = KEYS[1]
local zdranks_key = KEYS[2]
local user = ARGV[1]

local score = redis.call('HGET', hscores_key, user)
return redis.call('ZRANK', zdranks_key, score)