找回密码
 立即注册
查看: 147|回复: 0

springboot3缓存redis菜品处理代码

[复制链接]

156

主题

5

精华

160

金币

技术维护QQ:515138

积分
345
发表于 2025-12-23 14:03:51 | 显示全部楼层 |阅读模式
springboot3缓存redis菜品处理代码
后端:
  1. package com.zidiu.controller.admin;
  2. import com.zidiu.dto.DishDTO;
  3. import com.zidiu.dto.DishPageQueryDTO;
  4. import com.zidiu.entity.Dish;
  5. import com.zidiu.result.PageResult;
  6. import com.zidiu.result.Result;
  7. import com.zidiu.service.DishService;
  8. import com.zidiu.vo.DishVO;
  9. import io.swagger.v3.oas.annotations.Operation;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.web.bind.annotation.*;
  15. import java.util.List;
  16. import java.util.Set;
  17. @RestController
  18. @Slf4j
  19. @RequestMapping("/admin/dish")
  20. @Tag(name = "菜品相关接口")
  21. public class DishController {
  22.     @Autowired
  23.     private DishService dishService;
  24.     @Autowired
  25.     private RedisTemplate redisTemplate;
  26.     /**
  27.      * 新增菜品
  28.      *
  29.      * @param dishDTO
  30.      * @return
  31.      */
  32.     @PostMapping
  33.     @Operation(summary = "新增菜品", description = "新增菜品")
  34.     public Result<String> save(@RequestBody DishDTO dishDTO) {
  35.         log.info("新增菜品:{}", dishDTO);
  36.         dishService.saveWithFlavor(dishDTO);
  37.         //清理缓存数据
  38.         String key = "dish_" + dishDTO.getCategoryId();
  39.         cleanCache(key);
  40.         return Result.success();
  41.     }
  42.     /**
  43.      * 菜品分页查询
  44.      *
  45.      * @param dishPageQueryDTO
  46.      * @return
  47.      */
  48.     @GetMapping("/page")
  49.     @Operation(summary = "菜品分页查询", description = "菜品分页查询")
  50.     public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
  51.         log.info("菜品分页查询:{}", dishPageQueryDTO);
  52.         PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
  53.         return Result.success(pageResult);
  54.     }
  55.     /**
  56.      * 菜品批量删除
  57.      *
  58.      * @param ids
  59.      * @return
  60.      */
  61.     @DeleteMapping
  62.     @Operation(summary = "菜品批量删除", description = "菜品批量删除")
  63.     public Result<String> delete(@RequestParam List<Long> ids) {
  64.         log.info("菜品批量删除:{}", ids);
  65.         dishService.deleteBatch(ids);
  66.         //将所有的菜品缓存数据清理掉,所有以dish_开头的key
  67.         cleanCache("dish_*");
  68.         return Result.success();
  69.     }
  70.     /**
  71.      * 根据id查询菜品
  72.      *
  73.      * @param id
  74.      * @return
  75.      */
  76.     @GetMapping("/{id}")
  77.     @Operation(summary = "根据id查询菜品", description = "根据id查询菜品")
  78.     public Result<DishVO> getById(@PathVariable Long id) {
  79.         log.info("根据id查询菜品:{}", id);
  80.         DishVO dishVO = dishService.getByIdWithFlavor(id);
  81.         return Result.success(dishVO);
  82.     }
  83.     /**
  84.      * 修改菜品
  85.      *
  86.      * @param dishDTO
  87.      * @return
  88.      */
  89.     @PutMapping
  90.     @Operation(summary = "修改菜品", description = "修改菜品")
  91.     public Result<String> update(@RequestBody DishDTO dishDTO) {
  92.         log.info("修改菜品:{}", dishDTO);
  93.         dishService.updateWithFlavor(dishDTO);
  94.         //将所有的菜品缓存数据清理掉,所有以dish_开头的key
  95.         cleanCache("dish_*");
  96.         return Result.success();
  97.     }
  98.     /**
  99.      * 菜品起售停售
  100.      *
  101.      * @param status
  102.      * @param id
  103.      * @return
  104.      */
  105.     @PostMapping("/status/{status}")
  106.     @Operation(summary = "菜品起售停售", description = "菜品起售停售")
  107.     public Result<String> startOrStop(@PathVariable Integer status, Long id) {
  108.         dishService.startOrStop(status, id);
  109.         //将所有的菜品缓存数据清理掉,所有以dish_开头的key
  110.         cleanCache("dish_*");
  111.         return Result.success();
  112.     }
  113.     /**
  114.      * 根据分类id查询菜品
  115.      *
  116.      * @param categoryId
  117.      * @return
  118.      */
  119.     @GetMapping("/list")
  120.     @Operation(summary = "根据分类id查询菜品", description = "根据分类id查询菜品")
  121.     public Result<List<Dish>> list(Long categoryId) {
  122.         List<Dish> list = dishService.list(categoryId);
  123.         return Result.success(list);
  124.     }
  125.     /**
  126.      * 清理缓存数据
  127.      * @param pattern
  128.      */
  129.     private void cleanCache(String pattern){
  130.         Set keys = redisTemplate.keys(pattern);
  131.         redisTemplate.delete(keys);
  132.     }
  133. }
复制代码
前端:
  1. package com.zidiu.controller.user;
  2. import com.zidiu.constant.StatusConstant;
  3. import com.zidiu.entity.Dish;
  4. import com.zidiu.result.Result;
  5. import com.zidiu.service.DishService;
  6. import com.zidiu.vo.DishVO;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.util.List;
  16. @RestController("userDishController")
  17. @RequestMapping("/user/dish")
  18. @Slf4j
  19. @Tag(name = "C端-菜品浏览接口")
  20. public class DishController {
  21.     @Autowired
  22.     private DishService dishService;
  23.     @Autowired
  24.     private RedisTemplate redisTemplate;
  25.     /**
  26.      * 根据分类id查询菜品
  27.      *
  28.      * @param categoryId
  29.      * @return
  30.      */
  31.     @GetMapping("/list")
  32.     @Operation(summary = "根据分类id查询菜品", description = "根据分类id查询菜品")
  33.     public Result<List<DishVO>> list(Long categoryId) {
  34.         //构造redis中的key,规则:dish_分类id
  35.         String key = "dish_" + categoryId;
  36.         //查询redis中是否存在菜品数据
  37.         List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
  38.         if(list != null && !list.isEmpty()){
  39.             //如果存在,直接返回,无须查询数据库
  40.             return Result.success(list);
  41.         }
  42.         Dish dish = new Dish();
  43.         dish.setCategoryId(categoryId);
  44.         dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
  45.         //如果不存在,查询数据库,将查询到的数据放入redis中
  46.         list = dishService.listWithFlavor(dish);
  47.         redisTemplate.opsForValue().set(key, list);
  48.         return Result.success(list);
  49.     }
  50. }
复制代码
缓存配置:
  1. package com.zidiu.config;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.RedisConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;
  8. @Configuration
  9. @Slf4j
  10. public class RedisConfiguration {
  11.     @Bean
  12.     public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
  13.         log.info("开始创建redis模板对象...");
  14.         RedisTemplate redisTemplate = new RedisTemplate();
  15.         //设置redis的连接工厂对象
  16.         redisTemplate.setConnectionFactory(redisConnectionFactory);
  17.         //设置redis key的序列化器
  18.         redisTemplate.setKeySerializer(new StringRedisSerializer());
  19.         return redisTemplate;
  20.     }
  21. }
复制代码
完整源代码:
jinhei-waimai.zip (526.02 KB, 下载次数: 0, 售价: 50 金币)

Spring Data Redis使用方式

SpringBoot3+SSM的企业级Java项目实战之外卖小程序


上一篇:springboot3之微信小程序开发
下一篇:HttpClient,httpclient在springboot3微信登录实操案例代码
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-1-19 13:44

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表