▼ DataBase, NoSQL/Redis

Redis(레디스) | Spring Boot 프로젝트 연동하기

Valar 2021. 11. 25. 11:45
반응형

[DB, NoSQL] - Windows 10 | 레디스(Redis) 설치 및 명령어

[DB, NoSQL] - Windows 10 | 레디스(Redis) 비밀번호 설정

 

📌 구성환경

SpringBoot 2.5.6, Redis 3.2.100

 

build.gradle

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

 

application.yml (properties)

spring:
  redis:
    lettuce:
      pool:
        max-active: 5 #pool에 할당될 수 있는 커넥션 최대수 (음수로 하면 무제한)
        max-idle: 5 #pool의 "idle" 커넥션 최대수 (음수로 하면 무제한)
        min-idle: 2 #풀에서 관리하는 idle 커넥션의 최소 수 대상 
    host: 127.0.0.1
    port: 6379
    password: redisPass

 

Configuration (RedisConfig.java)

레디스(Redis) 연결을 위한 ConnectionFactory 설정과 실제 사용을 위한 Template을 구성한다.

 

package com.artmoa.member.config;

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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(password);
        return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }

    @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;
    }
}

 

Util Class (redisUtil.java)

데이터 생성, 조회 테스트를 위한 메서드를 생성한다.
데이터 생성 시 만료 시간을 설정할 수 있으며 시간이 만료될 경우 자동으로 소멸된다.

 

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

public class redisUtil {
  @Autowired
  private RedisTemplate redisTemplate;
  
  public void redisSetTest() {
    // Redis Template
    ValueOperations<String, String> vop = redisTemplate.opsForValue();
    // Redis Set Key-Value
    vop.set("key1", "data1"); // 만료제한 없이 생성
    vop.set("key-expire", "data-expire", Duration.ofSeconds(60)); // 60초 뒤에 해당 키가 만료되어 소멸된다.
  }
  
  public void redisGetTest() {
    // Redis Template
    ValueOperations<String, String> vop = redisTemplate.opsForValue();
    String redisValue = vop.get("key1");
    String redisExpireValue = vop.get("key-expire");
  }
}

 

redis-cli.exe

등록한 데이터를 redis-cli에서도 확인할 수 있다.
ttl [key] 명령어를 통해 남은 만료 시간을 확인할 수 있다.

 

 

반응형