cleaned up caches package

This commit is contained in:
Annika Hannig 2021-10-15 17:17:11 +02:00
parent bb28f4e8d3
commit 164d3e9318
4 changed files with 24 additions and 15 deletions

View File

@ -16,7 +16,7 @@ when we are paginating the results for better client performance.)
// NeighborsCache implements a cache to store neighbors
type NeighborsCache struct {
response *api.NeighboursResponse
response *api.NeighborsResponse
disabled bool
}
@ -32,7 +32,7 @@ func NewNeighborsCache(disabled bool) *NeighborsCache {
// Get retrievs the neighbors response from the cache, if present,
// and makes sure the information is still up to date.
func (cache *NeighborsCache) Get() *api.NeighboursResponse {
func (cache *NeighborsCache) Get() *api.NeighborsResponse {
if cache.disabled {
return nil
}
@ -50,7 +50,7 @@ func (cache *NeighborsCache) Get() *api.NeighboursResponse {
// Set updates the neighbors cache with a new response retrieved
// from a backend source.
func (cache *NeighborsCache) Set(response *api.NeighboursResponse) {
func (cache *NeighborsCache) Set(response *api.NeighborsResponse) {
if cache.disabled {
return
}

View File

@ -14,9 +14,11 @@ NeighborsCache Tests
func TestNeighborsCacheSetGet(t *testing.T) {
cache := NewNeighborsCache(false)
response := &api.NeighboursResponse{
Api: api.ApiStatus{
Ttl: time.Now().UTC().Add(23 * time.Millisecond),
response := &api.NeighborsResponse{
Response: api.Response{
Meta: api.Meta{
TTL: time.Now().UTC().Add(23 * time.Millisecond),
},
},
}

3
pkg/caches/package.go Normal file
View File

@ -0,0 +1,3 @@
// Package caches provides various implementation
// of all sorts of caches.
package caches

View File

@ -11,27 +11,29 @@ func TestRoutesCacheSetGet(t *testing.T) {
cache := NewRoutesCache(false, 2)
response := &api.RoutesResponse{
Api: api.ApiStatus{
Ttl: time.Now().UTC().Add(23 * time.Millisecond),
Response: api.Response{
Meta: api.Meta{
TTL: time.Now().UTC().Add(23 * time.Millisecond),
},
},
}
nId := "neighbor_42"
nID := "neighbor_42"
if cache.Get(nId) != nil {
if cache.Get(nID) != nil {
t.Error("There should not be anything cached yet!")
}
cache.Set(nId, response)
cache.Set(nID, response)
fromCache := cache.Get(nId)
fromCache := cache.Get(nID)
if fromCache != response {
t.Error("Expected", response, "got", fromCache)
}
time.Sleep(33 * time.Millisecond)
fromCache = cache.Get(nId)
fromCache = cache.Get(nID)
if fromCache != nil {
t.Error("Expected empty cache result, got:", fromCache)
}
@ -41,8 +43,10 @@ func TestRoutesCacheLru(t *testing.T) {
cache := NewRoutesCache(false, 2)
response := &api.RoutesResponse{
Api: api.ApiStatus{
Ttl: time.Now().UTC().Add(23 * time.Millisecond),
Response: api.Response{
Meta: api.Meta{
TTL: time.Now().UTC().Add(23 * time.Millisecond),
},
},
}