add cache file and a unit test
This commit is contained in:
parent
a21ebe5af2
commit
f34056f241
2 changed files with 126 additions and 0 deletions
92
cache/cache.go
vendored
Normal file
92
cache/cache.go
vendored
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GlobalScoreCache *ScoreCache
|
||||||
|
cacheExpiredAfter = 5 * time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerScore struct {
|
||||||
|
expiresAt time.Time
|
||||||
|
Score float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScoreCache struct {
|
||||||
|
stop chan struct{}
|
||||||
|
|
||||||
|
mu sync.RWMutex
|
||||||
|
|
||||||
|
scores map[netip.Addr]ServerScore
|
||||||
|
}
|
||||||
|
|
||||||
|
type CacheMissError struct{}
|
||||||
|
|
||||||
|
func (m *CacheMissError) Error() string {
|
||||||
|
return "User is not in cache!"
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCacheMissError() *CacheMissError {
|
||||||
|
return &CacheMissError{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocalCache() *ScoreCache {
|
||||||
|
lc := &ScoreCache{
|
||||||
|
scores: make(map[netip.Addr]ServerScore),
|
||||||
|
stop: make(chan struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
return lc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *ScoreCache) Add(score float64, ip netip.Addr, ts time.Time) {
|
||||||
|
ssc := ServerScore{Score: score, expiresAt: ts.Add(cacheExpiredAfter)}
|
||||||
|
sc.mu.Lock()
|
||||||
|
sc.scores[ip] = ssc
|
||||||
|
sc.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return ServerScore{}, newCacheMissError()
|
||||||
|
}
|
||||||
|
|
||||||
|
if now.After(cachedScore.expiresAt) {
|
||||||
|
lc.delete(ip)
|
||||||
|
return ServerScore{}, newCacheMissError()
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedScore, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lc *ScoreCache) delete(ip netip.Addr) {
|
||||||
|
lc.mu.Lock()
|
||||||
|
|
||||||
|
delete(lc.scores, ip)
|
||||||
|
lc.mu.Unlock()
|
||||||
|
}
|
34
cache/cache_test.go
vendored
Normal file
34
cache/cache_test.go
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCacheGet(t *testing.T) {
|
||||||
|
const testFloat = 1.23456
|
||||||
|
cache := NewLocalCache()
|
||||||
|
cache.Add(testFloat, netip.MustParseAddr("1.2.3.4"), time.Now().Add(5*time.Minute))
|
||||||
|
|
||||||
|
score, _ := cache.Get(netip.MustParseAddr("1.2.3.4"))
|
||||||
|
if score.Score != testFloat {
|
||||||
|
t.Fatalf("cache.Get(\"1.2.3.4\") = %f, want %f", score.Score, testFloat)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue