Skip to content

Commit 26160ae

Browse files
authored
feat : Redis fallback 패턴 구현 (#280)
1 parent c19e4c8 commit 26160ae

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

application/src/main/kotlin/com/pokit/user/port/service/UserService.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ class UserService(
117117
}
118118

119119
override fun getUserInfo(userId: Long): User {
120-
return userCachePort.loadById(userId)
121-
?: userPort.loadById(userId)?.also { userCachePort.persist(it) }
120+
cacheOrNull { userCachePort.loadById(userId) }
121+
?.let { return it }
122+
123+
return userPort.loadById(userId)
124+
?.also { tryCachePersist(it) }
122125
?: throw NotFoundCustomException(UserErrorCode.NOT_FOUND_USER)
123126
}
124127

@@ -161,4 +164,12 @@ class UserService(
161164
interestPort.persist(Interest(userId = userId, interestType = it))
162165
}
163166
}
167+
168+
// redis fallback
169+
private inline fun <T> cacheOrNull(block: () -> T?): T? =
170+
runCatching(block).getOrNull()
171+
172+
private fun tryCachePersist(user: User) {
173+
runCatching { userCachePort.persist(user) }
174+
}
164175
}

0 commit comments

Comments
 (0)