728x90

이전글

Redis 로컬 설치 방법은 다음 글을 참고하세요

 

[Redis] Redis 설치

Redis 소개 Key - Value 형태 인메모리 데이터 저장 구조 문자열, 해시, 리스트, 셋, 스트림 등 데이터 구조 제공 Spring Data Redis Some commands (such as SINTER and SUNION) can only be processed on the server side when all invo

chordplaylist.tistory.com

Reids 설정

Spring Gradle 의존성 추가

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

application.yml host, port 추가

spring:
  redis:
    host: localhost # 저는 EC2 환경이므로 EC2 DNS 주소를 입력하였습니다.
    port: 6379

config.class 생성

@Configuration
public class RedisConfig {

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

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

    @Bean
    public RedisConnectionFactory redisConnectionFactory(){
        return new LettuceConnectionFactory(host, port);
    }
}

application.yml에서 작성한 값을 @Value를 통해 입력받습니다.
하단에 LettuceConnectionFactory를 반환 받게 되는데, Lettuce를 쓰는 이유는 다음과 같습니다.

  • netty 기반
  • 다중 스레드 공유

Redis Template

상단의 config에 다음 내용을 추가합니다.

@Configuration
public class RedisConfig {

    ...
    @Bean
    public RedisTemplate<?, ?> redisTemplate(){
        RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }

}

Redis Dao

@Component
public class RedisDao {
    private final RedisTemplate<String, String> redisTemplate;

    public RedisDao(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setValues(String key, String value) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(key, value);
    }

    public void setValues(String key, String value, Duration duration) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(key, value, duration);
    }

    public void setValues(String key, String value, long timeout, TimeUnit unit) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(key, value, timeout, unit);
    }

    public String getValues(String key) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        return values.get(key);
    }

    public void deleteValues(String key) {
        redisTemplate.delete(key);
    }
}

 

참조

 

Spring Boot 에서 Redis 사용하기

1. Overview Spring Boot 에서 spring-data-redis 라이브러리를 활용해서 Redis 를 사용하는 방법을 알아봅니다. Redis 에 대한 개념과 로컬 설치 방법은 Redis 설치 및 명령어 글을 확인해주세요. 2. Java 의 Redis Cl

bcp0109.tistory.com

 

 

hwsa1004 (Dev_ch) - velog

[Spring] JPA를 이용한 페이징&정렬 게시판 서비스를 구현하던 도중 page로 프론트에게 넘기게 되었다. 프론트에게 페이지로 넘길때는 다양한 방법이 있겠지만, JPA의 Pageable을 통해 페이지네이션하

velog.io

Lettuce를 사용하는 이유

 

Why is Lettuce the default Redis client used in Spring Session Redis? · Issue #789 · spring-projects/spring-session

Why Lettuce over Jedis? Is this an arbitrary decision or is there a technical reason behind this decision? Thank you.

github.com

 

반응형
코드플리