SpringBoot-Mybatis-Plus-Ehcache实现二级缓存

Ehcache  简介:

       是现在最流行的纯Java开源缓存框架,配置简单、结构清晰、功能强大,最初知道它,是从Hibernate的缓存开始的, 是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力;

Ehcache 特点 : 

    1,快速; 2,简单; 3,多种缓存策略; 4, 缓存数据有两级:内存和磁盘,因此无需担心容量问题;

    5, 缓存数据在虚拟机重启的过程中写入磁盘; 6,可以通过RMI,可插入API等方式进行分布式缓存;

    7, 具有缓存和缓存管理器的侦听接口; 8, 支持多缓存管理器实例,以及一个实例的多个缓存区域;

Ehcache 三大元素说明:

    1, CacheManager : 缓存管理器,可以通过单例或者多例的方式创建,也是Ehcache的入口类.

    2, Cache : 每个CacheManager可以管理多个Cache,每个Cache可以采用hash的方式管理多个Element.

    3, Element : 用于存放真正缓存内容的.

配置及使用 :

下面是 spring boot + mybatis-plus + mybath-encache 的集成

1,  引入Maven :

        <dependency> <!-- MyBatis-Plus -->
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency> <!-- MP核心库 2.0.8版本以上,CRUD不支持二级缓存 -->
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.0.8</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generate</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency> <!-- 开启二级缓存 -->
		    <groupId>org.mybatis.caches</groupId>
		    <artifactId>mybatis-ehcache</artifactId>
		    <version>1.1.0</version>
		</dependency>

2,  Ehcache配置文件: src/main/resources/config/ehcache-config.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 path="java.io.tmpdir" />

	<!--
        name:缓存名称。
        maxElementsInMemory:缓存最大个数。
        eternal:对象是否永久有效,一但设置了,timeout将不起作用。
        timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
        timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
        overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
        maxElementsOnDisk:硬盘最大缓存个数。
        diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。   
        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
        clearOnFlush:内存数量最大时是否清除。
    -->


	<!-- 默认缓存 -->
	<defaultCache 
		eternal="false" 
		maxElementsInMemory="1000"
		overflowToDisk="false" 
		diskPersistent="false" 
		timeToIdleSeconds="0"
		timeToLiveSeconds="600" 
		memoryStoreEvictionPolicy="LRU" />
	
	<!-- 自定义缓存 -->
	<cache 
		name="testCache"
		eternal="false" 
		maxElementsInMemory="1000"
		overflowToDisk="false" 
		diskPersistent="false" 
		timeToIdleSeconds="5"
		timeToLiveSeconds="5" 
		memoryStoreEvictionPolicy="LRU" />

</ehcache>

3,  引入框架:  在 appilcation.properties 中加入:

spring.cache.ehcache.config=classpath:config/ehcache-config.xml

4,  mybatis-plus : 开启二级缓存 :

mybatis-plus.configuration.cache-enabled=true

5,  mapper.xml 开启二级缓存 :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.gy.fast.module.sys.dao.SysMenuDao">
    
    <cache type="org.mybatis.caches.ehcache.EhcacheCache">
	    <property name="timeToIdleSeconds" value="3600"/>
	    <property name="timeToLiveSeconds" value="3600"/>
	    <property name="maxEntriesLocalHeap" value="1000"/>
	    <property name="maxEntriesLocalDisk" value="10000000"/>
	    <property name="memoryStoreEvictionPolicy" value="LRU"/>
	</cache>

</mapper>

到此, Ehcache配置mybatis二级缓存完成.

注意:  mybatis-pluse封装的CRUD使用二级缓存时 mybatis-plus 版本必须要低于2.0.9, 否则不生效,只有在mapper.xml中有的SQL方法才生效.

猜你喜欢

转载自my.oschina.net/u/3681868/blog/1814216