分享

Mybatis 整合 ehcache缓存

 青苔IT 2021-09-06

自定义缓存 - ehcache

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

  1. 导包

<!-- https:///artifact/org.mybatis.caches/mybatis-ehcache  --><dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.1.0</version></dependency>
  1. 在 Mapper.xml 中指定使用 ehcache 缓存实现

<!--在当前 Mapper.xml 中使用二级缓存--><cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
  1. 在resource中定义配置文件 ehcache.xml 

<?xml version="1.0" encoding="UTF-8" ?><ehcache xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http:///ehcache.xsd" updateCheck="false">

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多