本文源碼: GitHub·點這里 || GitEE·點這里
EhCache是一個純Java的進程內(nèi)緩存框架,具有快速、上手簡單等特點,是Hibernate中默認的緩存提供方。
Hibernate三級緩存機制簡介:
一級緩存:基于Session級別分配一塊緩存空間,緩存訪問的對象信息。Session關閉后會自動清除緩存。
二級緩存:是SessionFactory對象緩存,可以被創(chuàng)建出的多個 Session 對象共享,二級緩存默認是關閉的,如果要使用需要手動開啟,并且依賴EhCache組件。
三級緩存:查詢緩存,配置開啟該緩存的情況下,重復使用一個sql查詢某個范圍內(nèi)的數(shù)據(jù),會進行緩存。
Ehcache:直接在Jvm虛擬機中緩存,速度快,效率高,不適合處理大規(guī)模緩存數(shù)據(jù),在分布式環(huán)境下,緩存數(shù)據(jù)共享操作復雜;
Redis:作為獨立的緩存中間件,在分布式緩存系統(tǒng)中非常好用,緩存數(shù)據(jù)共享,有效支撐大量數(shù)據(jù)緩存,支持哨兵模式,或者集群模式的高可用成熟方案;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
基礎配置
spring:
cache:
ehcache:
config: classpath:ehcache.xml
啟動類注解
@EnableCaching
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args) ;
}
}
<ehcache xmlns:xsi="/tupian/20230522/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)滿了的時候,將其中的元素臨時的存放在磁盤上,重啟后就會消失;
@Service
public 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í)行該方法時可以直接從緩存中獲取結果,而不需要再次執(zhí)行該方法。
@CacheEvict:注解標記在需要清除緩存元素的方法或類上的,當標記在一個類上時表示其中所有的方法的執(zhí)行都會觸發(fā)緩存的清除操作,并且可以按照指定屬性清除。
GitHub·地址
/tupian/20230522/middle-ware-parent
GitEE·地址
/tupian/20230522/middle-ware-parent
分享題目:SpringBoot2整合Ehcache組件,輕量級緩存管理-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article26/cedejg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、動態(tài)網(wǎng)站、網(wǎng)站收錄、做網(wǎng)站、定制開發(fā)、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)