From a3cd3c85b2839b3abf01fbc9ad388cb587390350 Mon Sep 17 00:00:00 2001
From: zyx <1029606625@qq.com>
Date: Mon, 27 Feb 2023 21:07:15 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8SpringCache=E7=BC=93=E5=AD=98?=
=?UTF-8?q?=E5=A5=97=E9=A4=90=E6=95=B0=E6=8D=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 4 ++++
src/main/java/cn/czyx007/reggie/ReggieApplication.java | 2 ++
src/main/java/cn/czyx007/reggie/common/R.java | 3 ++-
.../cn/czyx007/reggie/controller/SetmealController.java | 8 ++++++++
src/main/resources/application.yml | 3 +++
5 files changed, 19 insertions(+), 1 deletion(-)
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