Compare commits
No commits in common. "2a574d6f3b832c332295e9c733b227d8f1ec58e0" and "c713e16ed23a2d4f26d25782b24b6258303375c2" have entirely different histories.
2a574d6f3b
...
c713e16ed2
2 changed files with 1 additions and 17 deletions
3
cache/cache.go
vendored
3
cache/cache.go
vendored
|
@ -69,13 +69,12 @@ func (sc *ScoreCache) Add(score float64, ip netip.Addr, ts time.Time) {
|
||||||
func (lc *ScoreCache) Get(ip netip.Addr) (ServerScore, error) {
|
func (lc *ScoreCache) Get(ip netip.Addr) (ServerScore, error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
lc.mu.RLock()
|
lc.mu.RLock()
|
||||||
|
defer lc.mu.RUnlock()
|
||||||
|
|
||||||
cachedScore, ok := lc.scores[ip]
|
cachedScore, ok := lc.scores[ip]
|
||||||
if !ok {
|
if !ok {
|
||||||
lc.mu.RUnlock()
|
|
||||||
return ServerScore{}, newCacheMissError()
|
return ServerScore{}, newCacheMissError()
|
||||||
}
|
}
|
||||||
lc.mu.RUnlock()
|
|
||||||
|
|
||||||
if now.After(cachedScore.expiresAt) {
|
if now.After(cachedScore.expiresAt) {
|
||||||
lc.delete(ip)
|
lc.delete(ip)
|
||||||
|
|
15
cache/cache_test.go
vendored
15
cache/cache_test.go
vendored
|
@ -32,18 +32,3 @@ func TestCacheGet(t *testing.T) {
|
||||||
t.Fatalf("cache.Get(\"1.2.3.4\") = %f, want %f", score.Score, testFloat)
|
t.Fatalf("cache.Get(\"1.2.3.4\") = %f, want %f", score.Score, testFloat)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCacheMiss(t *testing.T) {
|
|
||||||
now := time.Now()
|
|
||||||
const testFloat = 1.23456
|
|
||||||
|
|
||||||
expiredTime := now.Add(-(time.Minute * 10))
|
|
||||||
|
|
||||||
cache := NewLocalCache()
|
|
||||||
cache.Add(testFloat, netip.MustParseAddr("1.2.3.4"), expiredTime)
|
|
||||||
|
|
||||||
_, err := cache.Get(netip.MustParseAddr("1.2.3.4"))
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("cache.Get(\"1.2.3.4\") = got nil, want error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue