课外开发苍穹外卖苍穹外卖学习笔记(七)
Jie一. 缓存菜品
通过Redis来缓存菜品数据,减少数据库查询操作
缓存菜品
根据分类ID缓存菜品
key : value
DishController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.sky.controller.user;
import com.sky.constant.StatusConstant; import com.sky.entity.Dish; 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.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;
import java.util.List;
@RestController("userDishController") @RequestMapping("/user/dish") @Api(tags = "菜品管理") @Slf4j public class DishController {
@Autowired private DishService dishService;
@Autowired private RedisTemplate redisTemplate;
@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.isEmpty()) { return Result.success(list); } Dish dish = new Dish(); dish.setCategoryId(categoryId); dish.setStatus(StatusConstant.ENABLE); list = dishService.listWithFlavor(dish); redisTemplate.opsForValue().set(key, list); return Result.success(list); } }
|
清除缓存数据
当我们新增菜品、修改菜品、批量删除菜品、起售、停售菜品时清理缓存
DishController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| 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.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") @RequestMapping("/admin/dish") @Slf4j @Api(tags = "菜品管理") public class DishController {
@Autowired private DishService dishService;
@Autowired private RedisTemplate redisTemplate;
@PostMapping @ApiOperation("新增菜品") public Result save(@RequestBody DishDTO dishDTO) { log.info("新增菜品:{}", dishDTO); dishService.saveWithFlavor(dishDTO);
String key = "dish_" + dishDTO.getCategoryId(); redisTemplate.delete(key); return Result.success(); }
@GetMapping("/page") @ApiOperation("分页查询菜品") public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) { log.info("分页查询菜品:{}", dishPageQueryDTO); PageResult pageResult = dishService.pageQuery(dishPageQueryDTO); return Result.success(pageResult); }
@DeleteMapping @ApiOperation("删除菜品") public Result delete(@RequestParam List<Long> ids) { log.info("删除菜品:{}", ids); dishService.deleteBatch(ids);
cleanCache("dish_*"); return Result.success(); }
@GetMapping("/{id}") @ApiOperation("根据id查询菜品") public Result<DishVO> getById(@PathVariable Long id) { log.info("根据id查询菜品:{}", id); DishVO dishVO = dishService.getByIdWithFlavor(id); return Result.success(dishVO); }
@PutMapping @ApiOperation("修改菜品") public Result update(@RequestBody DishDTO dishDTO) { log.info("修改菜品:{}", dishDTO); dishService.updateWithFlavor(dishDTO);
cleanCache("dish_*"); return Result.success(); }
@PostMapping("/status/{status}") @ApiOperation("修改菜品状态") public Result updateStatus(@RequestParam Long id, @PathVariable Integer status) { log.info("修改菜品状态:{}", id); dishService.updateStatus(id, status);
cleanCache("dish_*"); return Result.success(); }
@GetMapping("/list") @ApiOperation("根据分类id查询菜品") public Result<List<Dish>> listResult(@RequestParam Long categoryId) { log.info("根据分类id查询菜品:{}", categoryId); List<Dish> list = dishService.listByCategoryId(categoryId); return Result.success(list); }
private void cleanCache(String pattern) { Set keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } }
|
缓存套餐
Spring Cache
Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能
Spring Cache提供了一层抽象,底层可以切换不同的缓存实现,例如:
- EHCache
- Caffeine
- Redis
maven坐标
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.7.3</version> </dependency>
|
常用注解
注解 | 说明 |
---|
@EnableCaching | 开启缓存注解功能,通常加在启动类上 |
@Cacheable | 在方法执行前先查询缓存中是否有数据,如果有数据,则直接返回缓存数据;如果没有缓存数据,调用方法并将方法返回值放到缓存中 |
@CachePut | 将方法的返回值放到缓存中 |
@CacheEvict | 将一条或多条数据从缓存中删除 |
二. 购物车功能
添加购物车
查看购物车
清空购物车
删除购物车中一个商品