springboot中註解實現集成redisjava
https://blog.csdn.net/a67474506/article/details/52608855web
Spring定義了org.springframework.cache.CacheManager和org.springframework.cache.Cache接口來統一不一樣的緩存技術,而SpringBoot爲咱們提供了自動配置多個CacheManager的實現。redis
其實是SpringBoot提早作好了這些工做,將經常使用的緩存中間件先集成了SpringBoot項目,開發者只須要【在POM中引入具體的實現】和【配置緩存中間件的好參數】便可。spring
application.yml中的配置信息以下所示:apache
其中的App類和DataCache類是公用的。也就是用SimpleCache、Guava、Redis三種不一樣的緩存應用,這兩個類的代碼徹底同樣。緩存
package com.ding.data; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ding.data.cache.DataCache; /** * 是Spring Boot項目的核心註解,主要是開啓自動配置 */ @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @RestController // 開啓緩存 @EnableCaching public class App { @Autowired private DataCache dataCache; public static void main(String[] args) { SpringApplication.run(App.class, args); } @RequestMapping("/put") public String put(Long id, String value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()) + " : value is " + dataCache.put(id, value) ; } @RequestMapping("/get") public String query(Long id){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()) + " : value is " +dataCache.query(id) ; } @RequestMapping("/remove") public String remove(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dataCache.remove(id) ; return sdf.format(new Date()) + " : success " ; } }
package com.ding.data.cache; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @Component public class DataCache { private Map<Long, String> dataMap = new HashMap<Long, String>(); /** * 初始化 */ @PostConstruct public void init() { dataMap.put(1L, "張三"); dataMap.put(2L, "李四"); dataMap.put(3L, "王五"); } /** * 查詢 * 若是數據沒有緩存,那麼從dataMap裏面獲取,若是緩存了, * 那麼從guavaDemo裏面獲取 * 而且將緩存的數據存入到 guavaDemo裏面 * 其中key 爲 #id+dataMap */ @Cacheable(value="guavaDemo" ,key="#id + 'dataMap'") public String query(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : query id is " + id); return dataMap.get(id); } /** * 插入 或者更新 * 插入或更新數據到dataMap中 * 而且緩存到 guavaDemo中 * 若是存在了那麼更新緩存中的值 * 其中key 爲 #id+dataMap */ @CachePut(value="guavaDemo" ,key="#id + 'dataMap'") public String put(Long id, String value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id); dataMap.put(id, value); // data persistence return value; } /** * 刪除 * 刪除dataMap裏面的數據 * 而且刪除緩存guavaDemo中的數據 * 其中key 爲 #id+dataMap */ @CacheEvict(value="guavaDemo" , key="#id + 'dataMap'") public void remove(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data"); dataMap.remove(id); // data remove } }
不須要在POM中引入任何緩存中間件(如Redis、Guava、Ehcache等),也不須要在application.yml配置文件中寫任何配置信息。springboot
只須要在主啓動類上加上 @EnableCaching 註解並在想使用緩存的地方使用 @Cacheable 、 @CachePut 、@CacheEvict 等註解就可使用了。app
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> --> <!-- <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> --> </dependencies> </project>
首次訪問eclipse
http://localhost:8080/get?id=1maven
eclipse控制檯打印以下
2018-07-03 19:49:22 : query id is 1
再次訪問,顯示內容同上,可是eclipse控制檯沒有打印。說明此時已經緩存成功了,沒有進入Service的方法。
訪問
http://localhost:8080/put?id=1&value=666
再次訪問
http://localhost:8080/get?id=1
Eclipse控制檯中並未打印,說明 @CachePut 註解後立刻數據立刻就進入了緩存。
訪問
http://localhost:8080/remove?id=1
Eclipse控制檯中打印以下:
2018-07-03 19:59:19 : remove id is 1 data
再次訪問
http://localhost:8080/get?id=1
緩存已刪除了。
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> </dependencies> </project>
spring: cache: #緩存名稱 cache-names: guavaDemo #緩存最大數量500條, 緩存失效時間 6個小時 guava.spec: maximumSize=500,expireAfterWrite=360m
跟SimpleCache的測試結果同樣。
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> --> </dependencies> </project>
spring: cache: #緩存名稱 cache-names: guavaDemo #緩存最大數量500條, 緩存失效時間 6個小時 #guava.spec: maximumSize=500,expireAfterWrite=360m # REDIS (RedisProperties) redis : host : localhost # server host port : 6379 # connection port password : 123 pool.max-idle : 8 # pool settings ... pool.min-idle : 1 pool.max-active : 8 pool.max-wait : -1
查詢前,可見Redis中尚未任何緩存信息
查詢
http://localhost:8080/get?id=1
Eclipse控制檯打印以下:
查看Redis中,可見多了一條數據
查看redis中的內容,已經變了
查看Redis的值,值已經消失了
調用結束,中止項目,查看Redis。可見調用後Redis的緩存仍然存在,由於同SimpleCache、Guava不同,Redis的緩存能夠實現持久化;而SimpleCache、Guava則隨着項目的中止緩存清空了。
若是沒有緩存,進入 @Cacheable 標註的方法內獲取數據,返回時將數據灌入value和key定位的緩存;若是有緩存,就不進入方法了,直接返回緩存。
確定進入方法進行操做,而後將返回值灌入value和key定位的緩存中。
刪除value和key定位的緩存。
在Spring Boot中經過@EnableCaching註解自動化配置合適的緩存管理器(CacheManager),默認狀況下Spring Boot根據下面的順序自動檢測緩存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
SpringBoot會根據你的類路徑裏面的依賴jar,來肯定使用什麼類型進行緩存,因此基本是咱們是不用配置spring.cache.type這個屬性的。
因此配置了Guava或者Redis緩存時,SpringBoot會自動檢測這兩個緩存的jar包和配置,發現那個就會自動適配上對應的緩存管理器並在後面使用。
那麼能否同時引入多個緩存組件並同時使用呢?如何使用呢?——後面會有說明