苍穹外卖--用户端查询时,保存套餐缓存,管理端增删改起停售时,套餐缓存的清理业务实现

This commit is contained in:
2025-11-24 14:13:24 +08:00
parent d1291b7a6c
commit 08292e03f4
4 changed files with 15 additions and 1 deletions

View File

@@ -86,12 +86,13 @@
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
<!--springDataRedis 起步依赖-->
<!--springDataRedis 起步依赖&缓存实现的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--缓存框架springCache的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>

View File

@@ -3,11 +3,13 @@ package com.sky;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching //开启缓存注解功能
public class SkyApplication {
public static void main(String[] args) {
SpringApplication.run(SkyApplication.class, args);

View File

@@ -10,6 +10,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.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -33,6 +34,8 @@ public class SetmealController {
*/
@PostMapping
@ApiOperation("新增套餐")
//在客户端新增套餐时,要把对应分类的套餐缓存清理,在下一次查询时,获取最新的数据进行缓存
@CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
public Result save(@RequestBody SetmealDTO setmealDTO) {
setmealService.saveWithDish(setmealDTO);
return Result.success();
@@ -57,6 +60,8 @@ public class SetmealController {
*/
@DeleteMapping
@ApiOperation("批量删除套餐")
//客户端批量删除套餐数据时,可能删除多个分类下的套餐,为了避免多余的查询操作,直接将套餐的所有缓存直接清理
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
public Result delete(@RequestParam List<Long> ids){
setmealService.deleteBatch(ids);
return Result.success();
@@ -83,6 +88,8 @@ public class SetmealController {
*/
@PutMapping
@ApiOperation("修改套餐")
//客户端修改套餐数据时,可能修改分类的类型,为了避免多余的查询操作,直接将套餐的所有缓存直接清理
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
public Result update(@RequestBody SetmealDTO setmealDTO) {
setmealService.update(setmealDTO);
return Result.success();
@@ -96,6 +103,8 @@ public class SetmealController {
*/
@PostMapping("/status/{status}")
@ApiOperation("套餐起售停售")
//客户端起售停售套餐时没有传递套餐的分类id为了避免多余的查询操作直接将套餐的所有缓存直接清理
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
public Result startOrStop(@PathVariable Integer status, Long id) {
setmealService.startOrStop(status, id);
return Result.success();

View File

@@ -8,6 +8,7 @@ import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -29,6 +30,7 @@ public class SetmealController {
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询套餐")
@Cacheable(cacheNames = "setmealCache",key = "#categoryId") //redis的key setmealCache::#categoryId
public Result<List<Setmeal>> list(Long categoryId) {
Setmeal setmeal = new Setmeal();
setmeal.setCategoryId(categoryId);