SpringBoot2整合Ehcache組件實現輕量級緩存管理
Hibernate三級緩存機制簡介:
一級緩存:基于Session級別分配一塊緩存空間,緩存訪問的對象信息。Session關閉后會自動清除緩存。
二級緩存:是SessionFactory對象緩存,可以被創建出的多個 Session 對象共享,二級緩存默認是關閉的,如果要使用需要手動開啟,并且依賴EhCache組件。
三級緩存:查詢緩存,配置開啟該緩存的情況下,重復使用一個sql查詢某個范圍內的數據,會進行緩存。
EhCache緩存特點 快速,簡單,并且提供多種緩存策略; 緩存數據有兩級:內存和磁盤,無需擔心容量問題; 緩存數據會在虛擬機重啟的過程中寫入磁盤; 可以通過RMI、可插入API等方式進行分布式緩存; 具有緩存和緩存管理器的偵聽接口; 支持多緩存管理器實例,以及一個實例的多個緩存區域; 提供Hibernate的緩存實現;對比Redis緩存Ehcache:直接在Jvm虛擬機中緩存,速度快,效率高,不適合處理大規模緩存數據,在分布式環境下,緩存數據共享操作復雜;
Redis:作為獨立的緩存中間件,在分布式緩存系統中非常好用,緩存數據共享,有效支撐大量數據緩存,支持哨兵模式,或者集群模式的高可用成熟方案;
二、集成SpringBoot框架1、核心依賴<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId></dependency>2、加載配置
基礎配置
spring: cache: ehcache: config: classpath:ehcache.xml
啟動類注解
@EnableCaching@SpringBootApplicationpublic class Application { public static void main(String[] args) {SpringApplication.run(Application.class,args) ; }}3、配置詳解
<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:持久化策略,當堆內存或者非堆內存里面的元素已經滿了的時候,將其中的元素臨時的存放在磁盤上,重啟后就會消失;三、注解用法@Servicepublic 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-parentGitEE·地址https://gitee.com/cicadasmile/middle-ware-parent
以上就是SpringBoot2整合Ehcache組件實現輕量級緩存管理的詳細內容,更多關于SpringBoot2 整合Ehcache組件的資料請關注好吧啦網其它相關文章!
相關文章:
