How to atomically delete keys matching a pattern using Redis
In my Redis DB I have a number of prefix:<numeric_id>
hashes.
Sometimes I want to purge them all atomically. How do I do this without using some distributed locking mechanism?
Starting with redis 2.6.0, you can run lua scripts, which execute atomically. I have never written one, but I think it would look something like this
EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:*
See the EVAL documentation.
Execute in bash:
redis-cli KEYS "prefix:*" | xargs redis-cli DEL
UPDATE
Ok, i understood. What about this way: store current additional incremental prefix and add it to all your keys. For example:
You have values like this:
prefix_prefix_actuall = 2
prefix:2:1 = 4
prefix:2:2 = 10
When you need to purge data, you change prefix_actuall first (for example set prefix_prefix_actuall = 3), so your application will write new data to keys prefix:3:1 and prefix:3:2. Then you can safely take old values from prefix:2:1 and prefix:2:2 and purge old keys.
Here's a completely working and atomic version of a wildcard delete implemented in Lua. It'll run much faster than the xargs version due to much less network back-and-forth, and it's completely atomic, blocking any other requests against redis until it finishes. If you want to atomically delete keys on Redis 2.6.0 or greater, this is definitely the way to go:
redis-cli -n [some_db] -h [some_host_name] EVAL "return redis.call('DEL', unpack(redis.call('KEYS', ARGV[1] .. '*')))" 0 prefix:
This is a working version of @mcdizzle's idea in his answer to this question. Credit for the idea 100% goes to him.
EDIT: Per Kikito's comment below, if you have more keys to delete than free memory in your Redis server, you'll run into the "too many elements to unpack" error. In that case, do:
for _,k in ipairs(redis.call('keys', ARGV[1])) do
redis.call('del', k)
end
As Kikito suggested.
链接地址: http://www.djcxy.com/p/86414.html上一篇: C#redis vs mongodb性能
下一篇: 如何自动删除使用Redis匹配模式的密钥