SpringBoot在annotation的層面實現了數據緩存的功能,基于Spring的AOP技術。所有的緩存配置只是在annotation層面配置,像聲明式事務一樣。
Spring定義了CacheManager和Cache接口統一不同的緩存技術。其中CacheManager是Spring提供的各種緩存技術的抽象接口。而Cache接口包含緩存的各種操作。
CacheManger
針對不同的緩存技術,需要實現不同的cacheManager,Spring定義了如下的cacheManger實現。
CacheManger | 描述 |
---|---|
SimpleCacheManager | 使用簡單的Collection來存儲緩存,主要用于測試 |
ConcurrentMapCacheManager | 使用ConcurrentMap作為緩存技術(默認) |
NoOpCacheManager | 測試用 |
EhCacheCacheManager | 使用EhCache作為緩存技術,以前在hibernate的時候經常用 |
GuavaCacheManager | 使用google guava的GuavaCache作為緩存技術 |
HazelcastCacheManager | 使用Hazelcast作為緩存技術 |
JCacheCacheManager | 使用JCache標準的實現作為緩存技術,如Apache Commons JCS |
RedisCacheManager | 使用Redis作為緩存技術 |
常規的SpringBoot已經為我們自動配置了EhCache、Collection、Guava、ConcurrentMap等緩存,默認使用ConcurrentMapCacheManager。SpringBoot的application.properties配置文件,使用spring.cache前綴的屬性進行配置。
application配置
1
2
3
4
5
6
|
spring.cache.type=#緩存的技術類型 spring.cache.cache-names=應用程序啟動創建緩存的名稱 spring.cache.ehcache.config=ehcache的配置文件位置 spring.cache.infinispan.config=infinispan的配置文件位置 spring.cache.jcache.config=jcache配置文件位置 spring.cache.jcache.provider=當多個jcache實現類時,指定選擇jcache的實現類 |
入口類配置
加入注解 @EnableCaching
緩存注解
注解 | 描述 |
---|---|
@Cacheable | 在調用方法之前,首先應該在緩存中查找方法的返回值,如果這個值能夠找到,就會返回緩存的值。否則,這個方法就會被調用,返回值會放到緩存之中。 |
@CachePut | 將方法的返回值放到緩存中。在方法的調用前并不會檢查緩存,方法始終都會被調用。 |
@CacheEvict | 在緩存中清除一個或多個條目。 |
@Caching | 分組的注解,能夠同時應用多個其他的緩存注解。 |
手動使用EhCache
在實際開發過程中,存在不使用注解,需要自己添加緩存的情況。下面就以Ehcache為例,簡單寫一下配置過程。
1. 添加依賴
引入springboot-cache和ehcache。需要注意,EhCache不需要配置version,SpringBoot的根pom已經集成了。
1
2
3
4
5
6
7
8
9
10
|
<!-- 緩存 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-cache</ artifactId > </ dependency > <!-- ehcache --> < dependency > < groupId >net.sf.ehcache</ groupId > < artifactId >ehcache</ artifactId > </ dependency > |
2. 入口類配置
加入注解 @EnableCaching
1
2
3
4
|
@SpringBootApplication @EnableCaching public class DemoApplication { } |
3. EhCache配置
在src\main\resources目錄下,添加ehcache.xml文件,內容見文末。
4. application.application配置
1
2
3
4
|
# 配置ehcache緩存 spring.cache.type=ehcache # 指定ehcache配置文件路徑 spring.cache.ehcache.config=classpath:/ehcache.xml |
5. 使用Cache
注入SpringBoot自動配置的bean,org.springframework.cache.CacheManager。
一個簡單的測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.bbf.frame.test; import com.bbf.frame.Application; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; @RunWith (SpringJUnit4ClassRunner. class ) @WebAppConfiguration @SpringBootTest (classes = Application. class , webEnvironment = SpringBootTest.WebEnvironment.MOCK) public class TestCache { @Resource private CacheManager cacheManager; @Test public void cacheTest() { // 顯示所有的Cache空間 System.out.println(StringUtils.join(cacheManager.getCacheNames(), "," )); Cache cache = cacheManager.getCache( "userCache" ); cache.put( "key" , "123" ); System.out.println( "緩存成功" ); String res = cache.get( "key" , String. class ); System.out.println(res); } } |
附錄 EhCache.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<? xml version = "1.0" encoding = "UTF-8" ?> < ehcache xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "http://ehcache.org/ehcache.xsd" updateCheck = "false" > <!-- 指定一個文件目錄,當EHCache把數據寫到硬盤上時,將把數據寫到這個文件目錄下 --> < diskStore path = "java.io.tmpdir" /> <!-- 默認的管理策略 --> < defaultCache eternal = "false" maxElementsInMemory = "10000" overflowToDisk = "true" diskPersistent = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "120" diskExpiryThreadIntervalSeconds = "120" memoryStoreEvictionPolicy = "LRU" /> <!-- 此緩存最多可以存活timeToLiveSeconds秒,如果期間超過timeToIdleSeconds秒未訪問,緩存失效 --> < cache name = "userCache" eternal = "false" maxElementsInMemory = "100" overflowToDisk = "false" diskPersistent = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "180" memoryStoreEvictionPolicy = "LRU" /> <!-- maxElementsInMemory 內存中最大緩存對象數,看著自己的heap大小來搞 --> <!-- eternal:true表示對象永不過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認為false --> <!-- maxElementsOnDisk:硬盤中最大緩存對象數,若是0表示無窮大 --> <!-- overflowToDisk:true表示當內存緩存的對象數目達到了maxElementsInMemory界限后, 會把溢出的對象寫到硬盤緩存中。注意:如果緩存的對象要寫入到硬盤中的話,則該對象必須實現了Serializable接口才行。--> <!-- diskSpoolBufferSizeMB:磁盤緩存區大小,默認為30MB。每個Cache都應該有自己的一個緩存區。--> <!-- diskPersistent:是否緩存虛擬機重啟期數據 --> <!-- diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認為120秒 --> <!-- timeToIdleSeconds: 設定允許對象處于空閑狀態的最長時間,以秒為單位。當對象自從最近一次被訪問后, 如果處于空閑狀態的時間超過了timeToIdleSeconds屬性值,這個對象就會過期, EHCache將把它從緩存中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0, 則表示對象可以無限期地處于空閑狀態 --> <!-- timeToLiveSeconds:設定對象允許存在于緩存中的最長時間,以秒為單位。當對象自從被存放到緩存中后, 如果處于緩存中的時間超過了 timeToLiveSeconds屬性值,這個對象就會過期, EHCache將把它從緩存中清除。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0, 則表示對象可以無限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有意義 --> <!-- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時, Ehcache將會根據指定的策略去清理內存。可選策略有:LRU(最近最少使用,默認策略)、 FIFO(先進先出)、LFU(最少訪問次數)。--> </ ehcache > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000013269653