OPEN HYPER STEP
← 목록으로 (Java+Spring)
JAVA · 63 / 69
java
CHAPTER 63 / 69
읽기 약 2
FUNCTION

캐싱 Redis + Spring Cache


핵심 개념

Spring Cache + Redis로 성능을 최적화한다. @Cacheable, @CacheEvict, @CachePut, TTL 설정, 캐시 전략을 실무 수준으로 구현한다.

코드 분석
JAVA📋 코드 (45줄)
# application.properties
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.cache.type=redis
spring.cache.redis.time-to-live=3600000 # 1시간

// CacheConfig.java
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(1))
            .serializeValuesWith(RedisSerializationContext
                .SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(factory)
            .cacheDefaults(config).build();
    }
}

// UserService.java — 캐시 적용
@Service
public class UserService {
    // 캐시에 없으면 DB 조회 후 저장
    @Cacheable(value = "users", key = "#id")
    public UserDto findById(Long id) {
        return userRepository.findById(id).map(UserDto::from)
            .orElseThrow(() -> new UserNotFoundException(id));
    }

    // 수정 시 캐시 갱신
    @CachePut(value = "users", key = "#id")
    @Transactional
    public UserDto update(Long id, UpdateUserDto dto) {
        // ... 수정 로직
    }

    // 삭제 시 캐시 무효화
    @CacheEvict(value = "users", key = "#id")
    @Transactional
    public void delete(Long id) {
        userRepository.deleteById(id);
    }
}

AI 프롬프트
🤖 AI에게 잘 물어보는 법 — 모델·전략별 프롬프트
Claude

무료: Sonnet 4.6 / Pro $20/mo: Opus 4.6

이 Spring '캐싱 Redis + Spring Cache' 코드에서
DI 관련 버그·순환 참조·트랜잭션 누수를
찾아서 수정해줘.
ChatGPT

무료: GPT-5.5 / Plus $20/mo: GPT-5.5 Pro

'캐싱 Redis + Spring Cache'를 Spring Boot 3.x로 구현한
실전 API 코드(컨트롤러+서비스+레포지토리+테스트)를 완성형으로 만들어줘.
Gemini

무료: 2.5 Flash / Pro $19.99/mo: 3.1 Pro

이 Spring '캐싱 Redis + Spring Cache' 프로젝트의 빈 구조와
의존성 트리를 전체 분석하고
N+1 쿼리·순환 참조·성능 병목을 정리해줘.
Grok

무료: Grok 4.1 / SuperGrok $30/mo

Spring '캐싱 Redis + Spring Cache' vs Quarkus·Micronaut·Ktor의
동일 기능 구현을 2026년 한국 채용 시장 기준으로
솔직히 비교해줘.

⭐ 이것만 기억하세요
캐싱 Redis + Spring Cache 이 3가지만 확실히 잡으세요
1.매 요청마다 DB를 조회하면 동일 데이터도 반복 처리돼서 응답 시간이 불필요하게 길어집니다
2.@Cacheable로 메서드 결과를 캐싱하고, Redis를 캐시 저장소로 연동하면 밀리초 단위 응답이 가능합니다
3.다음 챕터에서 코드 품질을 보장하는 테스트를 심화 학습합니다


공유하기
진행도 63 / 69