1. 程式人生 > >Spring Boot 資料快取 - EhCache

Spring Boot 資料快取 - EhCache

EhCache 整合

EhCache 是一個純 Java 的程序內快取框架,具有快速、精幹等特點,是 Hibernate 中預設的 CacheProvider。

在 Spring Boot 中整合 EhCache 非常容易,只需要兩個步驟。

首先,在 pom.xml 中增加 EhCache 依賴。

<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
</dependency>

第二步,在 src/main/resources 目錄下建立 ehcache.xml。

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
  <cache name="ehcache" maxElementsInMemory="1000" timeToLiveSeconds="300"></cache>
</ehcache

其中,maxElementsInMemory,表示快取最大數目。 timeToLiveSeconds: ,表示設定物件在失效前允許存活時間(單位:秒)。

如果想對 ehcache.xml 更深入的瞭解,可以參考 http://www.ehcache.org/ehcache.xml。

執行起來,控制檯列印的日誌資訊,說明已經是EhCacheManager例項,說明 EhCache 開啟成功了。

  1. Bean 'cacheManager' of type [class org.springframework.cache.ehcache.EhCacheCacheManager]