我正在尝试从从 Redis 中的排序集返回的 json 编码字符串中提取值。
127.0.0.1:6379> eval 'local r= redis.call("ZRANGEBYSCORE", "iprange:locations", 34625535, "+inf", "LIMIT", 0, 1); return type(r);' 0
"table"
127.0.0.1:6379> eval 'local r= redis.call("ZRANGEBYSCORE", "iprange:locations", 34625535, "+inf", "LIMIT", 0, 1); return r;' 0
1) "{\"countryCode\": \"IT\", \"countryName\": \"Italy\"}"
我只想从结果中提取countryValue
。
试过 return r.countryCode;
, return r["countryCode"];
但都返回了 (nil)
顺便说一下,我已经在我的应用程序中通过将这个 json 解码为数据来处理这个 json 编码的字符串。 只是试图将这个简单的任务委托(delegate)给 Redis Lua 脚本引擎。
最佳答案
eval 'local r = redis.call("ZRANGEBYSCORE", "iprange:locations", 34625535, "+inf", "LIMIT", 0, 1);
return cjson.decode(r[1])["countryCode"];'
请注意,ZRANGEBYSCORE
返回结果数组,在 Lua 中用 table
表示。大概您会希望遍历结果并为每个结果提取 countryCode
。
关于redis - 如何从表值 Redis Lua 脚本中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548031/