苍穹外卖--redis缓存菜品功能业务实现
This commit is contained in:
@@ -9,6 +9,7 @@ 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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -22,6 +23,9 @@ public class DishController {
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 根据分类id查询菜品
|
||||
*
|
||||
@@ -31,11 +35,23 @@ public class DishController {
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
public Result<List<DishVO>> list(Long categoryId) {
|
||||
//这里采用redis中string数据类型进行缓存数据:key:value (dish_分类id : 菜品集合的字符串)
|
||||
//1.构造redis中的key,规则:dish_分类id
|
||||
String key = "dish_"+categoryId;
|
||||
//2.查询redis中是否存在菜品数据,
|
||||
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
|
||||
if (list != null && list.size() > 0) {
|
||||
//3.如果存在缓存,直接返回,无需查询数据库
|
||||
return Result.success(list);
|
||||
}
|
||||
//4.如果不存在缓存,查询数据库,将查询到的数据保存到redis中
|
||||
|
||||
Dish dish = new Dish();
|
||||
dish.setCategoryId(categoryId);
|
||||
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
|
||||
|
||||
List<DishVO> list = dishService.listWithFlavor(dish);
|
||||
list = dishService.listWithFlavor(dish);
|
||||
//5.缓存到reids中
|
||||
redisTemplate.opsForValue().set(key,list);
|
||||
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user