Skip to content

Instantly share code, notes, and snippets.

@HabeebCycle
Created January 22, 2021 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HabeebCycle/22e39411b4269c74f611e170e0ec4503 to your computer and use it in GitHub Desktop.
Save HabeebCycle/22e39411b4269c74f611e170e0ec4503 to your computer and use it in GitHub Desktop.
package com.habeebcycle.demo.api.config;
import com.habeebcycle.demo.api.model.User;
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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfiguration {
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private int redisPort;
@Value("${spring.redis.password}")
private String redisPassword;
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfig = new RedisStandaloneConfiguration();
redisStandaloneConfig.setHostName(redisHost);
redisStandaloneConfig.setPort(redisPort);
redisStandaloneConfig.setPassword(redisPassword);
return new LettuceConnectionFactory(redisStandaloneConfig);
}
@Bean
public ReactiveRedisOperations<String, User> redisOperations(LettuceConnectionFactory connectionFactory) {
RedisSerializationContext<String, User> serializationContext = RedisSerializationContext
.<String, User>newSerializationContext(new StringRedisSerializer())
.key(new StringRedisSerializer())
.value(new GenericToStringSerializer<>(User.class))
.hashKey(new StringRedisSerializer())
.hashValue(new GenericJackson2JsonRedisSerializer())
.build();
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment