diff --git a/pom.xml b/pom.xml index d098097..e96a1aa 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-cache + org.springframework.boot spring-boot-starter-data-redis diff --git a/src/main/java/cn/czyx007/reggie/ReggieApplication.java b/src/main/java/cn/czyx007/reggie/ReggieApplication.java index 5f315d5..9d08e35 100644 --- a/src/main/java/cn/czyx007/reggie/ReggieApplication.java +++ b/src/main/java/cn/czyx007/reggie/ReggieApplication.java @@ -3,6 +3,7 @@ package cn.czyx007.reggie; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.transaction.annotation.EnableTransactionManagement; /** @@ -12,6 +13,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @ServletComponentScan @EnableTransactionManagement +@EnableCaching//开启SpringCache缓存功能 public class ReggieApplication { public static void main(String[] args) { SpringApplication.run(ReggieApplication.class, args); diff --git a/src/main/java/cn/czyx007/reggie/common/R.java b/src/main/java/cn/czyx007/reggie/common/R.java index 23b33d0..1584777 100644 --- a/src/main/java/cn/czyx007/reggie/common/R.java +++ b/src/main/java/cn/czyx007/reggie/common/R.java @@ -2,6 +2,7 @@ package cn.czyx007.reggie.common; import lombok.Data; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; @@ -10,7 +11,7 @@ import java.util.Map; * @createTime : 2022/12/19 - 23:32 */ @Data -public class R { +public class R implements Serializable { private Integer code; //状态码:1成功,0和其它数字为失败 private String msg; //错误信息 diff --git a/src/main/java/cn/czyx007/reggie/controller/SetmealController.java b/src/main/java/cn/czyx007/reggie/controller/SetmealController.java index 34dec70..8d10c2a 100644 --- a/src/main/java/cn/czyx007/reggie/controller/SetmealController.java +++ b/src/main/java/cn/czyx007/reggie/controller/SetmealController.java @@ -16,6 +16,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; @@ -39,12 +41,15 @@ public class SetmealController { @Autowired private SetmealDishService setmealDishService; + //清理setmealCache分类下的所有缓存数据 + @CacheEvict(value = "setmealCache", allEntries = true) @PostMapping public R save(@RequestBody SetmealDto setmealDto){ setmealService.saveWithDish(setmealDto); return R.success("新增套餐成功"); } + @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status") @GetMapping("/list") public R> list(Setmeal setmeal){ LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); @@ -87,6 +92,7 @@ public class SetmealController { return R.success(setmealService.getByIdWithDishes(id)); } + @CacheEvict(value = "setmealCache", key = "#setmealDto.categoryId + '_' + #setmealDto.status") @PutMapping public R update(@RequestBody SetmealDto setmealDto){ setmealService.updateWithDishes(setmealDto); @@ -103,6 +109,8 @@ public class SetmealController { return R.success("套餐状态已经更改成功!"); } + //清理setmealCache分类下的所有缓存数据 + @CacheEvict(value = "setmealCache", allEntries = true) @Transactional @DeleteMapping public R delete(@RequestParam("ids") List ids){ diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a133ca1..8f3166d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,7 @@ spring: + cache: + redis: + time-to-live: 1800000 datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver