gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:06:19 27 4
gpt4 key购买 nike

我正在尝试生成一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com