SpringBoot自动配置redis,主要是两个自动配置类
缓存管理器配置类:org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
redis封装了工具RedisTemplate自动配置:org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
缓存管理器CacheManager配置,@Cacheable,@CacheEvict、@CachePut
我们使用@Cacheable,@CacheEvict、@CachePut 这些缓存注解的时候需要配置CacheManager,也就是需要可以参考RedisCacheConfiguration配置
springBoot的缓存管理器CacheManager封装了常用的缓存工具,
内存缓存:基于java8的高性能内存缓存Caffeine;JCache,EhCache noSQL数据库缓存(Couchbase,Redis) 其他:Infinispan 基于Apache 2.0协议的分布式键值存储系统
如果没有默认配置自定义的CacheManager时,RedisCacheConfiguration配置会生效
这个类配置的缓存管理器使用的是 JdkSerializationRedisSerializer 序列化,缓存数据在redis存储是以字节形式存储,不能再redis数据库查看
我们如果要自定义序列化,需要自定义CacheManager 这个Bean
package com.zwh.config.cache; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers; import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import java.util.LinkedHashSet; import java.util.List; /** * redis缓存配置,搭配@Cache()注解使用, 要直接操作Redis缓存配置,请到{@link com.zwh.config.RedisConfig} * 重写org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration * @author monkey * @date 2020/7/17 * @since 1.8 */ @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties({CacheProperties.class}) @EnableCaching public class RedisCacheConfiguration { @Bean RedisCacheManager cacheManager( CacheProperties cacheProperties, ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration, ObjectProvider<RedisCacheManagerBuilderCustomizer> redisCacheManagerBuilderCustomizers, RedisConnectionFactory redisConnectionFactory ) { RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults( determineConfiguration(cacheProperties, redisCacheConfiguration)); List<String> cacheNames = cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); } redisCacheManagerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); return new CacheManagerCustomizers(null).customize(builder.build()); } private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration( CacheProperties cacheProperties, ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration ) { return redisCacheConfiguration.getIfAvailable(() -> createConfiguration(cacheProperties)); } private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration( CacheProperties cacheProperties) { CacheProperties.Redis redisProperties = cacheProperties.getRedis(); org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration .defaultCacheConfig(); config = config.serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()) ); if (redisProperties.getTimeToLive() != null) { config = config.entryTtl(redisProperties.getTimeToLive()); } if (redisProperties.getKeyPrefix() != null) { config = config.prefixKeysWith(redisProperties.getKeyPrefix()); } if (!redisProperties.isCacheNullValues()) { config = config.disableCachingNullValues(); } if (!redisProperties.isUseKeyPrefix()) { config = config.disableKeyPrefix(); } return config; } }