springboot学习笔记:设置Redis缓存时间

15 篇文章 0 订阅
8 篇文章 0 订阅

环境

MacBook Pro
Intellij IDEA

前言

在使用注解@Cacheable时,不能设置缓存时间,使用起来不是很爽,
稍微研究了一下;

配置

文件路径:
在这里插入图片描述

因为其不支持设置缓存时间,所以得通过RedisCacheManager的方式来设置

/**
 * @author yutao
 * @since 2020/3/31 7:03 下午
 */
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
public class RedisCacheConfig {

    /**
     * @param redisProperties 可以通过Apollo设置spring.redis.timeout
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisCacheManager setupRedisCacheManager(RedisProperties redisProperties,
                                                    RedisConnectionFactory redisConnectionFactory) {
        ObjectMapper mapper = new ObjectMapper();
        // 序列化时允许非常量字段均输出类型
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        // 支持解析Joda Time
        mapper.registerModule(new JodaModule());
        // 支持解析时间里面带T的格式
        mapper.registerModule(new JavaTimeModule());

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                // 设置超时时间
                .entryTtl(Optional.ofNullable(redisProperties.getTimeout()).orElse(Duration.ofMinutes(1L)))
                // 设置key的序列化器
                .serializeKeysWith(fromSerializer(new StringRedisSerializer()))
                // 设置value的序列化器
                .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer(mapper)))
                // 禁止缓存null值
                .disableCachingNullValues();

        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(redisCacheConfiguration).transactionAware().build();
    }

}

joda time 的 toString 并不简单,比如LocalDate类型的 “2014-05-05” 转化到 json 后会有很长一串数据,再从 json 转到 joda time,jackson 报错,序列化有问题了。
在 jackson 2.0 (也就是 fastxml)以后,处理 joda time 有一种很简单的做法,就是直接注册 joda time module 到 jackson mapper

注意:

String string="{\"date\":1571887801613,\"aa\":{},\"amount1\":1.1,\"uname\":\"Tom\"}";

对上面的字符串进行反序列化,但是因为date:1571887801613,不能明确知道其类型,所以反序列化失败。

所以我们需要ObjectMapper.DefaultTyping.NON_FINAL配置。

ObjectMapper.DefaultTyping.NON_FINAL配置,会在序列化时记录对象类型,以便反序列化时得到对应的具体对象。

依赖

springBootPluginVersion = '2.1.5.RELEASE'
// cache support
compile("org.springframework.boot:spring-boot-starter-cache:$springBootPluginVersion")
compile("org.springframework.boot:spring-boot-starter-data-redis:$springBootPluginVersion")

参考地址:

https://www.jianshu.com/p/c5fcd2a1ab49

https://www.cnblogs.com/xinsheng/p/4439464.html

https://blog.csdn.net/u011385940/article/details/83903591

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值