Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions decoder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func initDataCache() {
go spawnpointCache.Start()

pokemonCache = ttlcache.New[uint64, Pokemon](
ttlcache.WithTTL[uint64, Pokemon](60*time.Minute),
ttlcache.WithDisableTouchOnHit[uint64, Pokemon](), // Pokemon will last 60 mins from when we first see them not last see them
ttlcache.WithTTL[uint64, Pokemon](10*time.Minute),
ttlcache.WithDisableTouchOnHit[uint64, Pokemon](), // Pokemon will last 10 mins by default but will be adjusted
)
go pokemonCache.Start()
initPokemonRtree()
Expand Down
17 changes: 13 additions & 4 deletions decoder/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ func getPokemonRecord(ctx context.Context, db db.DbDetails, encounterId uint64)
}

if db.UsePokemonCache {
pokemonCache.Set(encounterId, pokemon, ttlcache.DefaultTTL)
remainingDuration := pokemon.remainingDuration(time.Now().Unix())
if remainingDuration <= 0 { // DefaultTTL or PreviousDefaultTTL gives us an hour (longer than ttlcache default)
remainingDuration = time.Hour
}
pokemonCache.Set(encounterId, pokemon, remainingDuration)
}
pokemonRtreeUpdatePokemonOnGet(&pokemon)
return &pokemon, nil
Expand All @@ -174,7 +178,7 @@ func getOrCreatePokemonRecord(ctx context.Context, db db.DbDetails, encounterId
}
pokemon = &Pokemon{Id: encounterId}
if db.UsePokemonCache {
pokemonCache.Set(encounterId, *pokemon, ttlcache.DefaultTTL)
pokemonCache.Set(encounterId, *pokemon, time.Hour) // create new record with an hour TTL initially
}
return pokemon, nil
}
Expand Down Expand Up @@ -539,12 +543,17 @@ func (pokemon *Pokemon) isNewRecord() bool {
return pokemon.FirstSeenTimestamp == 0
}

// remainingDuration calculates a TTL for the pokemon cache based on known expiry
// timestamp - with a minimum of 1 minute to allow the record to stay around and be queried
// without rehydrating from the database post despawn
func (pokemon *Pokemon) remainingDuration(now int64) time.Duration {
remaining := ttlcache.DefaultTTL
remaining := ttlcache.PreviousOrDefaultTTL
if pokemon.ExpireTimestampVerified {
timeLeft := 60 + pokemon.ExpireTimestamp.ValueOrZero() - now
if timeLeft > 1 {
if timeLeft > 60 {
remaining = time.Duration(timeLeft) * time.Second
} else {
remaining = time.Minute
}
}
return remaining
Expand Down