Add in-memory cache #5

Merged
adoralaura merged 6 commits from feature-in-memory-cache into main 2024-04-19 11:55:25 +02:00
Showing only changes of commit 36e9aa49f7 - Show all commits

3
cache/cache.go vendored
View file

@ -69,12 +69,13 @@ func (sc *ScoreCache) Add(score float64, ip netip.Addr, ts time.Time) {
func (lc *ScoreCache) Get(ip netip.Addr) (ServerScore, error) {
now := time.Now()
lc.mu.RLock()
defer lc.mu.RUnlock()
cachedScore, ok := lc.scores[ip]
if !ok {
lc.mu.RUnlock()
return ServerScore{}, newCacheMissError()
}
lc.mu.RUnlock()
if now.After(cachedScore.expiresAt) {
lc.delete(ip)