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

标签 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 解码为数据来处理这个 json 编码的字符串。 只是试图将这个简单的任务委托(delegate)给 Redis Lua 脚本引擎。

最佳答案

使用 built-in JSON library :

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/

相关文章:

java - 从 CLI 解锁 Redis 锁

android - 在 Corona 中使用类传递数组对象

python - Redis 管道和 python 多处理

django - 如何在 Django 应用程序中接收 RabbitMQ 消息? ( celery ?)

node.js - 使用 Node.js、Redis 和 CouchDB 管理 future 事件通知

docker - 无法从 docker 实异常(exception)部连接到 windows 上的 docker redis

python - Python 中的协程与 Lua 中的协程相比如何?

c++ - 使用 C API 覆盖 Lua 中的赋值运算符

opengl - 无法让预制作工作 - 过度演示

nginx - lua-resty-redis set_keepalive 推荐设置