From c685a1c004579f2e0671b791b24c89a6ba7b8c5e Mon Sep 17 00:00:00 2001 From: Jan Kroken Date: Thu, 24 Oct 2024 13:51:47 +0200 Subject: [PATCH] =?UTF-8?q?max-st=C3=B8rrelse=20og=20levetid=20p=C3=A5=20c?= =?UTF-8?q?achingen=20av=20PDL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../barnetrygd/domain/PersonIdService.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/no/nav/pensjon/opptjening/omsorgsopptjening/start/innlesning/barnetrygd/domain/PersonIdService.kt b/src/main/kotlin/no/nav/pensjon/opptjening/omsorgsopptjening/start/innlesning/barnetrygd/domain/PersonIdService.kt index 5cca51b3..21b5f11d 100644 --- a/src/main/kotlin/no/nav/pensjon/opptjening/omsorgsopptjening/start/innlesning/barnetrygd/domain/PersonIdService.kt +++ b/src/main/kotlin/no/nav/pensjon/opptjening/omsorgsopptjening/start/innlesning/barnetrygd/domain/PersonIdService.kt @@ -1,23 +1,35 @@ package no.nav.pensjon.opptjening.omsorgsopptjening.start.innlesning.barnetrygd.domain +import com.google.common.cache.CacheBuilder import no.nav.pensjon.opptjening.omsorgsopptjening.start.innlesning.barnetrygd.external.pdl.PdlService import org.springframework.stereotype.Service +import java.time.Duration @Service class PersonIdService( val pdlService: PdlService, ) { - val personIdMap: MutableMap = mutableMapOf() + val cache = + CacheBuilder.newBuilder() + .expireAfterWrite(Duration.ofMinutes(5)) + .maximumSize(30) + .build() - // TODO: håndter personer som ikke finnes i PDL fun personFromIdent(fnr: String): PersonId? { - if (!personIdMap.containsKey(fnr)) { - val personId = pdlService.hentPerson(fnr) - personId.historiske.forEach { - personIdMap[it] = personId + return when (val personId: PersonId? = cache.getIfPresent(fnr)) { + is PersonId -> personId + null -> { + val personId = pdlService.hentPerson(fnr) + personId.historiske.forEach { + cache.put(it, personId) + } + personId + } + + else -> { // noop, but added to make this compile + throw RuntimeException("cache contains unrecognized value $personId") } } - return personIdMap[fnr] } } \ No newline at end of file