目录
缓存支持
Spring 框架只提供抽象,不提供具体的缓存存储,底层需要依赖第三方存储组件,如果当前应用没有注册CacheManager 或者 CacheResolver 实例,Spring Boot 会按以下缓存组件的顺序来检测:
- Generic
- JCache (SR-107)(EhCache 3, Hazelcast, Infinispan 等等)
- Hazelcast
- lnfinispan
- Couchbase
- redis
........
Spring Boot的自动配置类为:CacheAutoConfiguration,在 CacheAutoConfiguration 缓存自动配置类中引入了 CacheConfigurationImportSelector 配置选择器:
@AutoConfiguration(after = { CouchbaseDataAutoConfiguration.class, HazelcastAutoConfiguration.class,
HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class })
@ConditionalOnClass(CacheManager.class)
@ConditionalOnBean(CacheAspectSupport.class)
@ConditionalOnMissingBean(value = CacheManager.class, name = "cacheResolver")
@EnableConfigurationProperties(CacheProperties.class)
@Import({ CacheConfigurationImportSelector.class, CacheManagerEntityManagerFactoryDependsOnPostProcessor.class })
public class CacheAutoConfiguration {
...
static class CacheConfigurationImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
CacheType[] types = CacheType.values();
String[] imports = new String[types.length];
for (int i = 0; i < types.length; i++) {
imports[i] = CacheConfigurations.getConfigurationClass(types[i]);
}
return imports;
}
}
该选择器通过 CacheConfiqurations 缓存配置类来选择对应缓存组件的配置类:
final class CacheConfigurations {
private static final Map<CacheType, String> MAPPINGS;
static {
Map<CacheType, String> mappings = new EnumMap<>(CacheType.class);
mappings.put(CacheType.GENERIC, GenericCacheConfiguration.class.getName());
mappings.put(CacheType.HAZELCAST, HazelcastCacheConfiguration.class.getName());
mappings.put(CacheType.INFINISPAN, InfinispanCacheConfiguration.class.getName());
mappings.put(CacheType.JCACHE, JCacheCacheConfiguration.class.getName());
mappings.put(CacheType.COUCHBASE, CouchbaseCacheConfiguration.class.getName());
mappings.put(CacheType.REDIS, RedisCacheConfiguration.class.getName());
mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class.getName());
mappings.put(CacheType.CACHE2K, Cache2kCacheConfiguration.class.getName());
mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class.getName());
mappings.put(CacheType.NONE, NoOpCacheConfiguration.class.getName());
MAPPINGS = Collections.unmodifiableMap(mappings);
}
...代码...
}
缓存集成
依赖配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
加入依赖后,通过使用@EnableCaching
注解来启用缓存功能。通过参数type指定要使用的缓存组件,以redis为示例:
spring:
cache:
type: redis
禁用缓存
spring:
cache:
type: none
缓存类型type的值
GENERIC
: 通用缓存类型HAZELCAST
: Hazelcast 缓存类型INFINISPAN
: Infinispan 缓存类型JCACHE
: JCache 缓存类型COUCHBASE
: Couchbase 缓存类型REDIS
: Redis 缓存类型CAFFEINE
: Caffeine 缓存类型CACHE2K
: Cache2k 缓存类型SIMPLE
: 简单缓存类型NONE
: 无缓存类型
redis缓存集成
在Spring Boot 2.7中使用@EnableCaching注解启用缓存功能时,如果你想使用Redis作为缓存存储,你需要在配置文件中指定Redis的相关配置。
application.properties或application.yml文件中添加以下配置:
spring:
cache:
type: redis # 指定缓存使用redis
redis:
host: 127.0.0.1 # Redis服务器地址
database: 1 # Redis数据库索引(默认为0)
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
依赖配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置缓存管理器:
@Configuration
@EnableCaching
public class RedisTemplateConfiguration {
/**
* 默认过期时长,单位:秒
*/
@Getter
private long expire = 60 * 60 * 24;
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.java());
redisTemplate.setHashValueSerializer(RedisSerializer.java());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}