我在Nginx上运行LUA。我决定通过Redis获取一些变量。我用的是卢阿的桌子。是这样的;
local ip_blacklist = {
"1.1.1.1",
"1.1.1.2",
}我要在Nginx上印刷;
1.1.1.1
1.1.1.2我想把这些值保存在Redis上,而不是Lua上。我的红宝石:http://prntscr.com/10sv3ln
我的卢阿命令;
local ip = red:hmget("iplist", "ip_blacklist")我要在Nginx上印刷;
{"1.1.1.1","1.1.1.2",}它的数据不是以表的形式出现的,函数也不起作用。如何将这些数据称为本地ip_blacklist?
发布于 2021-03-23 12:23:42
https://redis.io/topics/data-types
红色散列是字符串字段和字符串值之间的映射。
不能将Lua表直接存储为散列值。据我所知,您已经使用{"1.1.1.1","1.1.1.2",}存储了一个文字字符串RedisInsight,但它不是这样工作的。
您可以使用JSON进行序列化:
server {
location / {
content_by_lua_block {
local redis = require('resty.redis')
local json = require('cjson.safe')
local red = redis:new()
red:connect('127.0.0.1', 6379)
-- set a string with a JSON array as a hash value; you can use RedisInsight for this step
red:hset('iplist', 'ip_blacklist', '["1.1.1.1", "1.1.1.2"]')
-- read a hash value as a string (a serialized JSON array)
local ip_blacklist_json = red:hget('iplist', 'ip_blacklist')
-- decode the JSON array to a Lua table
local ip_blacklist = json.decode(ip_blacklist_json)
ngx.say(type(ip_blacklist))
for _, ip in ipairs(ip_blacklist) do
ngx.say(ip)
end
}
}
}输出:
table
1.1.1.1
1.1.1.2https://stackoverflow.com/questions/66754552
复制相似问题