From 9ce1ddfefe10debbddea84daa16263cb0a098e63 Mon Sep 17 00:00:00 2001 From: James Berry Date: Sun, 30 Mar 2025 11:04:17 +0100 Subject: [PATCH 1/3] Change cache ttl calculation for pokemon --- decoder/pokemon.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/decoder/pokemon.go b/decoder/pokemon.go index 483e8653..93ceb80a 100644 --- a/decoder/pokemon.go +++ b/decoder/pokemon.go @@ -6,16 +6,17 @@ import ( "encoding/json" "errors" "fmt" + "strconv" + "strings" + "sync" + "time" + "golbat/config" "golbat/db" "golbat/geo" "golbat/grpc" "golbat/pogo" "golbat/webhooks" - "strconv" - "strings" - "sync" - "time" "github.com/UnownHash/gohbem" "github.com/golang/geo/s2" @@ -539,11 +540,13 @@ func (pokemon *Pokemon) isNewRecord() bool { } 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 From 570e0820d138ce9c7532a9590b01e852193cb01a Mon Sep 17 00:00:00 2001 From: James Berry Date: Sun, 30 Mar 2025 11:09:42 +0100 Subject: [PATCH 2/3] Change cache ttl calculation for pokemon --- decoder/pokemon.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/decoder/pokemon.go b/decoder/pokemon.go index 93ceb80a..9cf1c62d 100644 --- a/decoder/pokemon.go +++ b/decoder/pokemon.go @@ -161,7 +161,7 @@ func getPokemonRecord(ctx context.Context, db db.DbDetails, encounterId string) } if db.UsePokemonCache { - pokemonCache.Set(encounterId, pokemon, ttlcache.DefaultTTL) + pokemonCache.Set(encounterId, pokemon, pokemon.remainingDuration(time.Now().Unix())) } pokemonRtreeUpdatePokemonOnGet(&pokemon) return &pokemon, nil @@ -539,6 +539,9 @@ 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.PreviousOrDefaultTTL if pokemon.ExpireTimestampVerified { From 9033c13138e8e7f0f8048a80182d7fc9cf325571 Mon Sep 17 00:00:00 2001 From: James Berry Date: Sun, 30 Mar 2025 15:11:57 +0100 Subject: [PATCH 3/3] Reduce cache ttl to 10 mins to cause expiry to happen more frequently, but boost cache time for initial pokemon record create/load --- decoder/main.go | 2 +- decoder/pokemon.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/decoder/main.go b/decoder/main.go index 765d1849..482cfadd 100644 --- a/decoder/main.go +++ b/decoder/main.go @@ -126,7 +126,7 @@ func initDataCache() { go spawnpointCache.Start() pokemonCache = ttlcache.New[string, Pokemon]( - ttlcache.WithTTL[string, Pokemon](60*time.Minute), + ttlcache.WithTTL[string, Pokemon](10*time.Minute), ttlcache.WithDisableTouchOnHit[string, Pokemon](), // Pokemon will last 60 mins from when we first see them not last see them ) go pokemonCache.Start() diff --git a/decoder/pokemon.go b/decoder/pokemon.go index 9cf1c62d..946a7a91 100644 --- a/decoder/pokemon.go +++ b/decoder/pokemon.go @@ -161,7 +161,11 @@ func getPokemonRecord(ctx context.Context, db db.DbDetails, encounterId string) } if db.UsePokemonCache { - pokemonCache.Set(encounterId, pokemon, pokemon.remainingDuration(time.Now().Unix())) + 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 @@ -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 }