Spring Boot @Cacheable 方法緩存失效
-
新建了Spring Boot項目,加入了spring-boot-starter-cache依賴的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>wen</groupId> <artifactId>cachesession</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cachesession</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
建立控制類
package wen.cachesession.controller; import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { @RequestMapping("/hello") @Cacheable(value = "helloCache") public String hello(String name) { System.out.println("無緩存"); return "hello " + name; } }
-
訪問瀏覽器輸入 http://localhost:8080/hello?name=Stephanie, 刷新幾次之後控制檯輸出“無緩存”, 並不是理想的效果,說明每次服務器都重新加載
-
解決方案:① 在啓動類增加註解 @EnableCaching, ② 控制類類名處聲明:@CacheConfig(cacheNames = “helloCache”), 在具體方法上增加@Cacheable
-
啓動類:
package wen.cachesession; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class CachesessionApplication { public static void main(String[] args) { SpringApplication.run(CachesessionApplication.class, args); } }
-
控制類:
package wen.cachesession.controller; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @CacheConfig(cacheNames = "helloCache") public class RedisController { @RequestMapping("/hello") @Cacheable public String hello(String name) { System.out.println("無緩存"); return "hello " + name; } }
-
重啓程序刷新之後:網頁顯示不變,但是網頁多次刷新之後控制檯也只輸出一次“無緩存”, 說明調用了緩存