通过查看autoconfigure源码

org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration;

部分源码如下:

private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
ClassLoader classLoader) {
if (this.redisCacheConfiguration != null) {
return this.redisCacheConfiguration;
}
Redis redisProperties = this.cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig();
config = config.serializeValuesWith(SerializationPair
.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
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;
}

可以看到默认是使用的 JdkSerializationRedisSerializer ,还有就是如果容器里已经有 redisCacheConfiguration 就直接使用了。

那么只需要自己注入一个 RedisCacheConfiguration 即可。

代码可以直接用源码里面的:),调整序列化部分即可。

 @Configuration
@Conditional(SimpleCacheCondition.class)
public class MyRedisCacheConfiguration {
private final CacheProperties cacheProperties;
MyRedisCacheConfiguration(CacheProperties cacheProperties) {
this.cacheProperties = cacheProperties;
}
@Bean
public org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration() {
CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig();
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(valueSerializer()));
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;
}
/**
* 使用Jackson序列化器
* @return
*/
private RedisSerializer<Object> valueSerializer() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
return new GenericJackson2JsonRedisSerializer(objectMapper);
} }

这里使用的是 GenericJackson2JsonRedisSerializer ;

参考文档:

https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/serializer/RedisSerializer.html

可以看到已有的可用序列化器:

GenericJackson2JsonRedisSerializerGenericToStringSerializerJackson2JsonRedisSerializerJdkSerializationRedisSerializerOxmSerializerStringRedisSerializer

这里顺便学习一下 @Conditional ,这个注解可以帮我们控制什么时候来注册组件。

比如这里写了一个 RedisCacheConfiguration 配置,那我只想在启用了redis缓存时再注册,就可以使用这个注解,如 @Conditional(SimpleCacheCondition.class) 。

定义 SimpleCacheCondition ,部分代码参考 org.springframework.boot.autoconfigure.cache.CacheCondition

public class SimpleCacheCondition implements Condition {

    /**
* Determine if the condition matches.
*
* @param context the condition context
* @param metadata metadata of the {@link AnnotationMetadata class}
* or {@link MethodMetadata method} being checked
* @return {@code true} if the condition matches and the component can be registered,
* or {@code false} to veto the annotated component's registration
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
try {
String sourceClass = "";
if (metadata instanceof ClassMetadata) {
sourceClass = ((ClassMetadata) metadata).getClassName();
}
Environment environment = context.getEnvironment();
BindResult<CacheType> specified = Binder.get(environment)
.bind("spring.cache.type", CacheType.class);
if (!specified.isBound()) {
return false;
}
       //redis cache 启用 并且 是自定义的redisCacheConfiguration
if (specified.get().equals(CacheType.REDIS) && sourceClass.equals(MyRedisCacheConfiguration.class.getTypeName())) {
return true;
}
}catch (Exception ex)
{}
return false;
}
}
												

配置spring cache RedisCacheManager的序列化方法的更多相关文章

  1. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  2. spring boot系列(五)spring boot 配置spring data jpa (查询方法)

    接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...

  3. Spring Cache 带你飞(二)

    接着上一篇讲了 Spring Cache 如何被 Spring Aop 代理加载对应的代码,以及何如注入相关界面逻辑. Spring Cache 带你飞(一) 本篇我们围绕两个要点展开: 一个数据是如 ...

  4. 品味Spring Cache设计之美

    最近负责教育类产品的架构工作,两位研发同学建议:"团队封装的Redis客户端可否适配Spring Cache,这样加缓存就会方便多了" . 于是边查阅文档边实战,收获颇丰,写这篇文 ...

  5. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  6. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  7. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  8. Spring cache 缓存

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  9. Spring cache源码分析

    Spring cache是一个缓存API层,封装了对多种缓存的通用操作,可以借助注解方便地为程序添加缓存功能. 常见的注解有@Cacheable.@CachePut.@CacheEvict,有没有想过 ...

随机推荐

  1. configure文件的生成

    https://blog.csdn.net/mitesi/article/details/44773951 make 根据Makefile编译源代码,连接,生成目标文件,可执行文件. make cle ...

  2. Linux_磁盘分布_以及分区

    运用 Xshell  工具链接到你的服务器 1.     Fdisk -l    这是查看磁盘挂载列表情况 2.      Fdisk /dev/vdc   这是分区这个磁盘   m    是查看信息 ...

  3. CentOS7安装GitLab服务

    安装GitLab服务 1.安装必要依赖 yum install -y curl policycoreutils openssh-server openssh-clients postfix 2.下载安 ...

  4. bootstrap-treeview树形图参数详解

    哈哈 找了半天找到了,需要的可以去看看! 直接放上博客链接:https://blog.csdn.net/hailangtuteng/article/details/80842730

  5. c++11 强类型枚举 enum class

    在标准C++中,枚举类型不是类型安全的.枚举类型被视为整数,这使得两种不同的枚举类型之间可以进行比较.C++03 唯一提供的安全机制是一个整数或一个枚举型值不能隐式转换到另一个枚举别型. 此外,枚举所 ...

  6. 问题记录 --Error parsing column 1 (Function_Num=10 - String)”

    当C#查询数据库出现Error parsing column ## 的时候,首先去看看数据库里面该字段是什么类型,然后在看看你在创建model 的时候是什么类型,如果model的类型和数据库字段类型不 ...

  7. C#返回JSON格式数据

    又类的属性生成json格式数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  8. DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数

    DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数 文末有彩蛋,解决蛋疼问题 难度: ★★☆☆☆(2星) 适用范围: ★★★☆☆(3星) 概况: 基于时间的汇总可能是 ...

  9. 解析key值不确定的json数据

    遇到一个奇葩的需求,一段json的key值是动态的,并且这个key还是有作用的.这就要求在不知道key是多少的情况下去把这段json解析出来. 我用到的方法是迭代器.具体代码如下 JSONObject ...

  10. 运维面试题之k8s

    前言: 到了如今年k8s已经是事实上的容器集群标准了,是时候展现我真正的祖传k8s实力了 吐槽: 我干嘛要知道这些,能用不就行了k8s真香 Kubernetes有哪些特性? Kubernetes是自动 ...