-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#120 : media redis cache #124
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
16 commits
Select commit
Hold shift + click to select a range
dd068ae
feat: 이미지 조회를 위한 분산락 추가
koosco bfaa160
feat: redis media 캐시 어댑터 추가
koosco a38196b
ref: 메서드 시그니처 변경에 따른 media fake 구현체 메서드 수정
koosco 2f55e06
chore: 사용하지 않는 클래스 제거
koosco 8b0006a
feat: media 조회 usecase 분산락 추가
koosco 279f10c
feat: redis binary template 추가
koosco 692f63d
feat: async config 추가
koosco d95281b
fix: Redis 분산락 test profile 추가
koosco f5b37c2
fix: media S3 fallback 후 cache 재확인
koosco 14882f8
Merge branch 'staging' into feat/#120
koosco b321223
fix: RedisMediaBinaryCacheAdapter nullable type 수정
koosco 160ce3a
fix: media 캐시 ttl 만료 전 expire로 갱신하도록 수정
koosco 9a8d9f3
fix: async 설정 제거
koosco 0fdd1f6
fix: media type별 캐시 ttl 적용
koosco 232a1ff
Merge branch 'staging' into feat/#120
koosco d102ecc
fix: 분산락 single-flight 제거
koosco 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
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/yapp2app/media/application/port/DistributedLockPort.kt
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,13 @@ | ||
| package com.yapp2app.media.application.port | ||
|
|
||
| import java.time.Duration | ||
|
|
||
| /** | ||
| * 분산 락 포트 | ||
| * | ||
| * 여러 인스턴스 간 동시성 제어를 위한 분산 락 인터페이스입니다. | ||
| */ | ||
| interface DistributedLockPort { | ||
|
|
||
| fun <T> executeWithLock(key: String, ttl: Duration, action: () -> T): T? | ||
| } |
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
64 changes: 56 additions & 8 deletions
64
src/main/kotlin/com/yapp2app/media/application/usecase/GetImageByKeyUseCase.kt
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 |
|---|---|---|
| @@ -1,40 +1,54 @@ | ||
| package com.yapp2app.media.domain | ||
|
|
||
| import java.time.Duration | ||
|
|
||
| /** | ||
| * fileName : MediaType | ||
| * author : koo | ||
| * date : 2025. 12. 19. 오전 2:41 | ||
| * description : 이미지 저장 type | ||
| */ | ||
| enum class MediaType(val prefix: String) { | ||
| enum class MediaType(val prefix: String, val cacheTtl: Duration?) { | ||
|
|
||
| /** | ||
| * 사용자 프로필 | ||
| */ | ||
| USER_PROFILE("user-profiles"), | ||
| USER_PROFILE("user-profiles", Duration.ofHours(24)), | ||
|
|
||
| /** | ||
| * 인생네컷 | ||
| */ | ||
| PHOTO_BOOTH("photo-booth"), | ||
| PHOTO_BOOTH("photo-booth", null), | ||
|
|
||
| /** | ||
| * 확장성을 고려한 첨부 이미지 | ||
| */ | ||
| ATTACHMENT("attachments"), | ||
| ATTACHMENT("attachments", null), | ||
|
|
||
| /** | ||
| * 로고 이미지 | ||
| */ | ||
| LOGO("logo"), | ||
| LOGO("logo", Duration.ofHours(24)), | ||
|
|
||
| /** | ||
| * 포즈 이미지 | ||
| */ | ||
| POSE("pose"), | ||
| POSE("pose", Duration.ofHours(24)), | ||
|
|
||
| /** | ||
| * 업로드 검증, 테스트 등 | ||
| */ | ||
| TEMP("temp"), | ||
| TEMP("temp", null), | ||
| ; | ||
|
|
||
| val isCacheable: Boolean get() = cacheTtl != null | ||
|
|
||
| companion object { | ||
| private val PREFIX_MAP = entries.associateBy { it.prefix } | ||
|
|
||
| fun fromObjectKey(objectKey: String): MediaType? { | ||
| val prefix = objectKey.substringBefore('/') | ||
| return PREFIX_MAP[prefix] | ||
| } | ||
| } | ||
| } |
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/com/yapp2app/media/infra/cache/FakeMediaBinaryCacheAdapter.kt
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/com/yapp2app/media/infra/cache/fake/FakeMediaBinaryCacheAdapter.kt
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,65 @@ | ||
| package com.yapp2app.media.infra.cache.fake | ||
|
|
||
| import com.yapp2app.media.application.port.MediaBinaryCachePort | ||
| import org.springframework.context.annotation.Profile | ||
| import org.springframework.stereotype.Component | ||
| import java.time.Duration | ||
| import java.time.Instant | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| /** | ||
| * fileName : FakeMediaBinaryAdapter | ||
| * author : koo | ||
| * date : 2026. 1. 8. 오후 4:20 | ||
| * description : TTL 추적이 가능한 In-Memory 캐시 어댑터 (테스트 환경 전용) | ||
| */ | ||
| @Component | ||
| @Profile("test") | ||
| class FakeMediaBinaryCacheAdapter : MediaBinaryCachePort { | ||
|
|
||
| private val cache = ConcurrentHashMap<String, CacheEntry>() | ||
|
|
||
| data class CacheEntry(val data: ByteArray, val expiresAt: Instant) { | ||
| fun isExpired(): Boolean = Instant.now().isAfter(expiresAt) | ||
|
|
||
| fun remainingTtl(): Duration = | ||
| Duration.between(Instant.now(), expiresAt).takeIf { !it.isNegative } ?: Duration.ZERO | ||
| } | ||
|
|
||
| override fun get(key: String): ByteArray? { | ||
| val entry = cache[key] ?: return null | ||
| return if (entry.isExpired()) { | ||
| cache.remove(key) | ||
| null | ||
| } else { | ||
| entry.data | ||
| } | ||
| } | ||
|
|
||
| override fun put(key: String, value: ByteArray, ttl: Duration) { | ||
| cache[key] = CacheEntry( | ||
| data = value, | ||
| expiresAt = Instant.now().plus(ttl), | ||
| ) | ||
| } | ||
|
|
||
| override fun evict(key: String) { | ||
| cache.remove(key) | ||
| } | ||
|
|
||
| // 테스트용 메서드 | ||
| fun putWithTtl(key: String, value: ByteArray, ttl: Duration) { | ||
| cache[key] = CacheEntry( | ||
| data = value, | ||
| expiresAt = Instant.now().plus(ttl), | ||
| ) | ||
| } | ||
|
|
||
| fun getRemainingTtl(key: String): Duration? = cache[key]?.remainingTtl() | ||
|
|
||
| fun clearAll() { | ||
| cache.clear() | ||
| } | ||
|
|
||
| fun size(): Int = cache.size | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/yapp2app/media/infra/cache/redis/MediaRedisCacheKey.kt
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,13 @@ | ||
| package com.yapp2app.media.infra.cache.redis | ||
|
|
||
| /** | ||
| * Redis 캐시 키 네이밍 컨벤션 관리 | ||
| * | ||
| * Media 도메인의 Redis 키 포맷을 중앙 관리합니다. | ||
| * 포맷: media:binary:{objectKey} | ||
| */ | ||
| internal object MediaRedisCacheKey { | ||
| private const val BINARY_PREFIX = "media:binary:" | ||
|
|
||
| fun binaryKey(objectKey: String): String = "$BINARY_PREFIX$objectKey" | ||
| } |
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.