REDIS。是否可以创建 insertion-ordered list-set?
REDIS. Is it possible to create insertion-ordered list-set?
是否可以在 redis 中创建 insertion-ordered list-set?
示例:
填充数据:
push("listname", "x")
push("listname", "y")
push("listname", "x")
期望的结果:
get_list("listname") => ["x", "y"]
我们需要在分布式系统中做,所以有序集(ZADD)不适用,因为我们不能保证分数顺序。
如何结合使用 SET 和 LIST 数据结构LUA 脚本?
EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 listname setname value
示例:
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set x
(integer) 1
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set y
(integer) 2
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set x
(integer) 0
> LRANGE list 0 -1
1) "x"
2) "y"
是否可以在 redis 中创建 insertion-ordered list-set?
示例:
填充数据:
push("listname", "x")
push("listname", "y")
push("listname", "x")
期望的结果:
get_list("listname") => ["x", "y"]
我们需要在分布式系统中做,所以有序集(ZADD)不适用,因为我们不能保证分数顺序。
如何结合使用 SET 和 LIST 数据结构LUA 脚本?
EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 listname setname value
示例:
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set x
(integer) 1
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set y
(integer) 2
> EVAL "if redis.call('SADD', KEYS[2], ARGV[1]) == 1 then return redis.call('RPUSH', KEYS[1], ARGV[1]) else return 0 end" 2 list set x
(integer) 0
> LRANGE list 0 -1
1) "x"
2) "y"