Skip to content

Commit

Permalink
max-størrelse og levetid på cachingen av PDL
Browse files Browse the repository at this point in the history
  • Loading branch information
jankroken-nav committed Oct 24, 2024
1 parent 64c96e9 commit c685a1c
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, PersonId> = mutableMapOf()
val cache =
CacheBuilder.newBuilder()
.expireAfterWrite(Duration.ofMinutes(5))
.maximumSize(30)
.build<String, PersonId>()

// 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]
}

}

0 comments on commit c685a1c

Please sign in to comment.