解决Redis 缓存雪崩(过期时间不一致) 和 缓存穿透(黑名单)
public Product getdetailById(Integer id) {String key = "product." + id;// 查询黑名单中是否有该keyBoolean b = hashOperations.hasKey(PROODUCT_DETAIL_BLACK, key);if(b){log.error("商品不存在id=>{}",id);throw new BizException(201,"商品不存在在黑名单id=>"+id);}//如果黑名单没有该key,先查缓存Product product = valueOperations.get(PROODUCT_DETAIL+id);if (ObjectUtil.isNotEmpty(product)) {return product;}//缓存不存在,查询数据库log.debug("缓存不存在,查询数据库");product = productDao.selectById(id);if(ObjectUtil.isEmpty(product)){log.error("数据库中不存在该商品id=>{}",id);//在将该商品加入黑名单中hashOperations.put(PROODUCT_DETAIL_BLACK,key, DateUtil.now());throw new BizException(202,"商品不存在id=>"+id);}int i = RandomUtil.randomInt(-60, 60);//将查询出来的数据放到redis中;valueOperations.set(PROODUCT_DETAIL+ id, product,60*24+i, TimeUnit.MINUTES);return product;}