1. 程式人生 > >SpringBoot入門六,添加ehcache緩存

SpringBoot入門六,添加ehcache緩存

fig 根據 inter mls 永久 緩沖 ict tex policy

1.pom.xml文件添加引用包
<!-- 開啟cache緩存支持 -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- 引入ehcache支持 -->
<dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
</dependency>

2.添加ehcache的配置文件ehcache.xml(這個名字最好不要動)文件到resources文件中

如果是根目錄下,springboot的配置文件不用做任何操作
如果是在非根目錄下springboot的配置文件中需要指定具體的文件位置,如下:

<!-- 指定配置文件位置(不寫則直接從classpath根目錄下獲取ehcache.xml的文件) -->
spring.cache.ehcache.config=classpath:ehcache/ehcache.xml

ehcache.xml文件內容

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="50000"
        maxElementsOnDisk="100000"
        eternal="true"
        overflowToDisk="true"
        diskPersistent="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        diskSpoolBufferSizeMB="50"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LFU"
        />

    <cache name="demoCache"
        maxElementsInMemory="1"
        maxElementsOnDisk="100000"
        eternal="false"
        overflowToDisk="true"
        diskPersistent="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="30"
        diskSpoolBufferSizeMB="50"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="FIFO"
        />

    <cache name="demoCache02"
        maxElementsInMemory="1"
        maxElementsOnDisk="100000"
        eternal="false"
        overflowToDisk="true"
        diskPersistent="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        diskSpoolBufferSizeMB="50"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="FIFO"
        />
</ehcache>

<!--  
    diskStore:設置緩存文件 .data 的創建路徑
                user.home – 用戶主目錄;
                user.dir – 用戶當前工作目錄
                java.io.tmpdir – 默認臨時文件路徑
    name:緩存名稱.  
    maxElementsInMemory:緩存最大個數.
    maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大.  
    eternal="false":緩存中對象是否為永久的,如果為true,超時設置將被忽略,對象從不過期 
    overflowToDisk="true":當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中.  
    diskPersistent:是否緩存虛擬機重啟期數據,默認為false.  
    timeToIdleSeconds:設置對象在失效前的允許閑置時間(單位:秒),當緩存閑置 n 秒後銷毀.
                            僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大.  
    timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒),從開始創建的時間算起,當緩存存活 n 秒後銷毀.
                            最大時間介於創建時間和失效時間之間.僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大.  
    diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小.默認是30MB.每個Cache都應該有自己的一個緩沖區.  
    diskExpiryThreadIntervalSeconds:對象檢測線程運行時間間隔.標識對象狀態的線程多長時間運行一次.  
    memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存.
                                    默認策略是LRU(最近最少使用).你可以設置為FIFO(先進先出)或是LFU(較少使用).  
    clearOnFlush:內存數量最大時是否清除.  
        -->  

3.springboot的啟動類中添加@EnableCaching 註解

技術分享圖片

4.在需要使用的地方加上ehcache的註解即可

技術分享圖片

SpringBoot入門六,添加ehcache緩存