如何从 Redis Lua 脚本中的表值中检索值

我正试图从 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 编码字符串,将其解码为数据。 只是尝试将这个简单的任务委托给 Redis Lua 脚本引擎。

点赞
用户2395796
用户2395796

使用内置的JSON库

eval 'local  r = redis.call("ZRANGEBYSCORE", "iprange:locations", 34625535, "+inf", "LIMIT", 0, 1);
      return cjson.decode(r[1])["countryCode"];'

注意,ZRANGEBYSCORE会返回由table表示的结果数组。你可能需要循环遍历结果并提取每个的countryCode

2019-04-06 10:28:43