Day6
缓存菜品
通过Redis来缓存菜品数据,减少数据库查询操作

清理每个分类缓存数据
public Result<List<DishVO>> list(Long categoryId) {
//构造redis中的key,规则:dish_分类id
String key = "dish_" + categoryId;
//查询redis中是否存在菜品数据
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
//如果存在,直接返回,无需查询数据库
if(list != null && list.size() > 0) {
return Result.success(list);
}
Dish dish = new Dish();
dish.setCategoryId(categoryId);
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
//如果不存在,查询数据库,将查询到的数据库放入redis中
list = dishService.listWithFlavor(dish);
redisTemplate.opsForValue().set(key, list);
return Result.success(list);
}
缓存套餐
Spring cache



@Cacheable(cacheNames = "setmealCache", key = "categoryId")
- @Cacheable 注解:
这是 Spring 缓存机制的核心注解之一,用于标记一个方法的返回结果需要被缓存。当该方法被调用时,Spring 会先检查缓存中是否存在对应的结果:- 如果存在,直接返回缓存中的数据,不执行方法体
- 如果不存在,执行方法并将结果存入缓存,后续调用则直接使用缓存
- cacheNames = "setmealCache":
指定缓存的名称(也称为缓存的命名空间),相当于给缓存起了一个名字叫 "setmealCache"。
作用是将不同类型的数据缓存隔离开,避免缓存键冲突。例如不同业务的缓存可以用不同的 cacheNames 区分。 - key = "categoryId":
指定缓存中存储数据时使用的键(Key)。这里表示使用方法参数中的categoryId的值作为缓存键。
@CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
- @CacheEvict 注解:
该注解用于标记一个方法执行后需要清除缓存中的某些数据。通常在数据发生变更(如新增、修改、删除操作)时使用,以保证缓存数据与实际数据一致。 - cacheNames = "setmealCache":
指定要操作的缓存名称,与之前@Cacheable中配置的cacheNames对应,表明要清除的是名为 "setmealCache" 的缓存中的数据。 - key = "#setmealDTO.categoryId":
指定要清除的缓存项的键(Key)。这里的#setmealDTO.categoryId是 Spring EL 表达式,表示:#setmealDTO指代方法参数中的setmealDTO对象.categoryId表示获取该对象的categoryId属性值
- allEntries = true:
这是一个关键属性,表示清除该缓存中的所有条目,而不是根据某个 key 清除特定条目。
当设置为true时,无论缓存中存在多少个以不同 key 存储的数据,都会被全部清除。
添加购物车
public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//判断当前加入到购物车中的商品是否已经存在了
ShoppingCart shoppingCart = new ShoppingCart();
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
Long userId = BaseContext.getCurrentId();
shoppingCart.setUserId(userId);
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
//如果已经存在,只需要将数量加一
if(list != null && list.size() > 0) {
ShoppingCart Cart = list.get(0);
Cart.setNumber(Cart.getNumber() + 1);
shoppingCartMapper.updateNumberById(Cart);
}
else {
//如果不存在,需要插入一条购物车数据
Long dishId = shoppingCartDTO.getDishId();
if (dishId != null) {
//本次添加到购物车的是菜品
Dish dish = dishMapper.getById(dishId);
shoppingCart.setName(dish.getName());
shoppingCart.setImage(dish.getImage());
shoppingCart.setAmount(dish.getPrice());
} else {
//本次添加到购物车的是套餐
Long setmealId = shoppingCartDTO.getSetmealId();
Setmeal setmeal = setmealMapper.getById(setmealId);
shoppingCart.setName(setmeal.getName());
shoppingCart.setImage(setmeal.getImage());
shoppingCart.setAmount(setmeal.getPrice());
}
shoppingCart.setNumber(1);
shoppingCart.setCreateTime(LocalDateTime.now());
shoppingCartMapper.insert(shoppingCart);
}
}
ShoppingCart Cart = list.get(0);
根据userId,setmealId,dishId,dishFlavor查询和业务场景决定了查询结果只会有一条记录
查看购物车
根据微信用户id来查看自己的购物车,不能查看别人的购物车
public List<ShoppingCart> showShoppingCart() {
//获取到当前微信用户的id
Long userId = BaseContext.getCurrentId();
ShoppingCart shoppingCart = ShoppingCart.builder()
.userId(userId)
.build();
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
return list;
}
清空购物车
根据微信用户id来清空自己的购物车,不能清空别人的购物车
public void cleanShoppingCart() {
//获取到当前微信用户的id
Long userId = BaseContext.getCurrentId();
shoppingCartMapper.deleteByUserId(userId);
}
删除购物车中一个商品
public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
ShoppingCart shoppingCart = new ShoppingCart();
BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//设置查询条件,查询当前登录用户的购物车数据
shoppingCart.setUserId(BaseContext.getCurrentId());
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
if(list != null && list.size() > 0){
shoppingCart = list.get(0);
Integer number = shoppingCart.getNumber();
if(number == 1){
//当前商品在购物车中的份数为1,直接删除当前记录
shoppingCartMapper.deleteById(shoppingCart.getId());
}else {
//当前商品在购物车中的份数不为1,修改份数即可
shoppingCart.setNumber(shoppingCart.getNumber() - 1);
shoppingCartMapper.updateNumberById(shoppingCart);
}
}
}
浙公网安备 33010602011771号