mybatis整合ehcache

知识点:mybatis整合encache缓存框架,缓存从数据库中,查询的数据,不使用mybatis自带的二级缓存

补充:github上Mybatis Ehcache 适配器包说明地址:http://www.mybatis.org/ehcache-cache/index.html

 

(1)向pom.xm中导入缓存依赖包,和适配器包

<!--ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

<!--Mybatis Ehcache 适配器包-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>

(2)向mapper.xml中,添加配置
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache> //调用Mybatis Ehcache 适配器包中的接口,实现缓存功能
//补充:其他mapper.xml中,再次使用缓存,直接使用
<!--引用缓存,namespace:指定和哪个名称空间下的缓存一样-->
<cache-ref namespace="com.xxMapper"></cache-ref>
<!--<cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024"></cache>-->//区别mybatis自带的二级缓存配置


(3)加入ehcache.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="true" monitoring="autodetect" dynamicConfig="true">

<!-- name:缓存名称
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
maxElementsInMemory:内存缓存最大个数
maxEntriesLocalDisk:当maxElementsInMemory已达到最大值时,写入磁盘
timeToIdleSeconds:缓存对象失效前的闲置时间 默认值为:0 闲置时间无穷大
timeToLiveSeconds:缓存对象最大的有效时间(TTL) 默认值为:0 闲置时间无穷大
diskExpiryThreadIntervalSeconds::磁盘失效线程运行时间间隔,默认是120
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。
默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
clearOnFlush:内存数量最大时是否清除
-->

<!-- 磁盘缓存位置 -->
    <diskStore path="E:/nishuai/cache"/>

<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>

</ehcache>
 
 
 

 

posted @ 2018-07-09 14:46  shuaiflying  阅读(752)  评论(0编辑  收藏  举报