MyBatis+Ehcache实现二级缓存

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 08:19   23   0

1、引入jar包:

<!-- 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.1.0</version>
        </dependency>

2、在mapper.xml中添加配置:

<!--添加缓存配置-->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache">
        <property name="timeToIdleSeconds" value="60"></property><!--&lt;!&ndash;当缓存闲置60秒后销毁&ndash;&gt;-->
        <property name="timeToLiveSeconds" value="160"></property><!--&lt;!&ndash;缓存存在160秒后销毁&ndash;&gt;-->
        <property name="maxEntriesLocalHeap" value="1000"></property><!--堆内存中最大缓存对象数,0没有限制(必须设置)-->
        <property name="maxEntriesLocalDisk" value="1000000"></property><!-- 硬盘最大缓存个数。 -->
        <!--当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU-->
        <property name="memoryStoreEvictionPolicy" value="LFU"></property>
    </cache>




注意:

(1)当为select语句时:
flushCache默认为false,表示任何时候语句被调用,都不会去清空本地缓存和二级缓存。
useCache默认为true,表示会将本条语句的结果进行二级缓存,所以希望该查询不进行缓存,则手动将useCache=false。
(2)当为insert、update、delete语句时:
flushCache默认为true,表示任何时候语句被调用,都会导致本地缓存和二级缓存被清空。
useCache属性在该情况下没有。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP