Spring Boot Redis Cache 序列化配置

  • Post author:
  • Post category:其他




前言


Spring Boot

支持对

Spring Cache

的开箱即用,其中包括

Redis Cache

,本文结合部分代码示例

Spring Boot Redis Cache

的相关配置,比如

序列化



CacheProperties

@ConfigurationProperties(prefix = "spring.cache")
public class CacheProperties {

	// 缓存类型
	private CacheType type;

	private final Redis redis = new Redis();

	public static class Redis {
		// 过期时间,默认永久有效
		private Duration timeToLive;
		// 允许缓存空值
		private boolean cacheNullValues = true;
		// 缓存 key 前缀
		private String keyPrefix;
		// 默认使用前缀
		private boolean useKeyPrefix = true;
		// 缓存统计?
		private boolean enableStatistics;
		// ...
	}
	
	// ...

}
  • 通过

    spring.cache.xxx

    配置

    Spring Cache
  • 其中

    spring.cache.type = redis

    即是用

    redis

    缓存,同理还包括:

    generic


    jCache

  • 每种缓存支持各自的属性,比如

    redis

    配置

    过期时间



    key前缀



RedisCacheConfiguration

	@Bean
	RedisCacheManager cacheManager(CacheProperties cacheProperties,
			// CacheManager 后处理
			CacheManagerCustomizers cacheManagerCustomizers,
			// RedisCacheConfiguration 自定义
			ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
			// RedisCacheManagerBuilder 后处理
			ObjectProvider<RedisCacheManagerBuilderCustomizer> redisCacheManagerBuilderCustomizers,
			RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
		
		// ...
		
	}
  • 基于缓存类型的配置引入对应的配置类(参考

    CacheConfigurations

    ),比如

    RedisCacheConfiguration
  • 此处会创建对应的

    CacheManager

    ,比如

    RedisCacheManager
  • 不看细节,通过入参可以推测,支持我们

    • 通过

      CacheManagerCustomizer

      类后处理

      CacheManager

      ,很常规的模式了
    • 完全自定义

      RedisCacheConfiguration

      来实现对

      RedisCacheManager

      的控制
    • 通过

      RedisCacheManagerBuilderCustomizer

      类后处理

      RedisCacheManagerBuilder

      ,进而后处理默认的

      RedisCacheManager



createConfiguration

	private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(
			CacheProperties cacheProperties, ClassLoader classLoader) {
		Redis redisProperties = cacheProperties.getRedis();
		org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
				.defaultCacheConfig();
		
		// 默认 java 序列化
		config = config.serializeValuesWith(
				SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
		
		// 默认基于 CacheProperties#redis 属性配置
		if (redisProperties.getTimeToLive() != null) {
			config = config.entryTtl(redisProperties.getTimeToLive());
		}
		if (redisProperties.getKeyPrefix() != null) {
			config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
		}
		if (!redisProperties.isCacheNullValues()) {
			config = config.disableCachingNullValues();
		}
		if (!redisProperties.isUseKeyPrefix()) {
			config = config.disableKeyPrefix();
		}
		return config;
	}

如果上默认的

redis cache

配置:

  • 值序列器:

    JdkSerializationRedisSerializer
  • 其他属性诸如

    过期时间

    等都基于

    CacheProperties#redis

    配置



自定义

	@Bean
	public RedisCacheManagerBuilderCustomizer serializeCustomize() {
		return builder -> builder.cacheDefaults(
				RedisCacheConfiguration.defaultCacheConfig()
						// 指定值序列器为 json(GenericJackson2JsonRedisSerializer)
						.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(
								RedisSerializer.json()
						))
						// 指定缓存过期时间
						.entryTtl(Duration.ofHours(1L))
		);
	}

如上,可以自定义

RedisCacheManagerBuilderCustomizer

  • 指定值序列器为

    json



    GenericJackson2JsonRedisSerializer

  • 指定

    ttl

    (超时时间)



总结


Spring Boot



Cache

的支持



版权声明:本文为weixin_42189048原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。