lua - 如何在 Lua 脚本中使用 SMEMBERS 的结果作为 SUNION 的输入

标签 lua redis

我正在尝试生成一个 Lua 脚本,该脚本采用集合的成员(每个成员也代表一个集合)并返回并集。 这是这 3 组的具体示例:

smembers u:1:skt:n1
1) "s2"
2) "s3"
3) "s1"

smembers u:1:skt:n2
1) "s4"
2) "s5"
3) "s6"

smembers u:1:skts
1) "u:1:skt:n1"
2) "u:1:skt:n2"

所以集合u:1:skts包含其他2个集合的引用,并且我 想要生成 u:1:skt:n1u:1:skt:n2 的并集,如下所示:

1) "s1"
2) "s2"
3) "s3"
4) "s4"
5) "s5"
6) "s6"

这是我到目前为止所拥有的:

local indexes = redis.call("smembers", KEYS[1])
return redis.call("sunion", indexes)

但我收到以下错误:

(error) ERR Error running script (call to f_c4d338bdf036fbb9f77e5ea42880dc185d57ede4): 
@user_script:1: @user_script: 1: Lua redis() command arguments must be strings or integers

它似乎不喜欢将 indexes 变量作为 sunion 命令的输入。有什么想法吗?

最佳答案

请勿执行此操作,否则您将无法迁移到集群。这是来自documentation :

All Redis commands must be analyzed before execution to determine which keys the command will operate on. In order for this to be true for EVAL, keys must be passed explicitly. This is useful in many ways, but especially to make sure Redis Cluster can forward your request to the appropriate cluster node.

Note this rule is not enforced in order to provide the user with opportunities to abuse the Redis single instance configuration, at the cost of writing scripts not compatible with Redis Cluster.

如果你仍然决定违反规则,请使用Lua的unpack :

local indexes = redis.call("smembers", KEYS[1])
return redis.call("sunion", unpack(indexes))

关于lua - 如何在 Lua 脚本中使用 SMEMBERS 的结果作为 SUNION 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43234084/

相关文章:

lua - 更改lua脚本中map类型的aerospike db记录中bin的值

debugging - Lua:获取函数源

node.js - Redis需要和磁盘存储一样处理吗?

clojure - 使用 taoensso.carmine 检查是否存在多个 key

symfony - Doctrine query_cache 自动过期

compiler-construction - 是否可以用高级语言实现编译器?

lua - 如果表中有多个元素,如何在 Lua 中检查?

lua - 你可以在 lua 表的键中使用冒号吗

kubernetes - 是否可以使用 Istio 搜索网格在 Kubernetes 中创建 Redis 集群?

amazon-ec2 - Redis - 扩展不同的 ec2 区域