Skip to content

[feat] Spring Cache(Caffeine) 도입 — 화이트리스트 기반 Strict + after-commit 이벤트 무효화#137

Merged
hwijae33 merged 7 commits intodevfrom
feat-posts
Nov 1, 2025
Merged

[feat] Spring Cache(Caffeine) 도입 — 화이트리스트 기반 Strict + after-commit 이벤트 무효화#137
hwijae33 merged 7 commits intodevfrom
feat-posts

Conversation

@hwijae33
Copy link
Copy Markdown
Collaborator

요약

  • Spring Cache(Caffeine) 도입: 화이트리스트 기반 Strict CacheManager로 미리 등록한 캐시명만 허용.
  • after-commit 무효화: @TransactionalEventListener(phase = AFTER_COMMIT)커밋 후 정밀 evict 수행.
  • 대상 캐시(예): post:detail, post:images (CacheNames.POST_DETAIL/POST_IMAGES).

작업 내용

  • CaffeineCacheManager 커스터마이징
    • setCacheNames(...)허용 캐시명 화이트리스트 적용(동적 생성 차단)
    • 캐시별 TTL/maximumSizeAppCacheProps(yml 바인딩)로 주입, recordStats() 활성화
  • 도메인 이벤트 → 리스너(after-commit)
    • 이벤트: PostCacheEvent(Kind: UPDATED/DISABLED/DELETED, postId, detailChanged, imagesChanged)
    • 리스너: PostCacheInvalidationListener
      • UPDATED: detailChangedpost:detail만 evict, imagesChangedpost:images만 evict
      • DISABLED: post:detail evict
      • DELETED: post:detail, post:images 모두 evict

참고 사항

  • 일관성 보장: 롤백 시 캐시 변경 없음(무효화는 커밋 이후에만 실행).
  • 운영 안전성: 화이트리스트로 오타/임의 캐시 생성 방지.
  • 튜닝/관측: hit/miss/put/evict/size 지표 기반으로 TTL/size 조정(초기값 예: TTL 60s / size 1k).
  • yml 예시(요지):
    • spring.cache.type=caffeine
    • spring.cache.app.defaults/caches로 per-cache ttlSec, maximumSize 설정

관련 이슈

  • Close #이슈번호

-
- CacheConfig: Caffeine 캐시, 캐시별 정책(최대치/TTL/recordStats)
- Strict 모드: 미등록 캐시 이름 사용 시 fail-fast
- Post 관련 캐시 무효화 이벤트/리스너 기본 구조 추가
-
- PostService: 수정/삭제/비활성화 시 캐시 무효화 이벤트 발행
- PostImageService: 이미지 변경 시 관련 캐시 키 정밀 무효화 신호 전파
- PostImageRepository: 서비스 변경 흐름에 맞춘 시그니처 보완
@hwijae33 hwijae33 self-assigned this Oct 30, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 30, 2025

✅ 테스트 통과!

Copy link
Copy Markdown
Collaborator

@whoamixzerone whoamixzerone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 확인해주세요~

var 제거하고 Cache 등 명시 타입으로 변경

evictIfPresent 헬퍼 추가하여 중복 getCache/null 체크 정리

이벤트/변수 의미 있는 이름으로 리네이밍
Copy link
Copy Markdown
Collaborator

@whoamixzerone whoamixzerone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다.
단순히 캐시만 등록하는게 아닌 다른 규칙들도 고민하고 적용한 것이 느껴지네요 👏

@hwijae33 hwijae33 merged commit 5f0196b into dev Nov 1, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants