Cache源码解析(RedisCache配置过程)

①cache配置过程

1、找到CacheAutoConfiguration类

        内部有个配置类加载选择器,决定了哪个配置类会被加载!

2、找到CacheConfigurations类

        RedisCacheConfiguration为redis的缓存配置类。

3、找到RedisCacheConfiguration类

RedisCacheConfiguration向spring中放入了RedisCacheManager的bean,用于管理Redis的Cache。

其中有个方法:determineConfiguration()决定哪个redis缓存配置类生效。

4、转到determineConfiguration()方法

5、cache配置真相

  • ObjectProvider表示是从spring的容器中取值;网址:ObjectProvider使用说明 - 枫雪奕 - 博客园

  • determineConfiguration()方法表示在spring容器中如果自己配置了RedisCacheConfiguration类,则取得并使用该类;

  • 如果没有配置,则使用默认的RedisCacheConfiguration类

6、找到RedisCacheConfiguration类

这个类中制定了序列化的方式!因此,我们只需要将其改造成自己的MyRedisCacheConfiguration 配置类,将其放入容器中即可!

7、找到CacheProperties类

这不就是在properties文件中配置的属性!

②自定义RedisCacheConfiguration配置类指定序列化方式!

package com.wang.test.config.cache;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author wang
 * @date 2021/12/21 17:11
 * @description: RedisCacheConfiguration
 * @since 8
 */
@EnableConfigurationProperties(value = CacheProperties.class)//允许将配置属性配置类放入spring容器中
@Configuration
public class MyRedisCacheConfiguration {
    /**
     * 这里我们借鉴使用ObjectProvider,其实可以直接使用CacheProperties cacheProperties;
     * 因为@EnableConfigurationProperties(value = CacheProperties.class),容器中放入了CacheProperties的Bean实例
     * @param cacheProperties
     * @return
     */
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(ObjectProvider<CacheProperties> cacheProperties){
        //在默认配置上修改,也可以new一个。指定序列化方式!
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().
                serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).
                serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));

        //从容器中取得redisProperties并进行判断,不然在application.yml配置的文件无效。
        // 因为只有默认的RedisCacheConfiguration才会走下面的内容,自定义的需要cv大法后改造一下
        CacheProperties ifAvailable = cacheProperties.getIfAvailable();
        if (null != ifAvailable){
            CacheProperties.Redis redisProperties = ifAvailable.getRedis();
            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;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值