1. 程式人生 > >Spring Boot整合Ehcache步驟

Spring Boot整合Ehcache步驟

SpringBoot 整合 Ehcache

1 、修改 pom 檔案

             <groupId>com.bjsxt</groupId>

             <artifactId>23-spring-boot-ehcache</artifactId>

             <version>0.0.1-SNAPSHOT</version>

<dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-cache</artifactId>

</dependency>

2、建立 Ehcache 的配置檔案 檔名:ehcache.xml 位置:src/main/resources/ehcache.xml 

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>

  <!--defaultCache:echcache的預設快取策略  -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <!-- 自定義快取策略 -->
    <cache name="users"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

4、 修改 application.properties 檔案

spring.cache.ehcache.cofnig=ehcache.xml

5、

6、建立業務層:

 

7、 

8、

 

9、

第二次執行的時候直接從快取裡面取。而不需要再次進行查資料庫。