From 65941dac8b0dbffba2ffb5ee0ffa6d0d5c06dc37 Mon Sep 17 00:00:00 2001 From: Andrew Westberg Date: Fri, 29 Nov 2024 13:16:53 +0000 Subject: [PATCH] fix: Don't cache outlets --- .../EvearaDistributionRepositoryImpl.kt | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/newm-server/src/main/kotlin/io/newm/server/features/distribution/eveara/EvearaDistributionRepositoryImpl.kt b/newm-server/src/main/kotlin/io/newm/server/features/distribution/eveara/EvearaDistributionRepositoryImpl.kt index f6868ee0..167d478e 100644 --- a/newm-server/src/main/kotlin/io/newm/server/features/distribution/eveara/EvearaDistributionRepositoryImpl.kt +++ b/newm-server/src/main/kotlin/io/newm/server/features/distribution/eveara/EvearaDistributionRepositoryImpl.kt @@ -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 @@ -193,13 +192,6 @@ class EvearaDistributionRepositoryImpl( .expireAfterWrite(Duration.ofDays(1L)) .build() - private val outletsMutex = Mutex() - private val outletsCache = - Caffeine - .newBuilder() - .expireAfterWrite(Duration.ofDays(1L)) - .build() - private suspend fun getEvearaApiToken(): String { getTokenMutex.withLock { return apiTokenCache.getIfPresent(-1)?.let { apiToken -> @@ -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!" }