苍穹外卖--redis缓存菜品功能业务-当管理端操作菜品时,清理菜品缓存功能实现
This commit is contained in:
@@ -11,9 +11,11 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RestController("adminDishController")
|
||||
@Slf4j
|
||||
@@ -24,13 +26,20 @@ public class DishController {
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation("新增菜品")
|
||||
public Result add(@RequestBody DishDTO dto){
|
||||
log.info("新增菜品:{}",dto);
|
||||
dishService.saveDishWitchFlavor(dto);
|
||||
|
||||
//清理新增菜品所绑定的分类的缓存数据
|
||||
/*String key = "dish_"+dto.getCategoryId();
|
||||
redisTemplate.delete(key);*/
|
||||
String pattern = "dish_"+dto.getCategoryId();
|
||||
cleanDishCache(pattern);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@@ -52,6 +61,10 @@ public class DishController {
|
||||
log.info("菜品批量删除:{}",ids);
|
||||
//调用业务层的菜品批量删除功能
|
||||
dishService.deleteBath(ids);
|
||||
|
||||
//当前批量删除菜品时,我们需要通过更多的查询操作,才能知道删除了哪些分类的菜品,(为了效率)那么我们直接将所有的菜品缓存直接清楚即可
|
||||
cleanDishCache("dish_*");
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@@ -68,6 +81,8 @@ public class DishController {
|
||||
public Result getById(@RequestBody DishDTO dishDTO){ //@RequestParam:能够使用springmvc框架,对1,2,3 参数进行切割
|
||||
log.info("修改菜品:{}",dishDTO);
|
||||
dishService.updateWithFlavor(dishDTO);
|
||||
//当前修改菜品时,可能修改价格,也可能修改分类,那么我们直接将所有的菜品缓存直接清楚即可
|
||||
cleanDishCache("dish_*");
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@@ -76,9 +91,18 @@ public class DishController {
|
||||
public Result startOrStop(@PathVariable Integer status,Long id){ //@RequestParam:能够使用springmvc框架,对1,2,3 参数进行切割
|
||||
log.info("菜品起售停售:{},{}",status,id);
|
||||
dishService.startOrStop(status,id);
|
||||
|
||||
//当前起售停售菜品,只传递了菜品ID,那么我们要知道对应的分类,还要做一次select,那么我们直接将所有的菜品缓存直接清楚即可
|
||||
cleanDishCache("dish_*");
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
//抽取清理菜品缓存
|
||||
private void cleanDishCache(String pattern) {
|
||||
Set keys = redisTemplate.keys(pattern);
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
* @param categoryId
|
||||
|
||||
Reference in New Issue
Block a user