개요
Java Spring Boot 레디스 기본 설정에 대한 부분 및 간단한 사용법
Lettuce 사용 ( spring-boot-starter-data-redis 에서 기본 지원 별도 의존성 추가가 필요 없습니다.)
dependencies
org.springframework.boot 2.4.2
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}
Properties - 레디스 기본 설정 정보 입력
spring.redis.port=6379
spring.redis.host= host
spring.redis.password= password
RedisConfig.java - 레디스 기본설정
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private int redisPort;
@Value("${spring.redis.password}")
private String redisPassword;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration;
LettuceConnectionFactory lettuceConnectionFactory;
redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisHost);
redisStandaloneConfiguration.setPort(redisPort);
redisStandaloneConfiguration.setPassword(redisPassword); // elasticache 의 경우 비밀번호 설정이 없으므로 미사용
lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);
return lettuceConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
사용예시
@RequiredArgsConstructor
public class ... {
private final RedisTemplate redisTemplate;
// 데이터 저장
public ... testRedisSet(){
...
ValueOperations<String, Object> vop = redisTemplate.opsForValue();
vop.set("key","value",1,Timeunit.MINUTES) // 만료시간 1분뒤 key 에 대한 value 데이터 레디스에 기록
...
}
// 데이터 호출
public ... testRedisGet(){
...
ValueOperations<String, Object> vop = redisTemplate.opsForValue();
String data = (String) vop.get("key"); // redisTemplate.setValueSerializer(new StringRedisSerializer()); 저장값을 String 으로 해놓았기 때문에 받을때도 문자열로 받도록 처리
...
}
// 삭제
public ... testRedisDelete(){
redisTemplate.delete("key");
}
}
설정한 레디스는 어디에 사용할 예정인가?
- spring boot security ( public Authentication getAuthentication(String token) 인증정보 조회시 redis 데이터 선조회 )
- Cache 서비스
- Redis pub/sub 이용한 영상 콜백 처리
- Email , Mobile 같은 인증 처리시