1. 缓存规范
- CachingProvider:定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。
- CacheManager:定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
- Cache:是一个类似Map的数据结构并临时存储以key为索引的值,一个Cache仅被一个CacheManager所拥有。
- Entry:是一个储存在Cache中的key-value对。
- Expiry:每一个储存在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。

2. 缓存抽象
- Cache接口为缓存的组件规范定义,包含缓存的各种操作集合。
- Cache接口下Spring提供了各种xxxCache的实现,如RedisCache、EhcacheCache、ConcurrentMapCache等。
| 描述 |
---|
Cache | 缓存接口,定义缓存操作,实现有RedisCache、EhcacheCache、ConcurrentMapCache等 |
CacheManager | 缓存管理器,管理各种缓存(Cache)组件 |
@Cacheable | 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 |
@CacheEvict | 清空缓存 |
@CachePut | 保证方法被调用,又希望结果被缓存 |
@EnableCaching | 开启基于注解的缓存 |
keyGenerator | 缓存数据时key的生成策略 |
serialize | 缓存数据时value的序列化策略 |
@Cacheing | 指定多个缓存策略 |
3. 缓存装配
- org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
- org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
- org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
- org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
- org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
- org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
- org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
- org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
- org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration
- org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
4. 简单缓存
使用Spring Boot提供的基于Map的内存级缓存
4.1 依赖管理
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
4.2 开启缓存
@EnableCaching
@SpringBootApplication
public class SimpleCacheServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleCacheServiceApplication.class, args);
}
}
4.3 Cacheable
属性 | 描述 |
---|
cacheNames | 缓存的名字 |
key | 缓存索引,默认使用方法参数值 |
keyGenerator | 缓存索引生成器,与key属性二选一 |
cacheManager | 缓存管理器 |
condition | 满足条件进行缓存 |
unless | 满足条件不进行缓存 |
sync | 是否使用异步,默认为false |
@Override
@Cacheable(cacheNames = "user", key = "#userId")
public User findUserInformationByUserId(Integer userId) {
log.info("通过用户编号查询用户信息......");
return new User(userId, "Jack", LocalDateTime.now(), true);
}
4.4 CachePut
属性 | 描述 |
---|
cacheNames | 缓存的名字 |
key | 缓存索引,默认使用方法参数值 |
keyGenerator | 缓存索引生成器,与key属性二选一 |
cacheManager | 缓存管理器 |
condition | 满足条件进行缓存 |
unless | 满足条件不进行缓存 |
@Override
@CachePut(cacheNames = "user", key = "#user.userId")
public Boolean insertUserInformation(User user) {
log.info("业务层 - 添加用户信息......");
return true;
}
4.5 CacheEvict
属性 | 描述 |
---|
cacheNames | 缓存的名字 |
key | 需要清除缓存的索引,默认使用方法参数值 |
keyGenerator | 缓存索引生成器,与key属性二选一 |
cacheManager | 缓存管理器 |
condition | 满足条件进行清除缓存 |
allEntries | 是否清除全部缓存,默认为false |
beforeInvocation | 是否在目标方法执行之前清除缓存,默认为false |
@Override
@CacheEvict(cacheNames = "user", key = "#userId")
public Boolean deleteUserInformationByUserId(Integer userId) {
log.info("业务层 - 删除用户信息......");
return true;
}
4.6 Caching
4.7 CacheConfig