Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.2 kB
2
Indexable
Never
package com.aidtaas.mobius.cache.wrapper.lib.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.Arrays;

@Configuration
@ConditionalOnProperty(prefix = "spring.redis", name = "enable", havingValue = "true", matchIfMissing = false)
public class RedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Autowired
    private ConnectionPoolProperties connectionPool;

    @Value("${spring.redis.durationInDays}")
    private int durationInDays;

    @Bean
    @Primary
    public LettuceConnectionFactory lettuceConnectionFactory() {
        if (redisProperties.isStandalone()) {
            RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
            standaloneConfig.setHostName(redisProperties.getHost());
            standaloneConfig.setPort(redisProperties.getPort());
            standaloneConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));
            return new LettuceConnectionFactory(standaloneConfig, poolingClientConfiguration());
        } else {
            RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                    .master(redisProperties.getSentinel().getMaster())
                    .setSentinels(redisProperties.getSentinel().getNodes());

            if (redisProperties.getPassword() != null && !redisProperties.getPassword().isEmpty()) {
                sentinelConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));
            }

            return new LettuceConnectionFactory(sentinelConfig, poolingClientConfiguration());
        }
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager() {
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(durationInDays))
                .disableCachingNullValues()
                .serializeValuesWith(new GenericJackson2JsonRedisSerializer());

        return RedisCacheManager.builder(lettuceConnectionFactory())
                .cacheDefaults(cacheConfiguration)
                .build();
    }

    @Bean
    public GenericObjectPoolConfig poolingClientConfiguration() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(connectionPool.getMaxIdle());
        poolConfig.setMaxTotal(connectionPool.getMaxTotal());
        poolConfig.setMinIdle(connectionPool.getMinIdle());
        return poolConfig;
    }
}
Leave a Comment