Skip to content

refactor: 인메모리 저장소로 레디스 추상화#3

Merged
yunjae62 merged 6 commits into
mainfrom
refactor/add-inmemory-store
Aug 2, 2025
Merged

refactor: 인메모리 저장소로 레디스 추상화#3
yunjae62 merged 6 commits into
mainfrom
refactor/add-inmemory-store

Conversation

@yunjae62

@yunjae62 yunjae62 commented Aug 2, 2025

Copy link
Copy Markdown
Owner

개요

  • 기존의 레디스 의존적인 인메모리 저장소를 변경에 유연하게 추상화 작업을 진행함.
    • 레디스 7.2버전 라이선스 변경에 대응할 목적도 있지만, 프로젝트 초기에는 로컬 캐시를 사용하다가 이후에 변경하도록 작업함

작업사항

  • global.inmemory 디렉토리를 추가하고, global.redis를 하위로 이동
  • InMemoryStore 인터페이스 추가
  • 기존의 StringRedisTemplate, RedisUtil 객체를 RedisInMemoryStore 구현체로 대체
  • RedisUtil을 의존하던 객체를 InMemoryStore를 의존하도록 변경

세부사항

  • 인메모리 저장소 인터페이스 추가
    package ex.sample.global.inmemory;
    
    import java.time.Duration;
    import java.util.Optional;
    
    public interface InMemoryStore {
      
      <T> void put(String key, T value);
    
      <T> void put(String key, T value, Duration ttl);
    
      <T> Optional<T> get(String key, Class<T> clazz);
    
      boolean containsKey(String key);
    
      void remove(String key);
    
      void clear();
    }
  • 레디스 인메모리 저장소 구현체 추가
    @Slf4j
    @Component
    @RequiredArgsConstructor
    public class RedisInMemoryStore implements InMemoryStore {
    
        private final ObjectMapper mapper;
        private final StringRedisTemplate redisTemplate;
    
        @Override
        public <T> void put(String key, T value) {
            put(key, value, null);
        }
    
        @Override
        public <T> void put(String key, T value, Duration ttl) {
            String json = toJson(value);
    
            if (ttl != null) {
                redisTemplate.opsForValue().set(key, json, ttl);
            } else {
                redisTemplate.opsForValue().set(key, json);
            }
        }
    
        @Override
        public <T> Optional<T> get(String key, Class<T> clazz) {
            return Optional.ofNullable(redisTemplate.opsForValue().get(key))
                .map(json -> parseJson(json, clazz));
        }
    
        @Override
        public boolean containsKey(String key) {
            return redisTemplate.hasKey(key);
        }
    
        @Override
        public void remove(String key) {
            redisTemplate.delete(key);
        }
    
        @Override
        public void clear() {
            Objects.requireNonNull(redisTemplate.getConnectionFactory()).getConnection().commands().flushAll();
        }
    
        private <T> String toJson(T value) {
            try {
                return mapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
                log.error("Redis serialization error: {}", value, e);
                throw new GlobalException(ResponseCode.SYSTEM_ERROR);
            }
        }
    
        private <T> T parseJson(String json, Class<T> clazz) {
            try {
                return mapper.readValue(json, clazz);
            } catch (JsonProcessingException e) {
                log.error("Redis deserialization error: {}", json, e);
                throw new GlobalException(ResponseCode.SYSTEM_ERROR);
            }
        }
    }

관련 이슈

@yunjae62 yunjae62 self-assigned this Aug 2, 2025
@yunjae62 yunjae62 added the enhancement New feature or request label Aug 2, 2025
@github-actions

github-actions Bot commented Aug 2, 2025

Copy link
Copy Markdown

Test Results

1 tests   1 ✅  0s ⏱️
1 suites  0 💤
1 files    0 ❌

Results for commit f8207a3.

@yunjae62 yunjae62 merged commit ace1319 into main Aug 2, 2025
2 checks passed
@yunjae62 yunjae62 deleted the refactor/add-inmemory-store branch August 2, 2025 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant