项目一缓存商品

avatar
作者
猴君
阅读量:0

文章目录

概要

因为商品是经常被浏览的,所以数据库的访问量就问大大增加,造成负载过大影响性能,所以我们需要把商品缓存到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);         //如果不存在则查数据库存入redis         redisTemplate.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 {      @Autowired     private DishService dishService;     @Autowired     private 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);     } } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!