在我的Redis DB中,我有一些前缀:<numeric_id>哈希值。

有时我想把它们都原子地清除掉。如何在不使用分布式锁定机制的情况下做到这一点呢?


当前回答

我用EVAL命令的最简单的变体继承了这一点:

EVAL "return redis.call('del', unpack(redis.call('keys', 'my_pattern_here*')))" 0

我用我的值替换了my_pattern_here。

其他回答

下面的命令对我很有用。

redis-cli -h redis_host_url KEYS "*abcd*" | xargs redis-cli -h redis_host_url 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

我用EVAL命令的最简单的变体继承了这一点:

EVAL "return redis.call('del', unpack(redis.call('keys', 'my_pattern_here*')))" 0

我用我的值替换了my_pattern_here。

现在,您可以使用redis客户端并执行第一次扫描(支持模式匹配),然后单独DEL每个键。

然而,在官方redis github上有一个问题,在这里创建一个模式匹配-del,如果你发现它有用,就去展示它吧!

如果你使用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