Is there any way to pop all list items from redis

2019-05-29 04:57发布

I want to pop all list items from redis list at once.

I don't want to call lpop or rpop method by when the list is empty because it seems to be inefficient sending requests multi-time to redis-server.

I also know that I can get all lists with lrange method but not popping items.

Could you suggest me?

I just want to pop and get items in a list by one request to redis-server.

2条回答
霸刀☆藐视天下
2楼-- · 2019-05-29 05:38

Redis doesn't have an POPALL command, but with an embedded Lua script you can easily do that like this for example:

local reply = redis.call('LRANGE', KEYS[1], 0, -1)
redis.call('DEL', KEYS[1])
return reply
查看更多
smile是对你的礼貌
3楼-- · 2019-05-29 05:46

As Itamar Haber said, use lrange and del. In pipe mode it will be done as a single command.

LRANGE key 0 -1
DEL key
查看更多
登录 后发表回答