本文,讲解 Spring Boot 如何集成 EhCache,实现缓存。javascript
博客地址:blog.720ui.com/java
在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门」后,对 Spring Boot 集成缓存机制有必定了解后,咱们来了解下 EhCache 的使用。git
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
(完)
更多精彩文章,尽在「服务端思惟」微信公众号!