-
Notifications
You must be signed in to change notification settings - Fork 1
[feat] Spring Cache(Caffeine) 도입 — 화이트리스트 기반 Strict + after-commit 이벤트 무효화 #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df48867
build: 캐시 도입을 위한 Spring Cache/Caffeine 의존성 추가
hwijae33 73bb287
feat(cache): strict CacheManager와 캐시명 화이트리스트 도입 및 이벤트 기반 무효화 스켈레톤 추가
hwijae33 74aeaee
refactor(posts): @Cacheable 적용 및 after-commit 이벤트 기반 캐시 무효화 연동
hwijae33 704ba81
test: 캐시 히트/미스 및 after-commit 무효화 시나리오 추가
hwijae33 bb1c5ea
style: 불필요한 주석/미사용 import 제거 및 코드 포매팅 (동작 변경 없음)
hwijae33 37dc787
refactor(cache): PostCacheInvalidationListener 명시 타입 적용 및 가독성 개선
hwijae33 dd123e6
style(cache): 불필요한 FQCN 제거(CaffeineCache), 가독성 개선
hwijae33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package kr.co.pinup.cache; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @ConfigurationProperties(prefix ="spring.cache.app") | ||
| public record AppCacheProps( | ||
| Defaults defaults, | ||
| Map<String, Spec> caches | ||
| ) { | ||
| public record Defaults(Long maximumSize, Integer ttlSec) {} | ||
| public record Spec(Long maximumSize, Integer ttlSec) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package kr.co.pinup.cache; | ||
|
|
||
| public interface CacheNames { | ||
|
hwijae33 marked this conversation as resolved.
|
||
| String POST_DETAIL = "post:detail"; | ||
| String POST_IMAGES = "post:images"; | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/main/java/kr/co/pinup/cache/listener/PostCacheInvalidationListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package kr.co.pinup.cache.listener; | ||
|
|
||
| import kr.co.pinup.cache.CacheNames; | ||
| import kr.co.pinup.posts.event.PostCacheEvent; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PostCacheInvalidationListener { | ||
|
|
||
| private final CacheManager cacheManager; | ||
|
|
||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
|
hwijae33 marked this conversation as resolved.
|
||
| public void on(PostCacheEvent event) { | ||
| switch (event.kind()) { | ||
| case UPDATED -> { | ||
| if (event.detailChanged()) { | ||
| evictIfPresent(CacheNames.POST_DETAIL, event.postId()); | ||
| } | ||
| if (event.imagesChanged()) { | ||
| evictIfPresent(CacheNames.POST_IMAGES, event.postId()); | ||
| } | ||
| } | ||
| case DISABLED -> { | ||
| evictIfPresent(CacheNames.POST_DETAIL, event.postId()); | ||
| } | ||
| case DELETED -> { | ||
| evictIfPresent(CacheNames.POST_DETAIL, event.postId()); | ||
| evictIfPresent(CacheNames.POST_IMAGES, event.postId()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void evictIfPresent(String cacheName, Object key) { | ||
| Cache cache = cacheManager.getCache(cacheName); | ||
| if (cache != null) { | ||
| cache.evict(key); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package kr.co.pinup.config; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import kr.co.pinup.cache.AppCacheProps; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.caffeine.CaffeineCache; | ||
| import org.springframework.cache.caffeine.CaffeineCacheManager; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @EnableConfigurationProperties(AppCacheProps.class) | ||
| public class CacheConfig { | ||
|
|
||
| @Bean | ||
| public CacheManager cacheManager(AppCacheProps props) { | ||
|
|
||
| return new CaffeineCacheManager() { | ||
| { | ||
| setAllowNullValues(false); | ||
| setCacheNames(props.caches().keySet()); | ||
| } | ||
|
|
||
| @Override | ||
| protected CaffeineCache createCaffeineCache(String name) { | ||
| Caffeine<Object, Object> builder = Caffeine.newBuilder().recordStats(); | ||
|
|
||
| var d = props.defaults(); | ||
| var s = props.caches().get(name); | ||
|
|
||
| Long maxSize = (s != null && s.maximumSize() != null) | ||
| ? s.maximumSize() | ||
| : (d != null ? d.maximumSize() : null); | ||
|
|
||
| Integer ttlSec = (s != null && s.ttlSec() != null) | ||
| ? s.ttlSec() | ||
| : (d != null ? d.ttlSec() : null); | ||
|
|
||
| if (maxSize != null) builder = builder.maximumSize(maxSize); | ||
| if (ttlSec != null) builder = builder.expireAfterWrite(java.time.Duration.ofSeconds(ttlSec)); | ||
|
|
||
| return new CaffeineCache(name, builder.build(), false); | ||
| } | ||
|
|
||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package kr.co.pinup.posts.event; | ||
|
|
||
| public record PostCacheEvent( | ||
| Long postId, | ||
| Kind kind, | ||
| boolean detailChanged, // UPDATED일 때만 의미 | ||
| boolean imagesChanged // UPDATED일 때만 의미 | ||
| ) { | ||
| public enum Kind { UPDATED, DISABLED, DELETED } | ||
|
|
||
| public static PostCacheEvent updated(Long postId, boolean detailChanged, boolean imagesChanged) { | ||
| return new PostCacheEvent(postId, Kind.UPDATED, detailChanged, imagesChanged); | ||
| } | ||
| public static PostCacheEvent disabled(Long postId) { | ||
| return new PostCacheEvent(postId, Kind.DISABLED, false, false); | ||
| } | ||
| public static PostCacheEvent deleted(Long postId) { | ||
| return new PostCacheEvent(postId, Kind.DELETED, false, false); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.