使用Spring Cache缓存套餐数据

master
sheng 2 years ago
parent 269fb0f83a
commit 67f1bb10d0

@ -6,10 +6,16 @@
<component name="ChangeListManager">
<list default="true" id="09a0ab93-8583-4cca-9183-2b28acbb0d2c" name="Changes" comment="redis环境配置">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/DishController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/DishController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/UserController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/UserController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/TakeOutApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/TakeOutApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/common/R.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/common/R.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/SetmealController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/SetmealController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.yml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/application.yml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/application.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/UserController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/UserController.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/TakeOutApplication.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/TakeOutApplication.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/common/R.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/DishController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/DishController.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/SetmealController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/SetmealController.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -143,6 +149,8 @@
<item itemvalue="Spring Boot.TakeOutApplication" />
<item itemvalue="JUnit.UploadFileTest.testString" />
<item itemvalue="JUnit.UploadFileTest.testRealPath" />
<item itemvalue="Spring Boot.TakeOutApplication" />
<item itemvalue="JUnit.UploadFileTest.testString" />
</list>
</recent_temporary>
</component>

@ -84,8 +84,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

@ -5,11 +5,13 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
@Slf4j
@SpringBootApplication
@ServletComponentScan("cn.edu.hactcm.filter")
@MapperScan("cn.edu.hactcm.mapper")
@EnableCaching//开启Spring Cache 注解方式的缓存功能
public class TakeOutApplication {
public static void main(String[] args) {
SpringApplication.run(TakeOutApplication.class, args);

@ -1,6 +1,8 @@
package cn.edu.hactcm.common;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@ -9,7 +11,7 @@ import java.util.Map;
* @param <T>
*/
@Data
public class R<T> {
public class R<T> implements Serializable {
private Integer code; //编码1成功0和其它数字为失败

@ -12,6 +12,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
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.web.bind.annotation.*;
@ -38,6 +40,7 @@ public class SetmealController {
*/
@PostMapping
@Transactional
@CacheEvict(value = "setmealCache",allEntries = true)
public R<String> saveMealDish(@RequestBody SetmealDto setmealDto){
setmealService.saveWithDish(setmealDto);
return R.success("添加成功!");
@ -78,6 +81,7 @@ public class SetmealController {
* @return
*/
@DeleteMapping
@CacheEvict(value = "setmealCache",allEntries = true)
public R<String> deleteWithDish(@RequestParam List<Long> ids){
setmealService.removeWithDish(ids);
return R.success("删除成功");
@ -89,6 +93,7 @@ public class SetmealController {
* @return
*/
@GetMapping("/list")
@Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
public R<List<Setmeal>> get(Setmeal setmeal){
Long categoryId = setmeal.getCategoryId();

@ -14,6 +14,9 @@ spring:
host: localhost
port: 6379
database: 0
cache:
redis:
time-to-live: 1800000 #设置缓存数据的过期时间
mybatis-plus:
configuration:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射

@ -14,6 +14,9 @@ spring:
host: localhost
port: 6379
database: 0
cache:
redis:
time-to-live: 1800000 #设置缓存数据的过期时间
mybatis-plus:
configuration:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射

Loading…
Cancel
Save