【Mybatis】Mybatis整合第三方缓存ehcache

1. Mybatis整合第三方缓存原理图:

在这里插入图片描述

  1. 客户从数据库获取数据视为一次会话,抽象为sqlSession对象
  2. 一个Excutor包含增删改查的操作;
  3. CachingExcutor是对Excutor的包装,此处相当于代理模式
  4. 当有会话时,先访问CachingExcutor对象,CachingExcutor先从二级缓存查找数据,如果有就直接返回;如果没有,就进入Excutor的一级缓存,如果还是没有就执行Excutor的增删改查返回结果,并将结果保存至缓存中,同一个sqlSession再次访问就可以从一级缓存中取了;
  5. 由于mybatis的缓存只用了map实现,所以mybatis允许缓存由第三方缓存来实现,并定义了cache接口,第三方只要实现该接口即可,和mybatis整合在一起后由mybatis在程序中进行调用;

2. Maven导jar包

在全局配置文件中添加以下依赖:

导入ehcache核心包和Mybatis集成包

https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core

https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.11</version>
</dependency>
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.2.0</version>
</dependency>

导入第三方依赖的日志包

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>

3. 配置ehcache.xml

在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">
    <!-- 磁盘保存路径 -->
    <diskStore path="D:\Study\MybatisStudy\ehcache" />
    <defaultCache
            maxElementsInMemory="1"
            maxElementsOnDisk="10000000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>
</ehcache>

        <!-- 属性说明:
         diskStore:当内存中不够存储时,存储到指定数据在磁盘中的存储位置。
         defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略

        以下属性是必须的:
         maxElementsInMemory - 在内存中缓存的element的最大数目
         maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
         eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
         overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
         
        以下属性是可选的:
         timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
         timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大 diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
         diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
         diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
         memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
        -->

设置映射文件

<mapper namespace="com.mj.dao.IUserDao">
 <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
</mapper>

其它映射文件也需要使用该第三方缓存可以如上进行同样的配置,也可以如下设置:

<!--<cache-ref namespace="全类名"/>-->
<cache-ref namespace="com.mj.dao.IUserDao"/>

表示当前Dao与指定Dao共用一块缓存。

转载链接:https://blog.csdn.net/jinhaijing/article/details/84255107

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值