aliyun 的 redis lua 的随机值不随机!?
def get_cookie(hkey: str) -> str | None:
from core.redis.models import client
# Use Lua scripting to get a random field from the hash set
lua_script = """
local fields = redis.call('HKEYS', KEYS[1])
if #fields == 0 then
return nil
end
local random_index = math.random(1, #fields)
local field = fields[random_index]
return random_index
"""
return client.eval(lua_script, 1, hkey)
同样的函数,在 redis 官方的 7.2.1 下面是每次都可以取到随机的 cookie
但是在 aliyun 的 redis 下面,每次取到的值都是一样的
测试了一下,去 aliyun 开了一个 7.0 的 redis,就是随机的
至于 redis 官方的 5.0 是不是随机的,还得测试一下
更新
redis5.x 不支持随机
redis7.x 支持随机
redis6.x 没有测试