本文源碼:GitHub·點這里 || GitEE·點這里
1、基礎簡介
EhCache是一個純Java的進程內緩存框架,具有快速、上手簡單等特點,是Hibernate中默認的緩存提供方。
2、Hibernate緩存
Hibernate三級緩存機制簡介:
一級緩存:基于Session級別分配一塊緩存空間,緩存訪問的對象信息。Session關閉后會自動清除緩存。
二級緩存:是SessionFactory對象緩存,可以被創建出的多個 Session 對象共享,二級緩存默認是關閉的,如果要使用需要手動開啟,并且依賴EhCache組件。
三級緩存:查詢緩存,配置開啟該緩存的情況下,重復使用一個sql查詢某個范圍內的數據,會進行緩存。
3、EhCache緩存特點
- 快速,簡單,并且提供多種緩存策略;
- 緩存數據有兩級:內存和磁盤,無需擔心容量問題;
- 緩存數據會在虛擬機重啟的過程中寫入磁盤;
- 可以通過RMI、可插入API等方式進行分布式緩存;
- 具有緩存和緩存管理器的偵聽接口;
- 支持多緩存管理器實例,以及一個實例的多個緩存區域;
- 提供Hibernate的緩存實現;
4、對比Redis緩存
Ehcache:直接在Jvm虛擬機中緩存,速度快,效率高,不適合處理大規模緩存數據,在分布式環境下,緩存數據共享操作復雜;
Redis:作為獨立的緩存中間件,在分布式緩存系統中非常好用,緩存數據共享,有效支撐大量數據緩存,支持哨兵模式,或者集群模式的高可用成熟方案;
二、集成SpringBoot框架
1、核心依賴
1
2
3
4
5
6
7
8
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> |
2、加載配置
基礎配置
1
2
3
4
|
spring: cache: ehcache: config: classpath:ehcache.xml |
啟動類注解
1
2
3
4
5
6
7
|
@EnableCaching @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application. class ,args) ; } } |
3、配置詳解
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
|
<ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../config/ehcache.xsd" > <!-- 操作系統緩存的臨時目錄,內存滿后寫入該目錄 --> <diskStore path= "java.io.tmpdir" /> <defaultCache maxElementsInMemory= "1000" eternal= "false" timeToIdleSeconds= "120" timeToLiveSeconds= "120" maxElementsOnDisk= "10000000" diskExpiryThreadIntervalSeconds= "120" memoryStoreEvictionPolicy= "LRU" > <persistence strategy= "localTempSwap" /> </defaultCache> <cache name= "userEntity" maxElementsInMemory= "1000" eternal= "false" timeToIdleSeconds= "120" timeToLiveSeconds= "120" maxElementsOnDisk= "10000000" diskExpiryThreadIntervalSeconds= "120" memoryStoreEvictionPolicy= "LRU" > <persistence strategy= "localTempSwap" /> </cache> </ehcache> |
配置參數說明
maxElementsOnDisk:磁盤緩存中最多可以存放的元素數量;
eternal:緩存中對象是否永久有效;
timeToIdleSeconds:當eternal=false時使用,緩存數據有效期(單位:秒),時間段內沒有訪問該元素,將被清除;
timeToLiveSeconds:緩存數據的存活時間;
maxElementsInMemory:內存中最多可以存放的元素數量,overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中,若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素;
diskExpiryThreadIntervalSeconds:磁盤緩存的清理線程運行間隔;
memoryStoreEvictionPolicy:緩存釋放策略,LRU會優先清理最少使用的緩存;
localTempSwap:持久化策略,當堆內存或者非堆內存里面的元素已經滿了的時候,將其中的元素臨時的存放在磁盤上,重啟后就會消失;
三、注解用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Service public class CacheService { private static final Logger LOGGER = LoggerFactory.getLogger(CacheService. class ); @Resource private UserMapper userMapper ; @Cacheable (value= "userEntity" ) // 在緩存有效期內,首次查詢才訪問數據庫 public UserEntity getById (Integer id){ // 通過日志,標識方法是否執行 LOGGER.info( "getById..." +id); return userMapper.selectById(id) ; } @CacheEvict (value= "userEntity" ,key = "#id" ) //該ID數據更新,清空該ID緩存 public void updateUser(Integer id) { UserEntity user = new UserEntity() ; user.setId(id); user.setUserName( "myCache" ); userMapper.updateById(user); } } |
@Cacheable:注解標記在一個方法上,也可以標記在一個類上,標記在一個方法上表示該方法支持緩存,該方法被調用后將其返回值緩存起來,下次同樣的請求參數執行該方法時可以直接從緩存中獲取結果,而不需要再次執行該方法。
@CacheEvict:注解標記在需要清除緩存元素的方法或類上的,當標記在一個類上時表示其中所有的方法的執行都會觸發緩存的清除操作,并且可以按照指定屬性清除。
四、源代碼地址
GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent
到此這篇關于SpringBoot2 整合Ehcache組件,輕量級緩存管理的文章就介紹到這了,更多相關SpringBoot2 整合Ehcache組件,輕量級緩存管理內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/cicada-smile/p/13464061.html