Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.6 kB
2
Indexable
Never
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
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 lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class RedisConfig {

	@Value("${spring.redis.cluster}")
	private List<String> nodes;

	@Value("${spring.redis.password}")
	private String password;

	@Bean
	public LettuceConnectionFactory getLettuceConnectionFactory() {
		log.info("-----getLettuceConnectionFactory method-----");
		final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(
				new RedisClusterConfiguration(nodes));
		lettuceConnectionFactory.setPassword(password);
		lettuceConnectionFactory.afterPropertiesSet();
		return lettuceConnectionFactory;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate() {
		log.info("-----redisTemplate bean creation-----");
		final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setConnectionFactory(getLettuceConnectionFactory());
		return redisTemplate;
	}
}
Leave a Comment