SpringBoot2整合Ehcache組件實現(xiàn)輕量級緩存管理
Hibernate三級緩存機制簡介:
一級緩存:基于Session級別分配一塊緩存空間,緩存訪問的對象信息。Session關(guān)閉后會自動清除緩存。
二級緩存:是SessionFactory對象緩存,可以被創(chuàng)建出的多個 Session 對象共享,二級緩存默認是關(guān)閉的,如果要使用需要手動開啟,并且依賴EhCache組件。
三級緩存:查詢緩存,配置開啟該緩存的情況下,重復(fù)使用一個sql查詢某個范圍內(nèi)的數(shù)據(jù),會進行緩存。
EhCache緩存特點 快速,簡單,并且提供多種緩存策略; 緩存數(shù)據(jù)有兩級:內(nèi)存和磁盤,無需擔心容量問題; 緩存數(shù)據(jù)會在虛擬機重啟的過程中寫入磁盤; 可以通過RMI、可插入API等方式進行分布式緩存; 具有緩存和緩存管理器的偵聽接口; 支持多緩存管理器實例,以及一個實例的多個緩存區(qū)域; 提供Hibernate的緩存實現(xiàn);對比Redis緩存Ehcache:直接在Jvm虛擬機中緩存,速度快,效率高,不適合處理大規(guī)模緩存數(shù)據(jù),在分布式環(huán)境下,緩存數(shù)據(jù)共享操作復(fù)雜;
Redis:作為獨立的緩存中間件,在分布式緩存系統(tǒng)中非常好用,緩存數(shù)據(jù)共享,有效支撐大量數(shù)據(jù)緩存,支持哨兵模式,或者集群模式的高可用成熟方案;
二、集成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、加載配置
基礎(chǔ)配置
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'> <!-- 操作系統(tǒng)緩存的臨時目錄,內(nèi)存滿后寫入該目錄 --> <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>
配置參數(shù)說明
maxElementsOnDisk:磁盤緩存中最多可以存放的元素數(shù)量; eternal:緩存中對象是否永久有效; timeToIdleSeconds:當eternal=false時使用,緩存數(shù)據(jù)有效期(單位:秒),時間段內(nèi)沒有訪問該元素,將被清除; timeToLiveSeconds:緩存數(shù)據(jù)的存活時間; maxElementsInMemory:內(nèi)存中最多可以存放的元素數(shù)量,overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中,若overflowToDisk=false,則根據(jù)memoryStoreEvictionPolicy策略替換Cache中原有的元素; diskExpiryThreadIntervalSeconds:磁盤緩存的清理線程運行間隔; memoryStoreEvictionPolicy:緩存釋放策略,LRU會優(yōu)先清理最少使用的緩存; localTempSwap:持久化策略,當堆內(nèi)存或者非堆內(nèi)存里面的元素已經(jīng)滿了的時候,將其中的元素臨時的存放在磁盤上,重啟后就會消失;三、注解用法@Servicepublic class CacheService { private static final Logger LOGGER = LoggerFactory.getLogger(CacheService.class); @Resource private UserMapper userMapper ; @Cacheable(value='userEntity') // 在緩存有效期內(nèi),首次查詢才訪問數(shù)據(jù)庫 public UserEntity getById (Integer id){// 通過日志,標識方法是否執(zhí)行LOGGER.info('getById...'+id);return userMapper.selectById(id) ; } @CacheEvict(value='userEntity',key = '#id') //該ID數(shù)據(jù)更新,清空該ID緩存 public void updateUser(Integer id) {UserEntity user = new UserEntity() ;user.setId(id);user.setUserName('myCache');userMapper.updateById(user); }}
@Cacheable:注解標記在一個方法上,也可以標記在一個類上,標記在一個方法上表示該方法支持緩存,該方法被調(diào)用后將其返回值緩存起來,下次同樣的請求參數(shù)執(zhí)行該方法時可以直接從緩存中獲取結(jié)果,而不需要再次執(zhí)行該方法。
@CacheEvict:注解標記在需要清除緩存元素的方法或類上的,當標記在一個類上時表示其中所有的方法的執(zhí)行都會觸發(fā)緩存的清除操作,并且可以按照指定屬性清除。
四、源代碼地址GitHub·地址https://github.com/cicadasmile/middle-ware-parentGitEE·地址https://gitee.com/cicadasmile/middle-ware-parent
以上就是SpringBoot2整合Ehcache組件實現(xiàn)輕量級緩存管理的詳細內(nèi)容,更多關(guān)于SpringBoot2 整合Ehcache組件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
