如何在Redis中批量删除数十万个带有特殊字符的键 [英] How to bulk delete hundreds of thousands of keys with special characters in Redis
问题描述
我们有一个包含数十万个 Redis 键的列表,其中包含各种特殊字符,我们想批量删除它们.关于这个问题的类似问题有一些很好的答案:如何使用 Redis 以原子方式删除与模式匹配的键
We have a list of hundreds of thousands of Redis keys containing all sorts of special characters and we would like to bulk delete them. There are some great answers to a similar problem on this question: How to atomically delete keys matching a pattern using Redis
但是,我似乎无法找到以下情况的答案:
HOWEVER, I can't seem to find an answer for the case where:
- 我们有大量的密钥(数十万)
- 键有各种特殊字符,如双引号 (")、反斜杠 ()、各种奇怪的 Unicode 字符等.
- 我们使用的是 windows redis-cli 客户端
- 奖励:理想情况下,我们可以将此命令作为 MULTI/EXEC 事务的一部分发出,因此我们还可以原子地删除 SET 和键.
如果我们能做类似下面的事情,我会很高兴,但让它处理所有给 Redis 带来问题的特殊字符的键:
I would LOVE if we could just do something like the below, but have it handle keys with all of the special characters that give Redis problems:
redis-cli SMEMBERS "myGiganticListOfKeys" |xargs --delim='\n' redis-cli DEL
不幸的是,这只是给出了以下错误:
Unfortunately this just gives the below error:
"C:/Program Files (x86)/Git/bin/xargs.exe": redis-cli: Bad file number
我认为如果我们的键中没有特殊字符,这会起作用.
I think this would otherwise work if we didn't have special characters in the keys.
非常感谢.
推荐答案
您应该尝试使用更强大的客户端创建应用程序.请参阅客户列表.redis-cli 是一个非常基本的命令行实用程序,旨在破解/玩 Redis.
You should try creating an application using a more robust client. See the client list. The redis-cli is a very basic command-line utility intended just to hack/play with Redis.
我同意您的看法,最好的方法是重新设计您的键/值存储.
I agree with you that the best will be to re-design your key/value store.
如果您需要使多个键无效,请考虑使用标记机制:在添加键时使用散列按标签对键进行分组,然后使整个标签无效,删除散列中的所有键.
Consider using a tagging mechanism if you need to invalidate multiple keys: Use a hash to group keys by tag when the keys are added, and then invalidate the entire tag removing all the keys in the hash.
这篇关于如何在Redis中批量删除数十万个带有特殊字符的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!