Skip to content
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

fix: Don't cache outlets #616

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ import java.io.File
import java.math.BigDecimal
import java.time.Duration
import java.time.LocalDate
import kotlin.collections.set
import kotlin.random.Random.Default.nextLong
import kotlin.time.Duration.Companion.minutes
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -193,13 +192,6 @@ class EvearaDistributionRepositoryImpl(
.expireAfterWrite(Duration.ofDays(1L))
.build<Int, GetCountriesResponse>()

private val outletsMutex = Mutex()
private val outletsCache =
Caffeine
.newBuilder()
.expireAfterWrite(Duration.ofDays(1L))
.build<Int, GetOutletsResponse>()

private suspend fun getEvearaApiToken(): String {
getTokenMutex.withLock {
return apiTokenCache.getIfPresent(-1)?.let { apiToken ->
Expand Down Expand Up @@ -329,26 +321,22 @@ class EvearaDistributionRepositoryImpl(
}
}

override suspend fun getOutlets(user: User): GetOutletsResponse =
outletsMutex.withLock {
outletsCache.getIfPresent(-1) ?: run {
val response =
httpClient.get("$evearaApiBaseUrl/outlets") {
accept(ContentType.Application.Json)
bearerAuth(getEvearaApiToken())
parameter("uuid", user.distributionUserId!!)
}
if (!response.status.isSuccess()) {
throw ServerResponseException(response, "Error getting outlets!: ${response.bodyAsText()}")
}
val getOutletsResponse: GetOutletsResponse = response.body()
if (!getOutletsResponse.success) {
throw ServerResponseException(response, "Error getting outlets! success==false")
}

getOutletsResponse.also { outletsCache.put(-1, it) }
override suspend fun getOutlets(user: User): GetOutletsResponse {
val response =
httpClient.get("$evearaApiBaseUrl/outlets") {
accept(ContentType.Application.Json)
bearerAuth(getEvearaApiToken())
parameter("uuid", user.distributionUserId!!)
}
if (!response.status.isSuccess()) {
throw ServerResponseException(response, "Error getting outlets!: ${response.bodyAsText()}")
}
val getOutletsResponse: GetOutletsResponse = response.body()
if (!getOutletsResponse.success) {
throw ServerResponseException(response, "Error getting outlets! success==false")
}
return getOutletsResponse
}

override suspend fun addUser(user: User): AddUserResponse {
requireNotNull(user.firstName) { "User.firstName must not be null!" }
Expand Down