Mybatis(六)——整合ehcache(基於springboot)

mybatis提供的原生的二級緩存過於簡陋,使用ehcache代替原生二級緩存。

依賴:

<!-- caching -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 座標 -->
<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache</artifactId>
</dependency>

新增Ehcache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <!--http://www.ehcache.org/ehcache.xsd 可以下載ehcache.xsd-->

    <!-- 磁盤保存路徑,緩存超出就保存在磁盤裏 -->
    <diskStore path="E:\FromC\新桌面\ehcache" />

    <!--默認緩存-->
    <defaultCache
            maxElementsInMemory="10000"
            maxElementsOnDisk="10000000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>
    <!--自定義緩存-->
    <cache name="myToken"
           maxElementsInMemory="100"
           eternal="false"
           timeToIdleSeconds="5400"
           timeToLiveSeconds="5400"
           maxElementsOnDisk="100"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>
<!--
屬性說明:
l diskStore:指定數據在磁盤中的存儲位置。
l defaultCache:當藉助CacheManager.add("demoCache")創建Cache時,EhCache便會採用<defalutCache/>指定的的管理策略

以下屬性是必須的:
l maxElementsInMemory - 在內存中緩存的element的最大數目
l maxElementsOnDisk - 在磁盤上緩存的element的最大數目,若是0表示無窮大
l eternal - 設定緩存的elements是否永遠不過期。如果爲true,則緩存的數據始終有效,如果爲false那麼還要根據timeToIdleSeconds,timeToLiveSeconds判斷
l overflowToDisk - 設定當內存緩存溢出的時候是否將過期的element緩存到磁盤上

以下屬性是可選的:
l timeToIdleSeconds - 當緩存在EhCache中的數據前後兩次訪問的時間超過timeToIdleSeconds的屬性取值時,這些數據便會刪除,默認值是0,也就是可閒置時間無窮大
l timeToLiveSeconds - 緩存element的有效生命期,默認是0.,也就是element存活時間無窮大
diskSpoolBufferSizeMB 這個參數設置DiskStore(磁盤緩存)的緩存區大小.默認是30MB.每個Cache都應該有自己的一個緩衝區.
l diskPersistent - 在VM重啓的時候是否啓用磁盤保存EhCache中的數據,默認是false。
l diskExpiryThreadIntervalSeconds - 磁盤緩存的清理線程運行間隔,默認是120秒。每個120s,相應的線程會進行一次EhCache中數據的清理工作
l memoryStoreEvictionPolicy - 當內存緩存達到最大,有新的element加入的時候, 移除緩存中element的策略。默認是LRU(最近最少使用),可選的有LFU(最不常使用)和FIFO(先進先出)
-->

啓動類加上註解

@EnableCaching

配置文件上加配置

##ehcache緩存配置
spring.cache.ehcache.config=classpath:cache/ehcache.xml

在類或者方法上加入緩存

@Service
public class TblEmployeeServiceimpl implements TblEmployeeService {
    @Autowired
    private TblEmployeeMapper tblEmployeeMapper;
	//在實際的開發中,我們會對用戶信息或者其他做緩存策略,但是當我們修改或者新增數據後,
	//需要清除之前的緩存,重新查詢放入緩存當中
	//清除myToken緩存中的數據
    @CacheEvict(value = "myToken", allEntries = true)
    @Override
    public void insert(TblEmployeePO tblEmployeePO) {
                tblEmployeeMapper.insert(tblEmployeePO);
    }
    //使用myToken緩存
    @Cacheable(value = "myToken")
    @Override
    public TblEmployeePO select(Integer id) {
        TblEmployeePO tblEmployeePO = tblEmployeeMapper.selectByPrimaryKey(id);
        return  tblEmployeePO;
    }
}

注意:TblEmployeePO 要序列化

測試:

訪問:http://localhost:8080/select?id=1,無論當問多少次,日誌只在第一次訪問時,打印一次。說明第二次以後就走了緩存。
在這裏插入圖片描述
訪問:http://localhost:8080/insert,新增用戶,把緩存給清了,然後再訪問select接口。
可以看到日誌打印是這樣的:說明了在新增用戶時確實清除了之前的緩存,重新查詢放入緩存,之後就直接從緩存中獲取數據即可。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章