Spring Boot Redis Cache 序列化配置

前言

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

  • 指定值序列器为 jsonGenericJackson2JsonRedisSerializer
  • 指定 ttl (超时时间)

总结

Spring BootCache 的支持

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值