mybatis開啟二級緩存小記
1.開啟二級緩存
和一級緩存默認開啟不一樣,二級緩存需要我們手動開啟
首先在全局配置文件 mybatis-configuration.xml 文件中加入如下代碼:
<!--開啟二級緩存 -->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
其次在 UserMapper.xml 文件中開啟緩存
<!-- 開啟二級緩存 -->
<cache></cache>
我們可以看到 mapper.xml 文件中就這么一個空標簽<cache/>,其實這里可以配置<cache type="org.apache.ibatis.cache.impl.PerpetualCache"/>,PerpetualCache這個類是mybatis默認實現緩存功能的類。我們不寫type就使用mybatis默認的緩存,也可以去實現 Cache 接口來自定義緩存。
<cache type="org.apache.ibatis.cache.impl.PerpetualCache"></cache>
2.useCache和flushCache
mybatis中還可以配置userCache和flushCache等配置項,userCache是用來設置是否禁用二級緩存的,在statement中設置useCache=false可以禁用當前select語句的二級緩存,即每次查詢都會發出sql去查詢,默認情況是true,即該sql使用二級緩存。
<select id="selectUserByUserId" useCache="false" resultType="com.ys.twocache.User" parameterType="int">
select * from user where id=#{id}
</select>
這種情況是針對每次查詢都需要最新的數據sql,要設置成useCache=false,禁用二級緩存,直接從數據庫中獲取。
在mapper的同一個namespace中,如果有其它insert、update、delete操作數據后需要刷新緩存,如果不執行刷新緩存會出現臟讀。
設置statement配置中的flushCache=”true” 屬性,默認情況下為true,即刷新緩存,如果改成false則不會刷新。使用緩存時如果手動修改數據庫表中的查詢數據會出現臟讀。
<select id="selectUserByUserId" flushCache="true" useCache="false" resultType="com.ys.twocache.User" parameterType="int">
select * from user where id=#{id}
</select>
一般下執行完commit操作都需要刷新緩存,flushCache=true表示刷新緩存,這樣可以避免數據庫臟讀。所以我們不用設置,默認即可。
3.二級緩存整合ehcache
在全局配置文件 mybatis-configuration.xml 開啟緩存
<!--開啟二級緩存 -->
<settings>
<setting name="cacheEnabled" value= "true" />
</settings>
在 xxxMapper.xml 文件中整合 ehcache
將如下的類的全類名寫入<cache type="" ></cache>的type屬性中
<!-- 開啟本mapper的namespace下的二級緩存 type:指定cache接口的實現類的類型,不寫type屬性,mybatis默認使用PerpetualCache 要和ehcache整合,需要配置type為ehcache實現cache接口的類型 -->
<cache type= "org.mybatis.caches.ehcache.EhcacheCache" ></cache>
配置緩存參數
在 classpath 目錄下新建一個 ehcache.xml 文件,並增加如下配置:
<?xml version="1.0" encoding= "UTF-8" ?> <ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../config/ehcache.xsd" >
<diskStore path= "F:\develop\ehcache" />
<defaultCache maxElementsInMemory= "10000" eternal= "false" timeToIdleSeconds= "120" timeToLiveSeconds= "120" maxElementsOnDisk= "10000000" diskExpiryThreadIntervalSeconds= "120" memoryStoreEvictionPolicy= "LRU"> <persistence strategy= "localTempSwap" />
</defaultCache>
</ehcache>
diskStore:指定數據在磁盤中的存儲位置。
defaultCache:當借助CacheManager.add("demoCache")創建Cache時,EhCache便會采用<defalutCache/>指定的的管理策略
以下屬性是必須的:
maxElementsInMemory - 在內存中緩存的element的最大數目
maxElementsOnDisk - 在磁盤上緩存的element的最大數目,若是0表示無窮大
eternal - 設定緩存的elements是否永遠不過期。如果為true,則緩存的數據始終有效,如果為false那么還要根據timeToIdleSeconds,timeToLiveSeconds判斷
overflowToDisk - 設定當內存緩存溢出的時候是否將過期的element緩存到磁盤上
以下屬性是可選的:
timeToIdleSeconds - 當緩存在EhCache中的數據前后兩次訪問的時間超過timeToIdleSeconds的屬性取值時,這些數據便會刪除,默認值是0,也就是可閑置時間無窮大
timeToLiveSeconds - 緩存element的有效生命期,默認是0.,也就是element存活時間無窮大
diskSpoolBufferSizeMB 這個參數設置DiskStore(磁盤緩存)的緩存區大小.默認是30MB.每個Cache都應該有自己的一個緩沖區.
diskPersistent - 在VM重啟的時候是否啟用磁盤保存EhCache中的數據,默認是false。
diskExpiryThreadIntervalSeconds - 磁盤緩存的清理線程運行間隔,默認是120秒。每個120s,相應的線程會進行一次EhCache中數據的清理工作
memoryStoreEvictionPolicy - 當內存緩存達到最大,有新的element加入的時候, 移除緩存中element的策略。默認是LRU(最近最少使用),可選的有LFU(最不常使用)和FIFO(先進先出)
