通过查看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. linux-rhel7配置网卡bond双网卡主备模式

    参考以下文章中的 2.centos7配置bonding: https://www.cnblogs.com/huangweimin/articles/6527058.html 以下是配置过程的操作和打印 ...

  2. flutter 学习零碎知识点01

    1.Expanded组件 占满可用空间 -----可以到达类似flex布局中  第一列占用大量空间,所以它必须包装在Expanded widget中. 写死的高度改成Expanded自动撑满屏幕如果还 ...

  3. urlib库的使用

    urlib库实际上不是很常用,因为其api调用复杂,已被requests模块取代. 1.request发送请求 from urllib import request #默认指定的是get请求 #url ...

  4. VMware虚拟机共享宿主机硬盘步骤

    1.打开设置 2. 进去设置后,选择选项选项卡,启用文件夹共享,添加文件夹即可 注:虚拟机安装完成后我先装了 VMwareTools,然后进行的共享操作,貌似不安装时无法启用第三步,即 总是启用 按钮 ...

  5. FB面经prepare: 3 Window Max Sum

    Given a num array, find a window of size k, that has a maximum sum of the k entries. follow-up: Find ...

  6. 排序之选择排序(SelectSort)

    package com.sort; /* * 选择排序 * 把第一位与其他数进行比较,这样每轮比较都会出现一个最大值或最小值 * 根据需要让升序或降序排列 */ public class Select ...

  7. springboot自动生成mysql的DAO层代码

    springboot提供了强大丰富的整合内容,但是每次要写一堆dao层的xml或者数据库相关的配置代码的时候,还是挺繁琐又容易出错的. 可以用mybatis-generator自动生成代码: 只需要加 ...

  8. Web应用启动时,后台自动启动一个线程(转)

    原文:http://blog.sina.com.cn/s/blog_6810dfc20101ipzq.html Web应用启动时,后台自动启动一个线程 (1)前言 前几天,manager问道一个问题: ...

  9. work-7.2

    安装ubuntu,jdk ,git,maven,Intellij. 配置GIT时,需要将在本地生成的公钥粘贴到服务端. 先占个座,具体过程待补充. -------------------------- ...

  10. IT题库-134 | String、StringBuffer和StringBuilder的区别

    String是不可变的: StringBuffer是可变的,有默认长度的缓冲区,缓冲区一出时,则会自动增加: StringBuilder也是可变的,同上: StringBuffer是线程安全的(方法实 ...