Hello!
On Tue, Sep 4, 2012 at 10:30 PM, 刘建辉 wrote:
> 我在试用过你的HttpRedis2Module模块
> 然后用LuaRedisParser进行解析返回数据
> location /get_redis {
> set_unescape_uri $key $arg_key; # this requires ngx_set_misc
> redis2_query hget cate $key;
> redis2_pass 192.168.0.5:6379;
> }
> location /cate_json {
> default_type 'text/plain';
> content_by_lua '
> local json = require "cjson"
> local parser = require "redis.parser"
> local name = ngx.var.arg_name
> if name == "getcate" then
>
> local res = ngx.location.capture("/get_redis",
> {args = { key = ngx.var.arg_key } }
> )
> if res.status == 200 then
> reply = parser.parse_reply(res.body)
> ngx.say(reply)
> end
> end
> ';
>
> }
ngx_redis2 模块总是返回原始的 Redis 响应,所以像 flushall 这样的命令会返回 "+OK\r\n" 而不是
"OK". 这一点在官方文档中的 Description 一节里面有明确说明:
http://wiki.nginx.org/HttpRedis2Module#Description
引用如下:
"This module returns the raw TCP response from the Redis server. It's
recommended to use my LuaRedisParser (written in pure C) to parse
these responses into lua data structure when combined with
HttpLuaModule. "
使用 Lua 与 ngx_redis2 进行交互,需要借助于 lua-redis-parser 这样的库对原始的 Redis
响应进行解析,从而得到对应的 Lua 数据结构。相关细节可以参见 ngx_redis2 模块官方文档中的 Lua
Interoperability 一节:
http://wiki.nginx.org/HttpRedis2Module#Lua_Interoperability
不过,值得指出的是,如果你已经使用 Lua 来访问 redis 的话,推荐直接使用全新的 lua-resty-redis
库,因为用法更加简单灵活,同时性能也可能会更高一些:
https://github.com/agentzh/lua-resty-redis#name
另外,我同时把这封邮件抄送给了 OpenResty
中文邮件列表:https://groups.google.com/group/openresty
也欢迎你加入此列表并在那里和我们讨论这些问题 :)
Best regards,
-agentzh