项目配置
1. 依赖
首先搭建一个标准的SpringBoot项目工程,相关版本以及依赖如下
本项目借助SpringBoot 2.2.1.RELEASE
+ maven 3.5.3
+ IDEA
进行开发
<dependencies>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
复制代码
2. 配置
SpringBoot官方对Caffeine的集成,提供了非常好的支持,比如本文介绍的在使用 @Cacheable
注解来处理缓存时,我们无需额外操作,直接在配置文件来实现缓存的指定,以及对应的Caffeine相关配置限定
核心配置如下 application.yml
# 指定全局默认的缓存策略
spring:
cache:
type: caffeine
caffeine:
spec: initialCapacity=10,maximumSize=200,expireAfterWrite=5m
复制代码