自定義緩存 - ehcacheEhcache是一種廣泛使用的開源Java分布式緩存,。主要面向通用緩存,Java EE和輕量級容器 導(dǎo)包
<!-- https:///artifact/org.mybatis.caches/mybatis-ehcache --><dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version></dependency> 在 Mapper.xml 中指定使用 ehcache 緩存實現(xiàn)
<!--在當(dāng)前 Mapper.xml 中使用二級緩存--><cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 在resource中定義配置文件 ehcache.xml
<?xml version="1.0" encoding="UTF-8" ?><ehcache xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http:///ehcache.xsd" updateCheck="false">
<!--
diskStore: 緩存路徑, ehcache分為內(nèi)存和磁盤兩級, 此屬性定義磁盤的緩存位置
參數(shù):
user.home - 用戶主目錄
user.dir - 用戶當(dāng)前工作目錄
java.io.tmpdir - 默認臨時文件路徑
--> <!-- java項目 www.fhadmin.org -->
<!--當(dāng)二級緩存的對象 超過內(nèi)存限制時(緩存對象的個數(shù)>maxElementsInMemory),,存放入的硬盤文件 -->
<diskStore path="./tempdir/Tmp_Ehcache"/>
<!--default 默認緩沖策略, 當(dāng)ehcache找不到定義的緩存時, 則使用這個緩存策略, 這個只能定義一個-->
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="cloud_user"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
<!--
java項目 www.fhadmin.cn
maxElementsInMemory:設(shè)置 在內(nèi)存中緩存 對象的個數(shù)
maxElementsOnDisk:設(shè)置 在硬盤中緩存 對象的個數(shù)
eternal:設(shè)置緩存是否 永遠不過期
overflowToDisk:當(dāng)系統(tǒng)宕機的時候是否保存到磁盤上
maxElementsInMemory的時候,是否轉(zhuǎn)移到硬盤中
timeToIdleSeconds:當(dāng)2次訪問 超過該值的時候,,將緩存對象失效
timeToLiveSeconds:一個緩存對象 最多存放的時間(生命周期)
diskExpiryThreadIntervalSeconds:設(shè)置每隔多長時間,,通過一個線程來清理硬盤中的緩存
clearOnFlush: 內(nèi)存數(shù)量最大時是否清除
memoryStoreEvictionPolicy:當(dāng)超過緩存對象的最大值時,,處理的策略;LRU (最少使用),,F(xiàn)IFO (先進先出), LFU (最少訪問次數(shù))
--></ehcache>
|