-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor/#194] UserProfile 데이터 관리 최적화 #195
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
46ea194
Refactor: UserDataSource를 UserRemoteDataSource와 UserLocalDataSource로 분리
wjdrjs00 9236266
Feat: UserRepository 내 캐싱 로직 및 Mutex를 이용한 Race Condition 방지 로직 구현
wjdrjs00 1b40a14
Refactor: FetchUserProfileUseCase를 ObserveUserProfileUseCase(Flow)로 변경
wjdrjs00 35bf092
Feat: 로그아웃 및 회원탈퇴 성공 시 유저 정보 캐시를 초기화하도록 수정
wjdrjs00 58e9b3c
Test: UserRepositoryImpl에 대한 단위 테스트 추가 (캐싱, 동시성, 캐시 초기화 검증)
wjdrjs00 e5c743e
Refactor: Home, MyPage, OnBoarding ViewModel에서 유저 정보를 Flow로 구독하도록 변경
wjdrjs00 fae4788
Feat: userProfile 단일 조회를 위한 GetUserProfileUseCase 추가
wjdrjs00 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
10 changes: 10 additions & 0 deletions
10
data/src/main/java/com/threegap/bitnagil/data/user/datasource/UserLocalDataSource.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,10 @@ | ||
| package com.threegap.bitnagil.data.user.datasource | ||
|
|
||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
||
| interface UserLocalDataSource { | ||
| val userProfile: StateFlow<UserProfile?> | ||
| suspend fun saveUserProfile(userProfile: UserProfile) | ||
| fun clearCache() | ||
| } |
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
24 changes: 24 additions & 0 deletions
24
data/src/main/java/com/threegap/bitnagil/data/user/datasourceImpl/UserLocalDataSourceImpl.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,24 @@ | ||
| package com.threegap.bitnagil.data.user.datasourceImpl | ||
|
|
||
| import com.threegap.bitnagil.data.user.datasource.UserLocalDataSource | ||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class UserLocalDataSourceImpl @Inject constructor() : UserLocalDataSource { | ||
| private val _userProfile = MutableStateFlow<UserProfile?>(null) | ||
| override val userProfile: StateFlow<UserProfile?> = _userProfile.asStateFlow() | ||
|
|
||
| override suspend fun saveUserProfile(userProfile: UserProfile) { | ||
| _userProfile.update { userProfile } | ||
| } | ||
|
|
||
| override fun clearCache() { | ||
| _userProfile.update { null } | ||
| } | ||
| } |
10 changes: 4 additions & 6 deletions
10
...user/datasourceImpl/UserDataSourceImpl.kt → ...atasourceImpl/UserRemoteDataSourceImpl.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 |
|---|---|---|
| @@ -1,16 +1,14 @@ | ||
| package com.threegap.bitnagil.data.user.datasourceImpl | ||
|
|
||
| import com.threegap.bitnagil.data.common.safeApiCall | ||
| import com.threegap.bitnagil.data.user.datasource.UserDataSource | ||
| import com.threegap.bitnagil.data.user.datasource.UserRemoteDataSource | ||
| import com.threegap.bitnagil.data.user.model.response.UserProfileResponse | ||
| import com.threegap.bitnagil.data.user.service.UserService | ||
| import javax.inject.Inject | ||
|
|
||
| class UserDataSourceImpl @Inject constructor( | ||
| class UserRemoteDataSourceImpl @Inject constructor( | ||
| private val userService: UserService, | ||
| ) : UserDataSource { | ||
| ) : UserRemoteDataSource { | ||
| override suspend fun fetchUserProfile(): Result<UserProfileResponse> = | ||
| safeApiCall { | ||
| userService.fetchUserProfile() | ||
| } | ||
| safeApiCall { userService.fetchUserProfile() } | ||
| } |
53 changes: 49 additions & 4 deletions
53
data/src/main/java/com/threegap/bitnagil/data/user/repositoryImpl/UserRepositoryImpl.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 |
|---|---|---|
| @@ -1,14 +1,59 @@ | ||
| package com.threegap.bitnagil.data.user.repositoryImpl | ||
|
|
||
| import com.threegap.bitnagil.data.user.datasource.UserDataSource | ||
| import com.threegap.bitnagil.data.user.datasource.UserLocalDataSource | ||
| import com.threegap.bitnagil.data.user.datasource.UserRemoteDataSource | ||
| import com.threegap.bitnagil.data.user.model.response.toDomain | ||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import com.threegap.bitnagil.domain.user.repository.UserRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.emitAll | ||
| import kotlinx.coroutines.flow.filterNotNull | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class UserRepositoryImpl @Inject constructor( | ||
| private val userDataSource: UserDataSource, | ||
| private val userLocalDataSource: UserLocalDataSource, | ||
| private val userRemoteDataSource: UserRemoteDataSource, | ||
| ) : UserRepository { | ||
| override suspend fun fetchUserProfile(): Result<UserProfile> = | ||
| userDataSource.fetchUserProfile().map { it.toDomain() } | ||
| private val fetchMutex = Mutex() | ||
|
|
||
| override fun observeUserProfile(): Flow<Result<UserProfile>> = flow { | ||
| fetchAndCacheIfNeeded().onFailure { | ||
| emit(Result.failure(it)) | ||
| return@flow | ||
| } | ||
|
|
||
| emitAll( | ||
| userLocalDataSource.userProfile | ||
| .filterNotNull() | ||
| .map { Result.success(it) }, | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun getUserProfile(): Result<UserProfile> { | ||
| return fetchAndCacheIfNeeded() | ||
| } | ||
|
|
||
| override fun clearCache() { | ||
| userLocalDataSource.clearCache() | ||
| } | ||
|
|
||
| private suspend fun fetchAndCacheIfNeeded(): Result<UserProfile> { | ||
| userLocalDataSource.userProfile.value?.let { return Result.success(it) } | ||
|
|
||
| return fetchMutex.withLock { | ||
| userLocalDataSource.userProfile.value?.let { return@withLock Result.success(it) } | ||
|
|
||
| userRemoteDataSource.fetchUserProfile() | ||
| .onSuccess { response -> | ||
| userLocalDataSource.saveUserProfile(response.toDomain()) | ||
| } | ||
| .map { it.toDomain() } | ||
| } | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
data/src/test/java/com/threegap/bitnagil/data/user/repositoryImpl/UserRepositoryImplTest.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,126 @@ | ||
| package com.threegap.bitnagil.data.user.repositoryImpl | ||
|
|
||
| import com.threegap.bitnagil.data.user.datasource.UserLocalDataSource | ||
| import com.threegap.bitnagil.data.user.datasource.UserRemoteDataSource | ||
| import com.threegap.bitnagil.data.user.model.response.UserProfileResponse | ||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import com.threegap.bitnagil.domain.user.repository.UserRepository | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.awaitAll | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| class UserRepositoryImplTest { | ||
|
|
||
| private lateinit var localDataSource: FakeUserLocalDataSource | ||
| private lateinit var remoteDataSource: FakeUserRemoteDataSource | ||
| private lateinit var userRepository: UserRepository | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| localDataSource = FakeUserLocalDataSource() | ||
| remoteDataSource = FakeUserRemoteDataSource() | ||
| userRepository = UserRepositoryImpl(localDataSource, remoteDataSource) | ||
| } | ||
|
|
||
| @Test | ||
| fun `캐시가 비어있을 때 observeUserProfile을 구독하면 Remote에서 데이터를 가져와 캐시를 업데이트해야 한다`() = | ||
| runTest { | ||
| // given | ||
| val expectedProfile = UserProfile(nickname = "TestUser") | ||
| remoteDataSource.profileResponse = UserProfileResponse(nickname = "TestUser") | ||
|
|
||
| // when | ||
| // 구독(first)이 시작되는 순간 Fetch가 발생함 | ||
| val result = userRepository.observeUserProfile().first() | ||
|
|
||
| // then | ||
| assertEquals(expectedProfile, result.getOrNull()) | ||
| assertEquals(1, remoteDataSource.fetchCount.get()) | ||
| assertEquals(expectedProfile, localDataSource.userProfile.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `캐시가 이미 존재할 때 observeUserProfile을 구독하면 Remote를 호출하지 않고 캐시를 반환해야 한다`() = | ||
| runTest { | ||
| // given | ||
| val cachedProfile = UserProfile(nickname = "CachedUser") | ||
| localDataSource.saveUserProfile(cachedProfile) | ||
|
|
||
| // when | ||
| val result = userRepository.observeUserProfile().first() | ||
|
|
||
| // then | ||
| assertEquals(cachedProfile, result.getOrNull()) | ||
| assertEquals(0, remoteDataSource.fetchCount.get()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `여러 코루틴이 동시에 observeUserProfile을 구독해도 Remote API는 1회만 호출되어야 한다 (Race Condition 방지)`() = | ||
| runTest { | ||
| // given | ||
| remoteDataSource.profileResponse = UserProfileResponse(nickname = "RaceUser") | ||
| remoteDataSource.delayMillis = 100L // 네트워크 지연 시뮬레이션 | ||
|
|
||
| // when | ||
| // 10개의 코루틴이 동시에 구독 시작 | ||
| val jobs = List(10) { | ||
| async { userRepository.observeUserProfile().first() } | ||
| } | ||
| jobs.awaitAll() | ||
|
|
||
| // then | ||
| assertEquals(1, remoteDataSource.fetchCount.get()) | ||
| assertEquals("RaceUser", localDataSource.userProfile.value?.nickname) | ||
| } | ||
|
|
||
| @Test | ||
| fun `clearCache를 호출하면 로컬 캐시가 초기화되어야 한다`() = | ||
| runTest { | ||
| // given | ||
| localDataSource.saveUserProfile(UserProfile(nickname = "ToDelete")) | ||
|
|
||
| // when | ||
| userRepository.clearCache() | ||
|
|
||
| // then | ||
| assertEquals(null, localDataSource.userProfile.value) | ||
| } | ||
|
|
||
| // --- Fake Objects --- | ||
|
|
||
| private class FakeUserLocalDataSource : UserLocalDataSource { | ||
| private val _userProfile = MutableStateFlow<UserProfile?>(null) | ||
| override val userProfile: StateFlow<UserProfile?> = _userProfile.asStateFlow() | ||
|
|
||
| override suspend fun saveUserProfile(userProfile: UserProfile) { | ||
| _userProfile.update { userProfile } | ||
| } | ||
|
|
||
| override fun clearCache() { | ||
| _userProfile.update { null } | ||
| } | ||
| } | ||
|
|
||
| private class FakeUserRemoteDataSource : UserRemoteDataSource { | ||
| var profileResponse: UserProfileResponse? = null | ||
| val fetchCount = AtomicInteger(0) | ||
| var delayMillis = 0L | ||
|
|
||
| override suspend fun fetchUserProfile(): Result<UserProfileResponse> { | ||
| if (delayMillis > 0) delay(delayMillis) | ||
| fetchCount.incrementAndGet() | ||
| return profileResponse?.let { Result.success(it) } | ||
| ?: Result.failure(Exception("No profile set in fake")) | ||
| } | ||
| } | ||
| } |
7 changes: 6 additions & 1 deletion
7
domain/src/main/java/com/threegap/bitnagil/domain/auth/usecase/LogoutUseCase.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 |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| package com.threegap.bitnagil.domain.auth.usecase | ||
|
|
||
| import com.threegap.bitnagil.domain.auth.repository.AuthRepository | ||
| import com.threegap.bitnagil.domain.user.repository.UserRepository | ||
| import javax.inject.Inject | ||
|
|
||
| class LogoutUseCase @Inject constructor( | ||
| private val authRepository: AuthRepository, | ||
| private val userRepository: UserRepository, | ||
| ) { | ||
| suspend operator fun invoke(): Result<Unit> = authRepository.logout() | ||
| suspend operator fun invoke(): Result<Unit> = | ||
| authRepository.logout().onSuccess { | ||
| userRepository.clearCache() | ||
| } | ||
| } |
7 changes: 6 additions & 1 deletion
7
domain/src/main/java/com/threegap/bitnagil/domain/auth/usecase/WithdrawalUseCase.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 |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| package com.threegap.bitnagil.domain.auth.usecase | ||
|
|
||
| import com.threegap.bitnagil.domain.auth.repository.AuthRepository | ||
| import com.threegap.bitnagil.domain.user.repository.UserRepository | ||
| import javax.inject.Inject | ||
|
|
||
| class WithdrawalUseCase @Inject constructor( | ||
| private val authRepository: AuthRepository, | ||
| private val userRepository: UserRepository, | ||
| ) { | ||
| suspend operator fun invoke(reason: String): Result<Unit> = authRepository.withdrawal(reason) | ||
| suspend operator fun invoke(reason: String): Result<Unit> = | ||
| authRepository.withdrawal(reason).onSuccess { | ||
| userRepository.clearCache() | ||
| } | ||
| } |
5 changes: 4 additions & 1 deletion
5
domain/src/main/java/com/threegap/bitnagil/domain/user/repository/UserRepository.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 |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| package com.threegap.bitnagil.domain.user.repository | ||
|
|
||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| interface UserRepository { | ||
| suspend fun fetchUserProfile(): Result<UserProfile> | ||
| fun observeUserProfile(): Flow<Result<UserProfile>> | ||
| suspend fun getUserProfile(): Result<UserProfile> | ||
| fun clearCache() | ||
| } | ||
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
12 changes: 12 additions & 0 deletions
12
domain/src/main/java/com/threegap/bitnagil/domain/user/usecase/ObserveUserProfileUseCase.kt
l5x5l marked this conversation as resolved.
Show resolved
Hide resolved
|
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,12 @@ | ||
| package com.threegap.bitnagil.domain.user.usecase | ||
|
|
||
| import com.threegap.bitnagil.domain.user.model.UserProfile | ||
| import com.threegap.bitnagil.domain.user.repository.UserRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import javax.inject.Inject | ||
|
|
||
| class ObserveUserProfileUseCase @Inject constructor( | ||
| private val userRepository: UserRepository, | ||
| ) { | ||
| operator fun invoke(): Flow<Result<UserProfile>> = userRepository.observeUserProfile() | ||
| } |
Oops, something went wrong.
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.