自定義快取 - ehcache
Ehcache是一種廣泛使用的開源Java分布式快取,主要面向通用快取,Java EE和輕量級容器
- 導包
<!-- https://mvnrepository.com/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 快取實作
<!--在當前 Mapper.xml 中使用二級快取-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
- 在resource中定義組態檔 ehcache.xml
<?xml version="1.0" encoding="UTF-8" ?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!-- diskStore: 快取路徑, ehcache分為記憶體和磁盤兩級, 此屬性定義磁盤的快取位置 引數: user.home - 用戶主目錄 user.dir - 用戶當前作業目錄 java.io.tmpdir - 默認臨時檔案路徑 --> <!-- java專案 www.fhadmin.org --><!--當二級快取的物件 超過記憶體限制時(快取物件的個數>maxElementsInMemory),存放入的硬碟檔案 --> <diskStore path="./tempdir/Tmp_Ehcache"/> <!--default 默認緩沖策略, 當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:設定 在記憶體中快取 物件的個數 maxElementsOnDisk:設定 在硬碟中快取 物件的個數 eternal:設定快取是否 永遠不過期 overflowToDisk:當系統宕機的時候是否保存到磁盤上 maxElementsInMemory的時候,是否轉移到硬碟中 timeToIdleSeconds:當2次訪問 超過該值的時候,將快取物件失效 timeToLiveSeconds:一個快取物件 最多存放的時間(生命周期) diskExpiryThreadIntervalSeconds:設定每隔多長時間,通過一個執行緒來清理硬碟中的快取 clearOnFlush: 記憶體數量最大時是否清除 memoryStoreEvictionPolicy:當超過快取物件的最大值時,處理的策略;LRU (最少使用),FIFO (先進先出), LFU (最少訪問次數) --> </ehcache>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/297993.html
標籤:其他
上一篇:AOP:面向切片編程
