在我的Redis DB中,我有一些前缀:<numeric_id>哈希值。
有时我想把它们都原子地清除掉。如何在不使用分布式锁定机制的情况下做到这一点呢?
在我的Redis DB中,我有一些前缀:<numeric_id>哈希值。
有时我想把它们都原子地清除掉。如何在不使用分布式锁定机制的情况下做到这一点呢?
当前回答
如果你使用windows环境,请遵循以下步骤,它一定会工作:
Download GOW from here - https://github.com/bmatzelle/gow/wiki (because xargs command doesn't works in windows) Download redis-cli for Windows (detailed explanation is here - https://medium.com/@binary10111010/redis-cli-installation-on-windows-684fb6b6ac6b) Run cmd and open directory where redis-cli stores (example: D:\Redis\Redis-x64-3.2.100) if you want to delete all keys which start with "Global:ProviderInfo" execute this query (it's require to change bold parameters (host, port, password, key) and write yours, because of this is only example): redis-cli -h redis.test.com -p 6379 -a redispassword --raw keys "Global:ProviderInfo*" | xargs redis-cli -h redis.test.com -p 6379 -a redispassword del
其他回答
在bash执行:
redis-cli KEYS "prefix:*" | xargs redis-cli DEL
更新
好的,我明白了。这种方式怎么样:存储当前额外的增量前缀,并将其添加到所有的键。例如:
你的价值观是这样的:
prefix_prefix_actuall = 2
prefix:2:1 = 4
prefix:2:2 = 10
当您需要清除数据时,首先更改prefix_actuall(例如set prefix_prefix_actuall = 3),因此您的应用程序将把新数据写入关键字prefix:3:1和prefix:3:2。然后,您可以安全地从prefix:2:1和prefix:2:2中获取旧值并清除旧键。
Spring RedisTemplate本身提供了这个功能。RedissonClient在最新版本中已经弃用了“deletebyppattern”功能。
Set<String> keys = redisTemplate.keys("geotag|*");
redisTemplate.delete(keys);
如果你使用windows环境,请遵循以下步骤,它一定会工作:
Download GOW from here - https://github.com/bmatzelle/gow/wiki (because xargs command doesn't works in windows) Download redis-cli for Windows (detailed explanation is here - https://medium.com/@binary10111010/redis-cli-installation-on-windows-684fb6b6ac6b) Run cmd and open directory where redis-cli stores (example: D:\Redis\Redis-x64-3.2.100) if you want to delete all keys which start with "Global:ProviderInfo" execute this query (it's require to change bold parameters (host, port, password, key) and write yours, because of this is only example): redis-cli -h redis.test.com -p 6379 -a redispassword --raw keys "Global:ProviderInfo*" | xargs redis-cli -h redis.test.com -p 6379 -a redispassword del
对于那些无法解析其他答案的人:
eval "for _,k in ipairs(redis.call('keys','key:*:pattern')) do redis.call('del',k) end" 0
将key:*:pattern替换为您自己的模式,并将其输入到redis-cli中,就可以开始了。
Credit lisco来自:http://redis.io/commands/del
@mcdizle的解决方案是行不通的,它只适用于一个条目。
这一项适用于具有相同前缀的所有键
EVAL "for i, name in ipairs(redis.call('KEYS', ARGV[1])) do redis.call('DEL', name); end" 0 prefix*
注意:你应该用你的密钥前缀替换'prefix'…