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

com.arextest.web.api.service.beans.RedisConfiguration Maven / Gradle / Ivy

There is a newer version: 0.7.0.1
Show newest version
package com.arextest.web.api.service.beans;

import com.arextest.common.cache.CacheProvider;
import com.arextest.common.cache.DefaultRedisCacheProvider;
import com.arextest.common.cache.SentinelRedisCacheProvider;
import com.arextest.common.cache.redistemplate.RedisTemplateCacheProvider;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author b_yu
 * @since 2023/4/6
 */
@Configuration
public class RedisConfiguration {

  @Value("${arex.redis.uri:}")
  private String redisUri;

  @Value("${arex.redis.sentinelUrl:}")
  private String sentinelUrl;

  @Bean
  @ConditionalOnMissingBean(CacheProvider.class)
  @ConditionalOnExpression("!'${arex.redis.uri:}'.isEmpty() or !'${arex.redis.sentinelUrl:}'.isEmpty()")
  public CacheProvider cacheProvider() {
    if (StringUtils.isNotEmpty(sentinelUrl)) {
      return new SentinelRedisCacheProvider(sentinelUrl);
    }
    return new DefaultRedisCacheProvider(redisUri);
  }

  @Bean
  @ConditionalOnExpression("!'${arex.redis.uri:}'.isEmpty() or !'${arex.redis.sentinelUrl:}'.isEmpty()")
  public RedissonClient redissonClient(CacheProvider cacheProvider) {
    return cacheProvider.getRedissionClient();
  }

  @Bean
  @ConditionalOnExpression("'${arex.redis.uri:}'.isEmpty() and '${arex.redis.sentinelUrl:}'.isEmpty()")
  public RedisTemplate redisTemplate(
      RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.setKeySerializer(RedisSerializer.byteArray());
    template.setValueSerializer(RedisSerializer.byteArray());
    return template;
  }


  @Bean
  @ConditionalOnExpression("'${arex.redis.uri:}'.isEmpty() and '${arex.redis.sentinelUrl:}'.isEmpty()")
  @ConditionalOnMissingBean(CacheProvider.class)
  public CacheProvider redisCacheProvider(RedisTemplate redisTemplate,
      RedissonClient redissonClient) {
    return new RedisTemplateCacheProvider(redisTemplate, redissonClient);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy