本文介绍了如何删除Redis集群中匹配模式的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题中尝试了方法,但它不起作用,因为我在集群模式下工作,redis 告诉我:

I have tried method in this question, but it does not work since I'm working in cluster mode, and redis told me:

(错误)请求中的 CROSSSLOT 键不会散列到同一个槽

推荐答案

答案问题 尝试在单个 DEL 中删除多个键.但是,匹配给定模式的键可能不会位于同一个槽中,如果这些键不属于同一个槽,Redis 集群不支持多键命令.这就是您收到错误消息的原因.

Answers for that question try to remove multiple keys in a single DEL. However, keys matching the given pattern might NOT locate in the same slot, and Redis Cluster DOES NOT support multiple-key command if these keys don't belong to the same slot. That's why you get the error message.

为了解决这个问题,你需要一一DEL这些键:

In order to fix this problem, you need to DEL these keys one-by-one:

redis-cli --scan --pattern "foo*" |xargs -L 1 redis-cli del

xargs 命令的 -L 选项指定要删除的键数.您需要将此选项指定为 1.

The -L option for xargs command specifies the number of keys to delete. You need to specify this option as 1.

为了删除与模式匹配的所有键,您还需要为集群中的每个主节点运行上述命令.

In order to remove all keys matching the pattern, you also need to run the above command for every master nodes in your cluster.

注意

  1. 使用这个命令,你必须一个一个地删除这些键,这可能会很慢.您需要考虑重新设计您的数据库,并使用 hash-tags 使匹配模式的键属于同一个槽.这样您就可以在一个 DEL 中删除这些键.

  1. With this command, you have to delete these keys one-by-one, and that might be very slow. You need to consider re-designing your database, and use hash-tags to make keys matching the pattern belong to the same slot. So that you can remove these keys in a single DEL.

SCANKEYS 命令都是低效的,尤其是 KEYS 不应该在生产中使用.您需要考虑为这些键建立索引.

Either SCAN or KEYS command are inefficient, especially, KEYS should not be used in production. You need to consider building an index for these keys.

这篇关于如何删除Redis集群中匹配模式的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 01:58