package com.ssafy.backend.radis;
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.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Value("${spring.redis.host}")
private String host; // application.yml 에서 redis의 host 값
@Value("${spring.redis.port}")
private int port; // application.yml에서 radis의 port 값
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
setKeySerializer, setValueSerializer를 설정해주는 이유는 redis-cli를 통해서도 데이터를 확인하기 위함
이후 application.yml에 사용할 redis의 host와 port를 설정해 줌 → config에서 @Value로 그 값을 가지고 올 수 있음
spring:
redis:
host: ??
port: 6379
| 메소드 | 설명 |
|---|---|
| opsForValue | Strings를 쉽게 serialize/deserialize 해주는 interface |
| opsForList | List를 쉽게 Serialize/deserialize 해주는 interface |
| opsForSet | Set을 쉽게 serialize/deserialize 해주는 interface |
| opsForZSet | ZSet을 쉽게 serialize/deserialize 해주는 interface |
| opsForHash | Hash를 쉽게 Serialize/Deserialize 해주는 interface |
| delete(key) | key를 삭제 |