All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.hpsocket.soa.starter.data.redis.config.SoaFirstRedisConfig Maven / Gradle / Ivy


package io.github.hpsocket.soa.starter.data.redis.config;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.api.RedissonRxClient;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import io.github.hpsocket.soa.starter.data.redis.redisson.RedissonAutoConfigurationCustomizer;
import io.github.hpsocket.soa.starter.data.redis.redisson.RedissonProperties;

/** 默认 Redis 实例之外第一个 Redis 配置 */
@AutoConfiguration
@ConditionalOnExpression("'${spring.data.redis-first.host:}' != '' || '${spring.data.redis-first.url:}' != '' || '${spring.redis.redisson-first.config:}' != '' || '${spring.redis.redisson-first.file:}' != ''")
public class SoaFirstRedisConfig extends SoaAbstractRedisConfig
{
    public static final String redissonAutoConfigurationCustomizerBeanName = "firstRedissonAutoConfigurationCustomizer";
    public static final String soaDefaultRedissionPropertiesBeanName = "soaFirstRedissionProperties";
    public static final String soaDefaultRedisPropertiesBeanName = "soaFirstRedisProperties";
    public static final String redisStringKeyGeneratorBeanName = "firstRedisStringKeyGenerator";
    public static final String redisTemplateBeanName = "firstRedisTemplate";
    public static final String redisStringTemplateBeanName = "firstRedisStringTemplate";
    public static final String redisJsonTemplateBeanName = "firstRedisJsonTemplate";
    public static final String redisGenericJsonTemplateBeanName = "firstRedisGenericJsonTemplate";
    public static final String redisKryoTemplateBeanName = "firstRedisKryoTemplate";
    public static final String redisKryoNotNullTemplateBeanName = "firstRedisKryoNotNullTemplate";
    public static final String redisReactiveTemplateBeanName = "firstRedisReactiveTemplate";
    public static final String redisReactiveStringTemplateBeanName = "firstRedisReactiveStringTemplate";
    public static final String redisCacheManagerBeanName = "firstRedisCacheManager";
    public static final String redisDefaultCacheConfigurationBeanName = "firstRedisDefaultCacheConfiguration";
    public static final String redisInitialCacheSourceMapBeanName = "firstRedisInitialCacheSourceMap";
    public static final String redisInitialCacheConfigurationsBeanName = "firstRedisInitialCacheConfigurations";
    public static final String redisConnectionFactoryBeanName = "firstRedisConnectionFactory";
    public static final String redissonReactiveClientBeanName = "firstRedissonReactiveClient";
    public static final String redissonRxClientBeanName = "firstRedissonRxClient";
    public static final String redissonClientBeanName = "firstRedissonClient";

    public SoaFirstRedisConfig(
        @Qualifier(redissonAutoConfigurationCustomizerBeanName) ObjectProvider> redissonAutoConfigurationCustomizers,
        @Qualifier(soaDefaultRedissionPropertiesBeanName) ObjectProvider SoaRedissionProperties,
        @Qualifier(soaDefaultRedisPropertiesBeanName) ObjectProvider SoaRedisProperties)
    {
        super(redissonAutoConfigurationCustomizers, SoaRedissionProperties, SoaRedisProperties);
    }

    /** 第一个 Redis {@linkplain KeyGenerator} */
    @Override
    @Bean(redisStringKeyGeneratorBeanName)
    @ConditionalOnMissingBean(name = redisStringKeyGeneratorBeanName)
    public KeyGenerator keyGenerator()
    {
        return super.keyGenerator();
    }
                
