Created
January 22, 2021 06:56
-
-
Save HabeebCycle/22e39411b4269c74f611e170e0ec4503 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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