代码之家  ›  专栏  ›  技术社区  ›  guneysus

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

  •  0
  • guneysus  · 技术社区  · 5 年前

    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)

    只是想把这个简单的任务委托给Redis Lua脚本引擎。

    1 回复  |  直到 5 年前
        1
  •  2
  •   Kevin Christopher Henry    5 年前

    使用 built-in JSON library :

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

    请注意 ZRANGEBYSCORE table countryCode 为每个人。