递归与Lua在Redis中

huangapple go评论55阅读模式
英文:

Recursion with Lua in Redis

问题

我阅读了关于在Redis中使用Lua进行脚本编写的文档,但没有找到任何关于我接下来提出的问题是否违规或者是否跨越了任何暗示的界限的信息或建议。因此,我在Stack Overflow上提出这个问题,以确认。

我有一个Redis脚本,伪代码如下:

EVAL "return redis.call('EVALSHA', ARGV[1], 1, redis.call('GET', KEYS[1]), ARGV[2])" 1 foo c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f bar

c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f脚本的伪代码如下:

return redis.call('SET', KEYS[1], ARGV[1])

问题是,我的脚本是否违反了编程键生成的规则?据我所知,没有,因为第一个脚本实际上调用了与其一起调用的正确键,而第二个脚本为其调用的键进行了设置操作。在集群和非集群环境中运行时,其中键可以分布在不同节点上,这种方法是否会导致任何问题?或者这种方法有什么问题吗?

英文:

I read the documentation on scripting in lua for redis and I didnot find any line or recommendation on what i am proposing next being evil or if I am crossing any implied line. So asking this on S.O to confirm..

I have redis script, pseudo like follows

EVAL "return redis.call('EVALSHA',ARGV[1],1, redis.call('GET', KEYS[1]),ARGV[2])" 1 foo c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f bar

and pseudo for c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f script is

return redis.call('SET', KEYS[1], ARGV[1])

Question is did my script voilate any rules of programmatic key generation? as far as i can think no cause first script actually made a call to the correct key it was invoked with while the second script made the set call for the keys it was invoked with will this cause any issues running on cluster and non-cluster environments where keys can be spread across nodes? or anything wrong in this approach?

答案1

得分: 1

首先,EVALSHA 不允许在 Lua 脚本中使用,除非你启用了调试模式。

在 Lua 脚本中运行 EVALSHA 是危险的,因为你可能会陷入无限递归。比如,有人使用你的脚本调用 SCRIPT LOAD,然后将返回的 evalsha 值传递给脚本。糟糕!

此外,在你的情况下,内部脚本中的键,即 return redis.call('SET', KEYS[1], ARGV[1]),是通过 GET 调用动态生成的。这个键可能位于外部脚本键的不同哈希槽中,如果在 Redis 集群中运行脚本,这将会成为一个问题。

英文:

First of all, EVALSHA is NOT allowed in Lua script, unless you enabled debugging mode.

Run EVALSHA in Lua script is dangerous, since you might run into infinite recursion. Say, someone calls SCRIPT LOAD with your script, and pass the returned evalsha value to the script. Oops!

Also in your case, the key in the inner script, i.e. return redis.call('SET', KEYS[1], ARGV[1]), is dynamically generated with a GET call. This key might be in a different hash slot of the outer script key, and it will be a problem if you run the script in Redis Cluster.

huangapple
  • 本文由 发表于 2023年2月26日 19:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定