redis cluster 模式如何批量删除指定前缀的key


public static void delKeys(HostAndPort hostAndPort, String keysPattern) {
Map<String, JedisPool> clusterNodes = getJedisCluster(hostAndPort).getClusterNodes();

for (Map.Entry<String, JedisPool> entry : clusterNodes.entrySet()) {
Jedis jedis = entry.getValue().getResource();
if (!jedis.info("replication").contains("role:slave")) {
Set<String> keys = jedis.keys(keysPattern);

if (keys.size() > 0) {
Map<Integer, List<String>> map = new HashMap<>(6600);
for (String key : keys) {
int slot = JedisClusterCRC16.getSlot(key);//cluster模式执行多key操作的时候,这些key必须在同一个slot上,不然会报:JedisDataException: CROSSSLOT Keys in request don't hash to the same slot
//按slot将key分组,相同slot的key一起提交
if (map.containsKey(slot)) {
map.get(slot).add(key);
} else {
map.put(slot, Lists.newArrayList(key));
}
}
for (Map.Entry<Integer, List<String>> integerListEntry : map.entrySet()) {
jedis.del(integerListEntry.getValue().toArray(new String[integerListEntry.getValue().size()]));
}
}
}
}
}

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告