Spring Cache
Spring Cache是Spring框架提供的缓存抽象层,通过注解和自动化配置,简化应用中对缓存的操作,支持多种缓存实现(如Redis、Ehcache、Caffeine)。
1. 核心特性
- **声明式缓存:**通过注解(如@Cacheable、@CacheEvict)声明缓存行为,无需手动编写缓存逻辑。
- **多缓存实现支持:**兼容Redis、Ehcache、Caffeine等缓存工具,通过统一接口切换实现。
- **与Spring无缝集成:**基于AOP动态代理,拦截方法调用自动处理缓存。
- **灵活的缓存策略:**支持条件缓存(condition)、缓存键生成(key)、缓存过期等配置。
2. 核心注解
| 注解 | 作用 | 常用参数 | 示例 |
| @EnableCaching | 开启缓存注解功能,通常加在启动类上 | | |
| @Cacheable | 方法结果缓存。在方法执行前先查询缓存中是否有数据,如果有数据则直接返回缓存数据;如果没有缓存数据,调用方法并将方法返回值放到缓存中 | value(缓存名)、key(键)、condition(条件) | 缓存数据库查询结果 |
| @CachePut | 更新缓存,将方法的返回值放到缓存中 | value(缓存名)、key(键)、condition(条件) | 数据更新后刷新缓存 |
@CacheEvict | 删除缓存,将一条或多条数据从缓存中删除 | allEntries(清空所有键)、beforeInvocation(执行前删除) | 数据清除时删除缓存 |
---|
1. @Cacheable:
**作用:**标记方法的结果需要被缓存。当方法被调用时,先检查缓存是否存在对应键值,若存在则直接返回缓存值,否则执行方法并将结果存入缓存。
**使用场景:**查询操作(如数据库查询、复杂计算等)。
示例:
java
@Cacheable(value = "userCache", key = "#userId", condition = "#userId != null")
public User getUserById(Long userId) {
return userRepository.findById(userId).orElse(null);
}
2. @CachePut:
**作用:**更新缓存。无论缓存是否存在,都会执行方法,并将结果更新到缓存中。
**适用场景:**新增或更新操作(如更新用户信息后同步缓存)。
示例:
java
@CachePut(value = "userCache", key = "#user.id")
public User updateUser(User user) {
return userRepository.save(user);
}
3. @CacheEvict
**作用:**删除缓存。根据条件清除指定键或整个缓存区的数据。
**适用场景:**删除操作(如用户删除后清理缓存)。
示例:
java
@CacheEvict(value = "userCache", key = "#userId")
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
}
// 清空整个缓存区
@CacheEvict(value = "userCache", allEntries = true)
public void clearAllUserCache() {}
3.使用步骤(以Redis为例)
添加依赖:
XML
<!-- Spring Boot Starter Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 集成 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置缓存类型与Redis:
XML
# application.properties
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
# 可选:设置缓存过期时间(单位:毫秒)
spring.cache.redis.time-to-live=60000
**启用缓存:**在启动类添加@EnableCaching
java
@SpringBootApplication
@EnableCaching
public class MyApp { ... }
在Service层使用注解:
java
// 仅当参数id>10时缓存
@Cacheable(value = "users", condition = "#id > 10")
// 结果不为null时缓存
@Cacheable(value = "users", unless = "#result == null")
4. 缓存键与条件控制
自定义缓存键(SpEL表达式)
java
@Cacheable(value = "orders", key = "#userId + ':' + #status")
public List<Order> getOrdersByUserAndStatus(Long userId, String status) { ... }
条件缓存(condition和unless)
java
// 仅当参数id>10时缓存
@Cacheable(value = "users", condition = "#id > 10")
// 结果不为null时缓存
@Cacheable(value = "users", unless = "#result == null")
5. 适用场景
- **高频读低频写:**如商品详情页、用户信息查询。
- **耗时计算:**缓存复杂计算结果(如报表生成)。
- **API限流:**缓存接口调用次数。
- **会话管理:**分布式环境下用户状态缓存。
Spring Cache通过简化缓存逻辑与代码解耦,显著提升了开发效率。结合Redis等高性能缓存工具,能够轻松应对高并发场景。