![[DB/Redis] Spring 연결](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2Fcde1CU%2FbtsIQdQcNfO%2FAAAAAAAAAAAAAAAAAAAAAPCgPTrT67Vz8MAXH7Mixxvy2QAyTlF5MPQWHNIZK_Sr%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1756652399%26allow_ip%3D%26allow_referer%3D%26signature%3Dp%252BKK7UVrYw6eSqTr8hOl5Oz1KlU%253D)
[DB/Redis] Spring 연결Database/Redis2024. 7. 30. 11:11
Table of Contents
728x90
@Configuration
public class RedisConfig {
@Value("${REDIS_HOST}")
private String host;
@Value("${REDIS_PORT}")
private Integer port;
@Value("${REDIS_PASSWORD}")
private String password;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(port);
configuration.setPassword(password);
return new LettuceConnectionFactory(configuration);
}
/*redis 연결 방식 지정 json을 주로 사용*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// redis 키 직렬화 방식
template.setKeySerializer(new GenericToStringSerializer<>(String.class));
// redis 값 직렬화 방식
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
// redis 키 해시로 직렬화
template.setHashKeySerializer(new GenericToStringSerializer<>(String.class));
// redus 값 해시로 직렬화
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return template;
}
}
redis spring연결 방법이다.
728x90
'Database > Redis' 카테고리의 다른 글
[DB/Redis] Window 설치 + docker에 설치 (0) | 2024.07.01 |
---|
@코딩하는 자연대생 :: 자연대생도 코딩을 하고 싶어