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

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

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

标签:Mybatis,缓存,ehcache
0
投稿

猜你喜欢

  • Maven的porfile与SpringBoot的profile结合使用案例详解

    2023-11-14 00:07:59
  • Java实现年兽大作战游戏详解

    2023-11-08 04:28:05
  • Java数据结构之常见排序算法(下)

    2022-05-03 12:49:51
  • java金钱处理方法实例详解

    2023-05-29 10:38:29
  • SpringDataMongoDB多文档事务的实现

    2023-11-25 06:37:55
  • mybatis-plus Wrapper条件构造器updateForSet更新方式

    2022-12-30 10:40:19
  • 详解Spring Bean的集合注入和自动装配

    2023-02-18 15:02:58
  • Maven本地打包war包实现代码解析

    2021-09-06 12:51:14
  • java 避免出现NullPointerException(空指针)的方法总结

    2022-08-31 04:15:51
  • Spring Cloud 请求重试机制核心代码分析

    2023-04-11 04:00:23
  • jdk源码阅读Collection详解

    2022-07-25 08:57:40
  • springboot返回值转成JSONString的处理方式

    2022-09-27 12:04:46
  • 解决FeignClient发送post请求异常的问题

    2022-08-08 15:55:58
  • Java Apache common-pool对象池介绍

    2022-08-24 22:53:06
  • 一篇文章带你搞定JAVA注解

    2023-03-15 05:22:23
  • 使用webmagic实现爬虫程序示例分享

    2021-10-08 22:10:26
  • Java实现树形结构的示例代码

    2023-07-30 01:05:19
  • 解决nacos项目启动报错:Connection refused: no further informa问题

    2023-08-16 16:55:41
  • 当Mybatis遇上目录树超全完美解决方案

    2021-09-28 16:21:13
  • Java如何把数组转换为ArrayList

    2021-08-14 19:59:45
  • asp之家 软件编程 m.aspxhome.com