Spring Boot 快速集成 Ehcache 3
前言
在互联网服务端架构中,缓存的作用是尤为重要的,无论是基于服务器的内存缓存如 Redis,还是 基于 JVM 的内存缓存如 Ehcache ,在高并发场景中承载着巨大的流量,本文主要针对 JVM 内存框架 Ehcache 3 进行简单地练习,基于Spring Boot 集成 Ehcache 3 搭建一个简单的项目,来实现程序的内存缓存功能支持。
正文
Ehcache 3
Ehcache 是一个开源,具有高性能的 Java 缓存库,由于使用简单,扩展性强,是使用最广泛的 Java 缓存框架,同时具备了内存缓存和磁盘缓存的能力,最新的版本是 Ehcache 3.6。
集成步骤
-
首先创建一个基本的 Spring Boot 程序取名为
springboot-ehcache
,(版本为 2.1.3,以 maven 作为构建工具,不选择任何依赖。(本项目采用 IDEA 2018.5) -
在项目的 pom.xml 里添加 Ehcache 3 依赖,选择合适的版本,这里采用了3.0.0。
pom.xml
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.0.0</version>
</dependency>
-
在项目的 pom.xml 里添加 JSR-107 API 依赖
关于 JSR-107 API:Java 缓存规范的文档 API,类似 JDBC 规范。
pom.xml
<dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency>
-
添加 Spring Boot 依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>