使用SpringCache缓存套餐数据

This commit is contained in:
zyx 2023-02-27 21:07:15 +08:00
parent aed64f0b8e
commit a3cd3c85b2
5 changed files with 19 additions and 1 deletions

View File

@ -24,6 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>

View File

@ -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);

View File

@ -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<T> {
public class R<T> implements Serializable {
private Integer code; //状态码1成功0和其它数字为失败
private String msg; //错误信息

View File

@ -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<String> save(@RequestBody SetmealDto setmealDto){
setmealService.saveWithDish(setmealDto);
return R.success("新增套餐成功");
}
@Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
@GetMapping("/list")
public R<List<Setmeal>> list(Setmeal setmeal){
LambdaQueryWrapper<Setmeal> 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<String> 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<String> delete(@RequestParam("ids") List<Long> ids){

View File

@ -1,4 +1,7 @@
spring:
cache:
redis:
time-to-live: 1800000
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver