4

如文档http://redis.io/commands/sort

SORT mylist BY weight_* 

我想做的是

SORT mylist BY (weight_* + vote_*)

仅通过 Redis 就可以做到这一点吗?

4

1 回答 1

1

您可以使用 Lua 构建每个sum_*键并按这些键排序:

redis 127.0.0.1:6379> sadd myset 1 2 3
(integer) 0
redis 127.0.0.1:6379> mset weight_1 1 weight_2 2 weight_3 3
OK
redis 127.0.0.1:6379> mset vote_1 1 vote_2 2 vote_3 0
OK
redis 127.0.0.1:6379> eval "for i in ipairs(redis.call('smembers', KEYS[1])) do redis.call('set', 'sum_' .. i, redis.call('get','weight_' .. i) + redis.call('get', 'vote_' .. i)) end;" 1 myset
(nil)
redis 127.0.0.1:6379> sort myset by sum_*
1) "1"
2) "3"
3) "2"
于 2013-11-05T17:30:47.743 回答