Mybatis 整合 ehcache缓存


自定义缓存 - 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 缓存实现

<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

在resource中定义配置文件 ehcache.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!--
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 (最少访问次数)
 -->

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
234
粉丝
11
喜欢
34
收藏
35
排名:793
访问:8674
私信
所有博文
社区赞助商