How to call redis-cli in lua-resty-redis

154 views
Skip to first unread message

Howard Cai

unread,
Jan 30, 2015, 9:38:10 AM1/30/15
to openre...@googlegroups.com
such as: redis-cli KEYS "task:*" | xargs redis-cli DEL

I want bulk delete keys, help pls.

James Hurst

unread,
Jan 30, 2015, 12:53:27 PM1/30/15
to openre...@googlegroups.com
Hi,

You don't need to "call" redis-cli, as this is just a command line interface to the Redis API, and lua-resty-redis [1] also provides complete access to the Redis API [2]. Review the lua-resty-redis docs to see how to use the commands.

To bulk delete keys, I suggest you look at the SCAN command, which is similar to KEYS except allows you to iterate over a fixed number of keys at a time, since KEYS can be an expensive operation on large keyspaces and so should be avoided in production code.

Then, for each result in your SCAN, you simply execute a DEL on each key.


James.


On 30 January 2015 at 14:38, Howard Cai <hca...@gmail.com> wrote:
such as: redis-cli KEYS "task:*" | xargs redis-cli DEL

I want bulk delete keys, help pls.

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Howard Cai

unread,
Feb 5, 2015, 8:22:38 AM2/5/15
to openre...@googlegroups.com
I'm a new guy to lua-redis, Could you please give me an example code in lua?




在 2015年1月31日星期六 UTC+8上午1:53:27,James Hurst写道:

Howard Cai

unread,
Feb 5, 2015, 10:26:32 AM2/5/15
to openre...@googlegroups.com
local res,err = red:scan(0, "test*")
ngx.say(err)

I got: ERR syntax error 



在 2015年1月31日星期六 UTC+8上午1:53:27,James Hurst写道:
Hi,

Yichun Zhang (agentzh)

unread,
Feb 5, 2015, 3:22:23 PM2/5/15
to openresty-en
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

Yichun Zhang (agentzh)

unread,
Feb 5, 2015, 3:24:23 PM2/5/15
to openresty-en
Hello!

On Thu, Feb 5, 2015 at 12:22 PM, Yichun Zhang (agentzh) wrote:
> local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)

Sorry, this line should be like the following instead:

local ok, err = red:connect("127.0.0.1", 6379)

The "$TEST_NGINX_REDIS_PORT" thing is just my own test scaffold magic ;)

Regards,
-agentzh

Howard Cai

unread,
Feb 20, 2015, 2:01:06 AM2/20/15
to openre...@googlegroups.com
Sorry, I still got "failed to scan: ERR syntax error" by your sample code, what is your redis version?

My redis is http://download.redis.io/releases/redis-2.8.19.tar.gz.


在 2015年2月6日星期五 UTC+8上午4:22:23,agentzh写道:

Howard Cai

unread,
Feb 20, 2015, 2:03:48 AM2/20/15
to openre...@googlegroups.com
My mistake, the "red:scan(0, "match", "test*")"  is correct. thank you.


在 2015年2月6日星期五 UTC+8上午4:22:23,agentzh写道:
Hello!
Reply all
Reply to author
Forward
0 new messages