通过redis设置有效期来解决
@Transactional
public void checkRepeat(String name, String userId) {
String check_repeat = "repeat::%s::%s";
String key = String.format(check_repeat, name, userId);
boolean bool = redisTemplate.opsForValue().setIfAbsent(key, "1");
if (bool) {
redisTemplate.expire(key, 5, TimeUnit.SECONDS);
} else {
throw new RepeatException();
}
}