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

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> <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency> </dependency>
<!--springDataRedis 起步依赖--> <!--springDataRedis 起步依赖&缓存实现的起步依赖-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<!--缓存框架springCache的依赖-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId> <artifactId>spring-boot-starter-cache</artifactId>

View File

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

View File

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

View File

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