文章目录
-
- 概要
- 整体架构流程
- 技术细节
- 小结
概要
因为商品是经常被浏览的,所以数据库的访问量就问大大增加,造成负载过大影响性能,所以我们需要把商品缓存到redis当中,因为redis是存在内存中的,所以效率会比MySQL的快.
整体架构流程
技术细节
- 我们在缓存时需要保持数据的一致性
- 所以当管理端用户进行增删改操作的时候需要清理缓存,否则用户看到的商品则是缓存中的(未被修改的商品)
小结
@GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<DishVO>> list(Long categoryId) {//缓存菜品String key = "dish_" + categoryId;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);//查询起售中的菜品list = dishService.listWithFlavor(dish);//如果不存在则查数据库存入redisredisTemplate.opsForValue().set(key,list);return Result.success(list);}
。
package com.sky.controller.admin;import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;@RestController
@RequestMapping("admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate RedisTemplate redisTemplate;/*** 新增菜品* @param dishDTO* @return*/@PostMapping@ApiOperation("新增菜品")public Result save(@RequestBody DishDTO dishDTO){Long categoryId = dishDTO.getCategoryId();String key = "dish_" + categoryId;deleteCache(key);dishService.saveWithFlavor(dishDTO);return Result.success();}/*** 菜品分页查询* @param dishPageQueryDTO* @return*/@GetMapping("/page")@ApiOperation("菜品分页查询")public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){log.info("菜品分页查询:{}", dishPageQueryDTO);PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);return Result.success(pageResult);}/*** 批量删除菜品* @param ids* @return*/@DeleteMapping@ApiOperation("批量删除菜品")public Result delete(@RequestParam List<Long> ids){log.info("批量删除菜品:{}", ids);deleteCache("dish_*");dishService.deleteBatch(ids);return Result.success();}/*** 根据id查询菜品* @param id* @return*/@GetMapping("/{id}")@ApiOperation("根据id查询菜品")public Result<DishVO> getById(@PathVariable Long id){log.info("根据id查询菜品");DishVO dishVO = dishService.getByIdWithFlavor(id);return Result.success(dishVO);}/*** 修改菜品* @param dishDTO* @return*/@PutMapping@ApiOperation("修改菜品")public Result update(@RequestBody DishDTO dishDTO){log.info("修改菜品:{}",dishDTO);deleteCache("dish_*");dishService.updateWithFlavor(dishDTO);return Result.success();}@GetMapping("/list")public Result<List<Dish>> list(@RequestParam Long categoryId){log.info("根据分类id查询菜品:{}",categoryId);List<Dish> list = dishService.list(categoryId);return Result.success(list);}@PostMapping("/status/{status}")@ApiOperation("菜品起售停售")public Result<String> startOrStop(@PathVariable Integer status, Long id){deleteCache("dish_*");dishService.startOrStop(status,id);return Result.success();}private void deleteCache(String pattern){Set keys = redisTemplate.keys(pattern);redisTemplate.delete(keys);}
}