mybatis整合ehcache(分散式快取框架)
阿新 • • 發佈:2021-06-29
系統為了提高系統併發,效能、一般對系統進行分散式部署(叢集部署方式)
不使用分佈快取,快取的資料在各各服務單獨儲存,不方便系統 開發。所以要使用分散式快取對快取資料進行集中管理。
mybatis無法實現分散式快取,需要和其它分散式快取框架進行整合
mybatis提供了一個cache介面,如果要實現自己的快取邏輯,實現cache介面開發即可。
mybatis和ehcache整合,mybatis和ehcache整合包中提供了一個cache介面的實現類。
整合步驟
1、加入ehcachejar包
2、配置mapper中cache中的type為ehcache對cache介面的實現型別。
<mapper namespace="com.xxx.mybatis.mapper.UserMapper"> <!-- 開啟此mapper的namespace下的二級快取 type:指定cache介面的實現類的型別,mybatis預設使用PerpetualCache --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
3、配置ehcache.xml並將其放到classpath下
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <!-- 指定資料在磁碟中的儲存位置 --> <diskStore path="D:\DEV_ENV\ehcache" /> <!-- 快取策略 --> <defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000" eternal="false" overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>