    /** 第一个 Redis {@linkplain RedisTemplate} */
    @Override
    @Bean(redisTemplateBeanName)
    @ConditionalOnMissingBean(name = redisTemplateBeanName)
    public  RedisTemplate redisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.redisTemplate(redisConnectionFactory);
    }

    /** 第一个 Redis {@linkplain StringRedisTemplate} */
    @Override
    @Bean(redisStringTemplateBeanName)
    @ConditionalOnMissingBean(name = redisStringTemplateBeanName)
    public StringRedisTemplate stringRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.stringRedisTemplate(redisConnectionFactory);
    }

    /** 第一个 Redis 基于 FastJson 序列化的 {@linkplain RedisTemplate} */
    @Override
    @Bean(redisJsonTemplateBeanName)
    @ConditionalOnMissingBean(name = redisJsonTemplateBeanName)
    public RedisTemplate jsonRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.jsonRedisTemplate(redisConnectionFactory);
    }

    /** 第一个 Redis 基于通用 FastJson 序列化的 {@linkplain RedisTemplate} */
    @Override
    @Bean(redisGenericJsonTemplateBeanName)
    @ConditionalOnMissingBean(name = redisGenericJsonTemplateBeanName)
    public  RedisTemplate genericJsonRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.genericJsonRedisTemplate(redisConnectionFactory);
    }

    /** 第一个 Redis 基于 Kryo 序列化的 {@linkplain RedisTemplate} */
    @Override
    @Bean(redisKryoTemplateBeanName)
    @ConditionalOnMissingBean(name = redisKryoTemplateBeanName)
    public  RedisTemplate kryoRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.kryoRedisTemplate(redisConnectionFactory);
    }

    /** 第一个 Redis 基于 Kryo 序列化的 {@linkplain RedisTemplate} (不支持存储 null 值)*/
    @Override
    @Bean(redisKryoNotNullTemplateBeanName)
    @ConditionalOnMissingBean(name = redisKryoNotNullTemplateBeanName)
    public  RedisTemplate kryoNotNullRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory)
    {
        return super.kryoNotNullRedisTemplate(redisConnectionFactory);
    }
    
    /** 第一个 {@linkplain ReactiveRedisTemplate} */
    @Override
    @Bean(redisReactiveTemplateBeanName)
    @ConditionalOnMissingBean(name = redisReactiveTemplateBeanName)
    public  ReactiveRedisTemplate reactiveRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) ReactiveRedisConnectionFactory reactiveRedisConnectionFactory)
    {
        return super.reactiveRedisTemplate(reactiveRedisConnectionFactory);
    }

    /** 第一个 {@linkplain ReactiveStringRedisTemplate} */
    @Override
    @Bean(redisReactiveStringTemplateBeanName)
    @ConditionalOnMissingBean(name = redisReactiveStringTemplateBeanName)
    public ReactiveStringRedisTemplate reactiveStringRedisTemplate(@Qualifier(redisConnectionFactoryBeanName) ReactiveRedisConnectionFactory reactiveRedisConnectionFactory)
    {
        return super.reactiveStringRedisTemplate(reactiveRedisConnectionFactory);
    }

    /** 第一个 Redis {@linkplain RedisCacheManager} */
    @Override
    @Bean(redisCacheManagerBeanName)
    @ConditionalOnMissingBean(name = redisCacheManagerBeanName)
    public RedisCacheManager redisCacheManager(
        @Qualifier(redisConnectionFactoryBeanName) RedisConnectionFactory redisConnectionFactory,
        @Qualifier(redisDefaultCacheConfigurationBeanName) RedisCacheConfiguration redisDefaultCacheConfiguration,
        @Qualifier(redisInitialCacheConfigurationsBeanName) MapPropertySource redisInitialCacheConfigurations)
    {
        return super.redisCacheManager(redisConnectionFactory, redisDefaultCacheConfiguration, redisInitialCacheConfigurations);
    }
    
    /** 第一个 Redis {@linkplain RedisCacheConfiguration} */
    @Override
    @Bean(redisDefaultCacheConfigurationBeanName)
    @ConditionalOnMissingBean(name = redisDefaultCacheConfigurationBeanName)
    public RedisCacheConfiguration redisDefaultCacheConfiguration()
    {
        return super.redisDefaultCacheConfiguration();
    }
    
    @Override
    @Bean(redisInitialCacheSourceMapBeanName)
    @ConditionalOnMissingBean(name = redisInitialCacheSourceMapBeanName)
    public Map redisInitialCacheSourceMap()
    {
        return super.redisInitialCacheSourceMap();
    }
    
    /** 第一个 Redis 初始 {@linkplain RedisCacheConfiguration} {@linkplain Map} */
    @Override
    @Bean(redisInitialCacheConfigurationsBeanName)
    @ConditionalOnMissingBean(name = redisInitialCacheConfigurationsBeanName)
    public MapPropertySource redisInitialCacheConfigurations(@Qualifier(redisInitialCacheSourceMapBeanName) Map redisInitialCacheSourceMap)
    {
        return super.redisInitialCacheConfigurations(redisInitialCacheSourceMap);
    }
    
    @Override
    @Bean(redisConnectionFactoryBeanName)
    @ConditionalOnMissingBean(name = redisConnectionFactoryBeanName)
    public RedissonConnectionFactory redissonConnectionFactory(@Qualifier(redissonClientBeanName) RedissonClient redisson)
    {
        return super.redissonConnectionFactory(redisson);
    }

    @Lazy
    @Override
    @Bean(redissonReactiveClientBeanName)
    @ConditionalOnMissingBean(name = redissonReactiveClientBeanName)
    public RedissonReactiveClient redissonReactive(@Qualifier(redissonClientBeanName) RedissonClient redisson)
    {
        return redisson.reactive();
    }

    @Lazy
    @Override
    @Bean(redissonRxClientBeanName)
    @ConditionalOnMissingBean(name = redissonRxClientBeanName)
    public RedissonRxClient redissonRxJava(@Qualifier(redissonClientBeanName) RedissonClient redisson)
    {
        return redisson.rxJava();
    }

    @Override
    @Bean(name = redissonClientBeanName, destroyMethod = "shutdown")
    @ConditionalOnMissingBean(name = redissonClientBeanName)
    public RedissonClient redisson() throws IOException
    {
        return super.redisson();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy