Spring Boot 揭祕與實戰(二) 數據緩存篇 - EhCache

本文,講解 Spring Boot 如何集成 EhCache,實現緩存。javascript

博客地址:blog.720ui.com/java

在閱讀「Spring Boot 揭祕與實戰(二) 數據緩存篇 - 快速入門」後,對 Spring Boot 集成緩存機制有必定了解後,咱們來了解下 EhCache 的使用。git

EhCache 集成

EhCache 是一個純 Java 的進程內緩存框架,具備快速、精幹等特色,是 Hibernate 中默認的 CacheProvider。github

在 Spring Boot 中集成 EhCache 很是容易,只須要兩個步驟。spring

首先,在 pom.xml 中增長EhCache 依賴。緩存

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>複製代碼

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

<?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 更深刻的瞭解,能夠參考 www.ehcache.org/ehcache.xml…框架

運行起來,控制檯打印的日誌信息,說明已是EhCacheManager實例,說明 EhCache 開啓成功了。ide

Bean 'cacheManager' of type [class org.springframework.cache.ehcache.EhCacheCacheManager]複製代碼

源代碼

相關示例完整代碼: springboot-action

(完)

更多精彩文章,盡在「服務端思惟」微信公衆號!

相關文章
相關標籤/搜索