Hello!
On Thu, Feb 5, 2015 at 7:26 AM, Howard Cai wrote:
> local res,err = red:scan(0, "test*")
> ngx.say(err)
>
> I got: ERR syntax error
>
You are using the redis "scan" command in the wrong way. Your syntax
won't even work in redis-cli either:
$ redis-cli
127.0.0.1:6379> scan 0 test*
(error) ERR syntax error
See the official redis manual for this command for the right syntax:
http://redis.io/commands/scan
To quote:
"SCAN cursor [MATCH pattern] [COUNT count]"
So the right calling syntax is
local res, err = red:scan(0, "match", "test*")
And we can also verify it easily on the redis-cli prompt:
127.0.0.1:6379> scan 0 match test*
1) "0"
2) 1) "testit"
2) "test"
3) "test2"
See? It works :)
Below is a standalone and complete example that demonstrates the usage of scan:
location = /t {
content_by_lua '
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
if not ok then
ngx.say("failed to connect: ", err)
return
end
assert(red:flushall())
assert(red:set("dog", 2))
assert(red:set("test", 5))
assert(red:set("test2", "hi"))
assert(red:set("testit", "bah"))
res, err = red:scan(0, "test*")
if not res then
ngx.say("failed to scan: ", err)
end
local cjson = require "cjson"
ngx.say("scan result: ", cjson.encode(res))
assert(red:set_keepalive(0, 100))
';
}
assuming your redis-server is listening on the local port 6379.
Accessing /t defined above gives
$ curl localhost:8080/t
scan result: ["0",["testit","test","test2"]]
Regards,
-agentzh