Onu*_*uru 8 sortedset batch-updates redis jedis
在REDIS中增加中等大小排序集的最佳方法是什么?(最好使用java驱动程序JEDIS)Set中有大约100-200K条记录.我想用给定的双数增加他们的分数.
1 a
2 b
3 c
Run Code Online (Sandbox Code Playgroud)
2 a
3 b
4 c
Run Code Online (Sandbox Code Playgroud)
我想出的唯一可能的解决方案是:
还有其他/更好的方法吗?
以下是如何在REDIS中使用EVAL和Lua来执行for循环以递增所有已排序的集合成员.
local members = redis.call('zrange',KEYS[1],0,-1)
for i, member in ipairs(members) do
redis.call('zincrby',KEYS[1],inc,member)
end
Run Code Online (Sandbox Code Playgroud)
将其保存为字符串并使用您的驱动程序运行eval(在本例中为java).执行没有返回.
使用Jedis
// script is the script string
// 1 is the number of keys- keep it this way for this script
// myzset is the name of your sorted set
// 1.5 is the increment, you can use +/- values to inc/dec.
jedis.eval(script, 1, "myzset", "1.5");
Run Code Online (Sandbox Code Playgroud)
客户端和redis之间的通信可能会花费很多时间。为了避免这种情况,您可以获取排序集的“SET 类型”副本。例如,假设您有一个排序集“key1”:
1 a
2 b
3 c
Run Code Online (Sandbox Code Playgroud)
你有一组“key2”:
a, b, c
Run Code Online (Sandbox Code Playgroud)
您可以轻松地实现增量:
def increase_sorted_set(increment = 1)
redis.ZINTERSTORE("key1", 2, "key1", "key2", "WEIGHTS", "1", increment)
end
Run Code Online (Sandbox Code Playgroud)
Redis 将为(未排序)集合的每个成员提供key2
默认分数1
。
例如:
redis 127.0.0.1:6379> ZADD key1 1 a 2 b 3 c
(integer) 3
redis 127.0.0.1:6379> SADD key2 a b c
(integer) 3
redis 127.0.0.1:6379> ZINTERSTORE key1 2 key1 key2 WEIGHTS 1 7
(integer) 3
redis 127.0.0.1:6379> ZRANGE key1 0 -1 WITHSCORES
1) "a"
2) "8"
3) "b"
4) "9"
5) "c"
6) "10"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3063 次 |
最近记录: |