防止检查用户名缓存穿透
controller
@GetMapping("/api/short-link/admin/v1/user/has-username")
public Result<Boolean> hasUsername(@RequestParam("username") String username) {
return Results.success(userService.hasUsername(username));
}
service
@Override
public Boolean hasUsername(String username) {
return !userRegisterCachePenetrationBloomFilter.contains(username);
}
布隆过滤器
@Configuration(value = "rBloomFilterConfigurationByAdmin")
public class RBloomFilterConfiguration {
@Bean
public RBloomFilter<String> userRegisterCachePenetrationBloomFilter(RedissonClient redissonClient) {
RBloomFilter<String> cachePenetrationBloomFilter = redissonClient.getBloomFilter("userRegisterCachePenetrationBloomFilter");
cachePenetrationBloomFilter.tryInit(100000000L, 0.001);
return cachePenetrationBloomFilter;
}
}