• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

在Mybatis中使用自定义缓存ehcache的方法

mybatis 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

自定义缓存 – ehcache

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器

1.导包

<!-- 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>

2.在 Mapper.xml 中指定使用 ehcache 缓存实现

<!--在当前 Mapper.xml 中使用二级缓存-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

3.在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 - 默认临时文件路径
 -->

 <!--当二级缓存的对象 超过内存限制时(缓存对象的个数>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"/>

 <!--
  maxElementsInMemory:设置 在内存中缓存 对象的个数
  maxElementsOnDisk:设置 在硬盘中缓存 对象的个数
  eternal:设置缓存是否 永远不过期
  over<p style="color:transparent">本文来源gao!%daima.com搞$代*!码$网3</p>flowToDisk:当系统宕机的时候是否保存到磁盘上
  maxElementsInMemory的时候,是否转移到硬盘中
  timeToIdleSeconds:当2次访问 超过该值的时候,将缓存对象失效
  timeToLiveSeconds:一个缓存对象 最多存放的时间(生命周期)
  diskExpiryThreadIntervalSeconds:设置每隔多长时间,通过一个线程来清理硬盘中的缓存
  clearOnFlush: 内存数量最大时是否清除
  memoryStoreEvictionPolicy:当超过缓存对象的最大值时,处理的策略;LRU (最少使用),FIFO (先进先出), LFU (最少访问次数)
  -->
</ehcache>

到此这篇关于在Mybatis中使用自定义缓存ehcache的方法的文章就介绍到这了,更多相关Mybatis自定义缓存ehcache内容请搜索搞代码以前的文章或继续浏览下面的相关文章希望大家以后多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:在Mybatis中使用自定义缓存ehcache的方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址