-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
max-størrelse og levetid på cachingen av PDL
- Loading branch information
1 parent
64c96e9
commit c685a1c
Showing
1 changed file
with
19 additions
and
7 deletions.
There are no files selected for viewing
26 changes: 19 additions & 7 deletions
26
...ensjon/opptjening/omsorgsopptjening/start/innlesning/barnetrygd/domain/PersonIdService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
|
||
} |