本文章摘自 Java技术小馆https://www.yuque.com/jtostring/am5oq3/ac34uu2liy50t042?singleDoc#LyUGd
在现代的微服务架构中,缓存已经成为提升系统性能、降低数据库压力和提高响应速度的关键技术之一。对于Java开发者而言,Spring Boot作为一种开发框架,不仅提供了灵活的缓存机制,而且通过与一些缓存库(如Caffeine)结合,能够进一步优化应用性能。
1. 什么是Caffeine缓存?
Caffeine是一个高性能的Java缓存库,它的主要特点包括:
- 高性能:Caffeine具有极快的读写性能,适合高并发的场景。
- 丰富的缓存策略:支持基于时间、大小等多种缓存失效策略。
- 自动回收:内存占用达到限制后会自动回收不常用的缓存。
相较于其他缓存解决方案(如Guava),Caffeine提供了更高的性能和更多的缓存控制选项,适合在Spring Boot中作为本地缓存解决方案。
2. Spring Cache与Caffeine集成
Spring Cache是Spring Framework提供的一种缓存抽象,简化了缓存操作。你可以通过@Cacheable
、@CachePut
、@CacheEvict
等注解轻松地对方法进行缓存操作,而不需要手动管理缓存的存取。
2.1 Caffeine缓存集成的优势
Spring Boot整合Caffeine可以带来以下优势:
- 性能提升:Caffeine缓存对常用数据的快速访问能够大幅提高应用性能,减少数据库负载。
- 内存管理:Caffeine通过LRU(最近最少使用)策略、最大缓存大小限制、缓存过期等机制自动管理内存,避免内存泄漏。
- 易于集成:Spring Cache抽象使得与Caffeine的集成非常简单,开发者无需管理缓存的底层实现。
3. 集成步骤
3.1 添加依赖
我们需要在Spring Boot项目的pom.xml
文件中添加Caffeine和Spring Cache相关依赖:
<dependencies>
<!-- Spring Boot Cache依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Caffeine缓存依赖 -->
<dependency>
<groupId>com.github.ben-manes</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
3.2 配置Caffeine缓存
在application.properties
或application.yml
文件中配置Caffeine缓存。比如设置最大缓存大小和过期策略:
# 配置Caffeine缓存
spring.cache.type=caffeine
spring.cache.caffeine.spec=maximumSize=100,expireAfterAccess=600s
这个配置表示缓存最大可以存储100个元素,并且缓存项在600秒后没有被访问则会过期。
3.3 启用缓存支持
在Spring Boot应用的主类或者任意配置类中启用缓存支持。只需要添加@EnableCaching
注解:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching // 启用缓存支持
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
4. 缓存注解的使用
Spring Cache通过注解简化了缓存操作,以下是常用的注解及其功能:
@Cacheable
:用于缓存方法返回值。@CacheEvict
:用于清除缓存。@CachePut
:用于更新缓存。
4.1 @Cacheable
注解
@Cacheable
注解用于缓存方法的返回值。当方法被调用时,Spring会先检查缓存中是否有数据,如果有数据则直接返回缓存值;如果没有,则执行方法并将返回值缓存。
@Cacheable(value = "users", key = "#id")
public User getUserById(Long id) {
return findUserById(id); // 查询数据库
}
4.2 @CacheEvict
注解
@CacheEvict
注解用于清除缓存。它常用于当数据发生变更时,清除相应的缓存。
@CacheEvict(value = "users", key = "#id")
public void updateUser(Long id, String name) {
// 更新用户信息
}
4.3 @CachePut
注解
@CachePut
注解用于更新缓存。与@Cacheable
不同,它不检查缓存,而是每次执行方法后都会更新缓存。
@CachePut(value = "users", key = "#id")
public User updateUser(Long id, String name) {
return updateUserInDatabase(id, name);
}
5. 实战案例:缓存查询结果
假设我们正在开发一个简单的用户查询系统,用户可以通过ID查询用户信息。我们希望缓存查询结果,以避免重复查询数据库。
5.1 定义实体类
定义一个User
实体类,表示用户信息:
public class User {
private Long id;
private String name;
private int age;
// Getters and Setters
}
5.2 创建缓存服务
创建一个UserService
类,模拟数据库查询操作,并使用@Cacheable
注解进行缓存。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
// 模拟数据库查询
public User findUserById(Long id) {
// 模拟查询数据库的操作
try {
Thread.sleep(2000); // 模拟延时
} catch (InterruptedException e) {
e.printStackTrace();
}
return new User(id, "User" + id, 25);
}
// 使用Spring Cache的@Cacheable注解进行缓存
@Cacheable(value = "users", key = "#id")
public User getUserById(Long id) {
return findUserById(id); // 调用数据库查询方法
}
}
5.3 控制器层
创建一个UserController
,通过REST API提供用户查询接口。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id); // 获取用户信息
}
}
5.4 运行与测试
运行这个Spring Boot应用。当你第一次访问/user/{id}
时,Spring会查询数据库(模拟查询),并将结果存入Caffeine缓存中。第二次访问同样的用户时,Spring将直接从缓存中读取数据,从而大大提高了性能。
6. 缓存管理与过期策略
Caffeine提供了丰富的缓存管理策略。通过maximumSize
、expireAfterWrite
、expireAfterAccess
等配置,我们可以精细控制缓存的存活时间和最大容量。
6.1 缓存过期策略
Caffeine支持两种常见的过期策略:
expireAfterWrite
:元素写入缓存后经过一定时间自动过期。expireAfterAccess
:元素在一定时间内没有访问则过期。
例如,在application.properties
中配置:
spring.cache.caffeine.spec=maximumSize=100,expireAfterWrite=3600s
6.2 设置最大缓存大小
为了避免缓存占用过多内存,我们可以设置缓存的最大元素数量。Caffeine会根据LRU策略自动清除最少使用的元素。
spring.cache.caffeine.spec=maximumSize=100
7. 手动清除缓存
在某些情况下,我们可能需要手动清除缓存。例如,当数据发生变更时,我们需要删除旧的缓存数据。Spring提供了@CacheEvict
注解来清除缓存。
@CacheEvict(value = "users", key = "#id")
public void updateUser(Long id, String name) {
// 更新用户信息
}
@CacheEvict
注解可以清除指定缓存的所有数据,或者仅清除某个特定的缓存项。