SpringBoot-Mybatis-Plus-Ehcache實現二級緩存

Ehcache  簡介:

       是如今最流行的純Java開源緩存框架,配置簡單、結構清晰、功能強大,最初知道它,是從Hibernate的緩存開始的, 是Hibernate的二級緩存技術之一,能夠把查詢出來的數據存儲在內存或者磁盤,節省下次一樣查詢語句再次查詢數據庫,大幅減輕數據庫壓力;java

Ehcache 特色 : 

    1,快速; 2,簡單; 3,多種緩存策略; 4, 緩存數據有兩級:內存和磁盤,所以無需擔憂容量問題;redis

    5, 緩存數據在虛擬機重啓的過程當中寫入磁盤; 6,能夠經過RMI,可插入API等方式進行分佈式緩存;spring

    7, 具備緩存和緩存管理器的偵聽接口; 8, 支持多緩存管理器實例,以及一個實例的多個緩存區域;數據庫

Ehcache 三大元素說明:

    1, CacheManager : 緩存管理器,能夠經過單例或者多例的方式建立,也是Ehcache的入口類.apache

    2, Cache : 每一個CacheManager能夠管理多個Cache,每一個Cache能夠採用hash的方式管理多個Element.緩存

    3, Element : 用於存放真正緩存內容的.bash

配置及使用 :

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

1,  引入Maven :app

<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/ehcache.xml 框架

         (注意: 文件名,路徑必須是這樣,不然Ehcache不能單獨使用)

<?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:/ehcache.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方法才生效. 

固然咱們永遠都不要相信低版本的可靠性,好比在2.0.8中會因爲缺乏內置類型,而發生返回結果異常. 固然在使用緩存的過程當中,大量的測試是必要的.

這也是我比較糾結的地方, 咱們在項目中最頻繁使用的就是如 mybatis-plus 中能夠構造的簡單數據操做, 比較複雜少用的操做會放到mapper.xml中進行構造, 因此緩存固然緩存訪問較多的數據纔有意義吧, 不然又有什麼用呢.

基於這點呢, 如今最好的方案就是redis緩存啦, 固然redis相對ehcache來講比較龐大,配置較多,速度也慢一點,小點的項目不涉及分佈式的話根本是大才小用啦.

因此這個問題不錯,我目前也是在探索階段,怎樣編寫高效穩定的代碼,怎樣高效穩定的使用緩存,怎樣使用緩存可讓代碼耦合性較低,等等都須要繼續深刻學習. 

我全部的博客都是在工做學習中發現或掌握的技術,記錄下來的主要目的是但願下次複製黏貼不要太難找.哈哈,也但願和我遇到相同問題的同窗能快速解決.

相關文章
相關標籤/搜索