springboot添加cache緩存,並添加自定義緩存配置


maven依賴

        <!--   緩存     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- 引入ehcache支持 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

 

全局配置和具體實現

啟動類上面添加@EnableCaching 注解

@SpringBootApplication
@EnableCaching
public class EpidemicApplication {

    public static void main(String[] args) {
        SpringApplication.run(EpidemicApplication.class, args);
    }

}

 

實現上面添加@Cacheable

@Component
@CacheConfig(cacheNames = "orgCode")
public class OrgCodeDaoImpl implements OrgCodeDao {


    @Autowired
    private OrgCodeRepository orgCodeRepository;



    @Cacheable(value = "orgCodeFindAll")
    @Override
    public List<OrgCode> findAll() {
        return orgCodeRepository.findAll();
    }

}

 

實體類需要實現Serializable

public class OrgCode implements Serializable {

}

自定義配置

resource目錄下添加ehcache.xml,或者在application.properties文件中添加配置文件路徑

spring.cache.ehcache.config=ehcache.xml

 

具體配置文件內容:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://www.ehcache.org/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="orgCodeFindAll"
           maxElementsInMemory="1"
           maxElementsOnDisk="100000"
           eternal="false"
           overflowToDisk="true"
           diskPersistent="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="21600"
           diskSpoolBufferSizeMB="50"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="FIFO"
    />
    
</ehcache>
    
        <!--
http://www.ehcache.org/ehcache.xsd    文件idea報錯,可以訪問地址,將文件下載后引入項目,或者忽略報錯,測試報錯不影響使用
 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:內存數量最大時是否清除. -->

 

附錄

報錯調試

1.

java.lang.IllegalArgumentException: Cannot find cache named '' for Builder[public java.util.List com.avivacofco.epidemic.hr.dao.impl.HrUserDaoImpl.findAll()] caches=[hrFindAll] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

 

解決方案:

在ehcache.xml中添加具體cache配置

cache注解

 

 

 

 其中重要的是兩個屬性 即 value 和 key,其中value必填,並且在ehcache.xml中進行配置,key可以為空,如為空,spring會使用默認策略生成key。

備注:key值邏輯:引用參數使用 #p0 是比較適配的一種方案

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM