hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

Development/Spring

[Spring + Redis] Spring Redis 연동

dependency

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

 

application.properties

spring.redis.host=127.0.0.1 spring.redis.port=6379

 

RedisUtil.java

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
    @Autowired 
	private StringRedisTemplate redisTemplate;

    public void set(String key, String value, Long minutes){
        redisTemplate.opsForValue().set(key, value, minutes, TimeUnit.MILLISECONDS);
    }

    public String get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public Boolean delete(String key){
        return redisTemplate.delete(key);
    }

    public Boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }
}

 

이렇게 util 파일 만들면 StringRedisTemplate에 알아서 redis가 연결된다. 사용은

@Autowired private final RedisUtil redisUtil;
redisUtil.get("key1");
redisUtil.set("key1", "value1", 10);

 

이렇게 쓰면 된다.

'Development > Spring' 카테고리의 다른 글

[Spring] Custom Exception Handler  (0) 2022.10.05
[Spring + Jwt] Spring Boot + Spring Security + Redis + Jwt를 이용한 회원가입 및 로그인  (0) 2022.10.05
[JPA] JPA Join 이슈, FETCH JOIN의 사용  (0) 2022.10.04
[JPA] JPQL FROM절 subquery를 해결하는 방법 (inline view)  (0) 2022.10.04
[Spring] DTO와 Entity 간의 변환  (0) 2022.10.04
    hyelie
    hyelie

    티스토리툴바