vlambda博客
学习文章列表

Lua和Redis返回值映射

最近在撸gateway的代码,其中用到了redis做缓存,有个地方需要把lua脚本的返回值去适配一个redis的返回值后再给调用方。返回值适配网上很少例子,这里给一个记录一下。


从官网上找到说明:

Redis to Lua conversion table.

  • Redis integer reply -> Lua number

  • Redis bulk reply -> Lua string

  • Redis multi bulk reply -> Lua table (may have other Redis data types nested)

  • Redis status reply -> Lua table with a single ok field containing the status

  • Redis error reply -> Lua table with a single err field containing the error

  • Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type

Lua to Redis conversion table.

  • Lua number -> Redis integer reply (the number is converted into an integer)

  • Lua string -> Redis bulk reply

  • Lua table (array) -> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)

  • Lua table with a single ok field -> Redis status reply

  • Lua table with a single err field -> Redis error reply

  • Lua boolean false -> Redis Nil bulk reply.


status reply适配片段:

    local reply = {}

    reply['ok'] = 'OK'

    return reply