软件编程
位置:首页>> 软件编程>> java编程>> 在Mybatis中使用自定义缓存ehcache的方法

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

作者:weixin_44953227  发布时间:2022-02-24 17:27:35 

标签:Mybatis,缓存,ehcache

自定义缓存 - 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:设置缓存是否 永远不过期
 overflowToDisk:当系统宕机的时候是否保存到磁盘上
 maxElementsInMemory的时候,是否转移到硬盘中
 timeToIdleSeconds:当2次访问 超过该值的时候,将缓存对象失效
 timeToLiveSeconds:一个缓存对象 最多存放的时间(生命周期)
 diskExpiryThreadIntervalSeconds:设置每隔多长时间,通过一个线程来清理硬盘中的缓存
 clearOnFlush: 内存数量最大时是否清除
 memoryStoreEvictionPolicy:当超过缓存对象的最大值时,处理的策略;LRU (最少使用),FIFO (先进先出), LFU (最少访问次数)
 -->
</ehcache>

来源:https://blog.csdn.net/weixin_44953227/article/details/112801107

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com