본문 바로가기

개발중/Spring Boot & Redis

Spring Boot와 Redis를 사용한 데이터 저장 및 관리

728x90
반응형

 

Redis는 빠른 오픈 소스 인-메모리 키-값 데이터 스토어로, 다양한 데이터 구조를 지원합니다. 

Spring Boot 애플리케이션에서 Redis를 사용하면, 데이터 캐싱, 메시지 브로커 등 다양한 방식으로 활용할 수 있습니다. 

 

이 글에서는 Spring Boot를 이용하여 Redis 데이터 구조를 어떻게 조작하는지 보여드립니다.

 

설정 사항

먼저, Redis 서버가 설치되어 있어야 하며, application.properties 파일에 다음과 같이 Redis 서버 접속 정보를 설정해야 합니다:

spring.redis.host=localhost
spring.redis.port=6379

 

    •  

Redis 데이터 유형 및 조작

 

String

  • Redis CLI
127.0.0.1:6379> SET key value
OK
127.0.0.1:6379> GET key
"value"
127.0.0.1:6379> DEL key
(integer) 1
127.0.0.1:6379> GET key
(nil)

 

 

  • Spring Boot에서의 사용법
@Component
public class RedisTemplateString {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * Save String
     */
    public void saveStringType(String key, String value) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        stringStringValueOperations.set(key, value);
    }

    /**
     * Read String
     */
    public String getStringType(String key) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        return stringStringValueOperations.get(key);
    }

    /**
     * Delete String
     */
    public void deleteStringType(String key) {
        redisTemplate.delete(key);
    }
}

 

Map

  • Redis CLI
127.0.0.1:6379> HSET key field1 value1 field2 value2
(integer) 2
127.0.0.1:6379> HGETALL key
1) "field1"
2) "value1"
3) "field2"
4) "value2"
127.0.0.1:6379> DEL key
(integer) 1
127.0.0.1:6379> HGETALL key
(empty array)

 

 

  • Spring Boot에서의 사용법
@Component
public class HashOperationsMap {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    private HashOperations<String, String, String> hashOperations;

    @PostConstruct
    private void init() {
        hashOperations = redisTemplate.opsForHash();
    }

    /**
     * Save Map<String, String>
     */
    public void saveMap(String key, Map<String, String> map) {
        hashOperations.putAll(key, map);
    }

    /**
     * Read Map<String, String>
     */
    public Map<String, String> getMap(String key) {
        return hashOperations.entries(key);
    }

    /**
     * Delete Map<String, String>
     */
    public void deleteMap(String key){
        hashOperations.getOperations().delete(key);
    }
}

 

List

 

  • Redis CLI
127.0.0.1:6379> RPUSH key value1 value2
(integer) 2
127.0.0.1:6379> LPUSH key value3 value4
(integer) 4
127.0.0.1:6379> LRANGE key 0 -1
1) "value4"
2) "value3"
3) "value1"
4) "value2"
127.0.0.1:6379> DEL key
(integer) 1
127.0.0.1:6379> LRANGE key 0 -1
(empty array)

 

  • Spring Boot에서의 사용법
@Component
public class RedisTemplateList {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * Save List<String> : Right Push
     */
    public void saveListRightPush(String key, List<String> list) {
        ListOperations<String, String> listOps = redisTemplate.opsForList();
        list.forEach(word -> listOps.rightPush(key, word));
    }

    /**
     * Read List<String> : Left Push
     */
    public void saveListLeftPush(String key, List<String> list) {
        ListOperations<String, String> listOps = redisTemplate.opsForList();
        list.forEach(word -> listOps.leftPush(key, word));
    }

    /**
     * Read List<String>
     */
    public List<String> getList(String key) {
        ListOperations<String, String> listOps = redisTemplate.opsForList();
        return listOps.range(key, 0, -1);
    }

    /**
     * Delete List<String>
     */
    public void deleteList(String key) {
        redisTemplate.delete(key);
    }
}

 

 

 

Set

 

  • Redis CLI
127.0.0.1:6379> SADD key value1 value2
(integer) 2
127.0.0.1:6379> SMEMBERS key
1) "value1"
2) "value2"
127.0.0.1:6379> SADD key value1 value3
(integer) 1
127.0.0.1:6379> SMEMBERS key
1) "value1"
2) "value2"
3) "value3"
127.0.0.1:6379> SREM key value2
(integer) 1
127.0.0.1:6379> SMEMBERS key
1) "value1"
2) "value3"
127.0.0.1:6379> SCARD key
(integer) 2

 

  • Spring Boot에서의 사용법
@Component
public class RedisTemplateSets {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    private SetOperations<String, String> setOperations;

    @PostConstruct
    private void init() {
        this.setOperations = redisTemplate.opsForSet();
    }

    /**
     * Save Set<String>
     */
    public void addToSet(String key, String... values) {
        setOperations.add(key, values);
    }

    /**
     * Read Set<String>
     */
    public Set<String> getMembers(String key) {
        return setOperations.members(key);
    }

    /**
     * Delete Set<String>
     */
    public void removeFromSet(String key, String... values) {
        setOperations.remove(key, (Object[])values);
    }

    /**
     * Read Set Size
     */
    public Long setSize(String key) {
        return setOperations.size(key);
    }
}
728x90
반응형