SpringBoot整合Ehcache

上一篇 <<<Ehcache基礎(chǔ)知識(shí)
下一篇 >>>Redis的5種數(shù)據(jù)類型


代碼示例

<!--開啟 cache 緩存 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache緩存 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.9.1</version><!--$NO-MVN-MAN-VER$ -->
</dependency>
  cache:
    type: ehcache
    ehcache:
      config: classpath:app1_ehcache.xml
app1_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="java.io.tmpdir/ehcache-rmi-4000" />

    <!-- 多臺(tái)機(jī)器配置 rmiUrls=//192.168.8.32:400002/demoCache|//192.168.5.231:400003/demoCache -->
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:5000/userCache">
    </cacheManagerPeerProviderFactory>
    <!-- 配置 rmi 集群模式 -->
    <cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=127.0.0.1,port=4000,socketTimeoutMillis=120000" />

    <!-- 多播方式配置 搜索某個(gè)網(wǎng)段上的緩存 timeToLive 0是限制在同一個(gè)服務(wù)器 1是限制在同一個(gè)子網(wǎng) 32是限制在同一個(gè)網(wǎng)站 64是限制在同一個(gè)region 
        128是限制在同一個(gè)大洲 255是不限制 <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
        properties="peerDiscovery=automatic, multicastGroupAddress=224.1.1.1, multicastGroupPort=40000, 
        timeToLive=32" /> -->

    <!-- 默認(rèn)緩存 -->
    <defaultCache maxElementsInMemory="1000" eternal="true"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="true" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <!-- demo緩存 -->
    <cache name="userCache" maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
        <!-- 用于在初始化緩存,以及自動(dòng)設(shè)置 -->
        <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
    </cache>
</ehcache>
@EnableCaching 開啟ehcache緩存模式

@CacheConfig(cacheNames = "userCache")
public interface UserMapper {
    @Select("SELECT ID ,NAME,AGE FROM users where id=#{id}")
    @Cacheable
    List<Users> getUser(@Param("id") Long id);
}
@Cacheable  加了該注解的方法表示可以緩存
@CacheConfig 表示創(chuàng)建緩存配置,Key為userCache

Ehcache配置說明

1、diskStore :指定數(shù)據(jù)(.data and .index)存儲(chǔ)位置,可指定磁盤中的文件夾位置期 The diskStore element is optional. It must be configured if you have overflowToDisk or diskPersistent enabled for any cache. If it is not configured, a warning will be issues and java.io.tmpdir will be used.
2、defaultCache : 默認(rèn)的管理策略
Ehcache 使用Map集合實(shí)現(xiàn)的 element 其實(shí)就是 key 和value
一)、以下屬性是必須的:
  1、name: Cache的名稱,必須是唯一的(ehcache會(huì)把這個(gè)cache放到HashMap里)。
  2、maxElementsInMemory:在內(nèi)存中緩存的element的最大數(shù)目。
  3、maxElementsOnDisk:在磁盤上緩存的element的最大數(shù)目,默認(rèn)值為0,表示不限制。
  4、eternal:設(shè)定緩存的elements是否永遠(yuǎn)不過期。如果為true,則緩存的數(shù)據(jù)始終有效,如果為false那么還要根據(jù)timeToIdleSeconds,timeToLiveSeconds判斷。
  5、overflowToDisk: 如果內(nèi)存中數(shù)據(jù)超過內(nèi)存限制,是否要緩存到磁盤上。
二)、以下屬性是可選的:
  1、timeToIdleSeconds: 對(duì)象空閑時(shí)間,指對(duì)象在多長(zhǎng)時(shí)間沒有被訪問就會(huì)失效。只對(duì)eternal為false的有效。默認(rèn)值0,表示一直可以訪問。
  2、timeToLiveSeconds: 對(duì)象存活時(shí)間,指對(duì)象從創(chuàng)建到失效所需要的時(shí)間。只對(duì)eternal為false的有效。默認(rèn)值0,表示一直可以訪問。
  3、diskPersistent: 是否在磁盤上持久化。指重啟jvm后,數(shù)據(jù)是否有效。默認(rèn)為false。
  4、diskExpiryThreadIntervalSeconds: 對(duì)象檢測(cè)線程運(yùn)行時(shí)間間隔。標(biāo)識(shí)對(duì)象狀態(tài)的線程多長(zhǎng)時(shí)間運(yùn)行一次。
  5、diskSpoolBufferSizeMB: DiskStore使用的磁盤大小,默認(rèn)值30MB。每個(gè)cache使用各自的DiskStore。
  6、memoryStoreEvictionPolicy: 如果內(nèi)存中數(shù)據(jù)超過內(nèi)存限制,向磁盤緩存時(shí)的策略。默認(rèn)值LRU,可選FIFO、LFU。


推薦閱讀:
<<<分布式緩存與本地緩存的區(qū)別
<<<Ehcache基礎(chǔ)知識(shí)
<<<Redis的5種數(shù)據(jù)類型
<<<Redis存放實(shí)體對(duì)象的方式及區(qū)別
<<<Redis的應(yīng)用場(chǎng)景匯總
<<<Redis高效及線程安全的真正原因
<<<Redis為啥要分為16個(gè)庫(kù)
<<<RDB和AOF持久化方式的區(qū)別
<<<Redis與數(shù)據(jù)庫(kù)的一致性解決方案
<<<SpringBoot整合Redis的注解版本完成數(shù)據(jù)緩存
<<<Redis的淘汰策略
<<<Redis的事務(wù)操作(Mult和Watch)知識(shí)點(diǎn)
<<<Redis的過期機(jī)制使用場(chǎng)景示例
<<<Redis實(shí)現(xiàn)分布式鎖的原理分析
<<<Redis分布式鎖的實(shí)現(xiàn)代碼示例
<<<使用Redisson工具實(shí)現(xiàn)分布式鎖
<<<Redis集群模式之主從復(fù)制原理及存在的缺陷
<<<Redis集群模式之哨兵模式
<<<Redis集群模式之Cluster去中心化分片集群
<<<Linux環(huán)境下安裝單機(jī)Redis
<<<Redis Cluster集群環(huán)境搭建
<<<Redis Cluster如何動(dòng)態(tài)擴(kuò)容與縮容
<<<Redis Cluster主從節(jié)點(diǎn)自動(dòng)切換
<<<Redis集群模式的類型和缺陷匯總
<<<Redis緩存的穿透、擊穿和雪崩效應(yīng)
<<<Redis解決穿透擊穿問題時(shí)使用的布隆過濾器知識(shí)點(diǎn)
<<<Redis與MySQL的數(shù)據(jù)同步解決方案
<<<阿里云的Canal框架實(shí)現(xiàn)Redis與Mysql同步原理及代碼示例
<<<阿里云的Canal框架配置
<<<Redis官方提出的redlock分布式鎖
<<<Redis的調(diào)優(yōu)設(shè)置
<<<Redis常見問題匯總

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容