redis 正则匹配符合条件的key 进行删除
//根据名字的key,如果key数量超过100,就进行一次删除
public int clearRedis(@PathVariable("prefix") String prefix) throws IOException { ScanOptions options = ScanOptions.scanOptions().match(prefix + "*").count(1000).build(); Cursor cursor = redisTemplate.getConnectionFactory().getConnection().scan(options); int index = 0; try { List<String> list = new ArrayList<>(); while (cursor.hasNext()) { index++; try { String key = new String((byte[]) cursor.next()); list.add(key); if (list.size() >= 100) { redisTemplate.unlink(list); log.info("删除{},{},{},{}", index, cursor.getPosition(), list.get(0), key); list.clear(); } } catch (Exception e) { log.error("error,{},{}", index, cursor.getPosition()); } } if (list.size() > 0) { redisTemplate.unlink(list); log.info("删除{},{},{},{}", index, cursor.getPosition(), list.get(0), list.get(list.size() - 1)); list.clear(); } } finally { cursor.close(); } return index; }