diff --git a/pkg/grib2/cache/store.go b/pkg/grib2/cache/store.go index 19b10e8..fb9c756 100644 --- a/pkg/grib2/cache/store.go +++ b/pkg/grib2/cache/store.go @@ -30,7 +30,7 @@ type entry struct { func NewLRUStore(capacity int) Store { return &lruStore{ capacity: capacity, - cache: make(map[int]*list.Element), + cache: make(map[int]*list.Element, capacity), lru: list.New(), } } @@ -39,14 +39,14 @@ func (l *lruStore) Get(ctx context.Context, key int) (float32, bool) { l.mu.RLock() defer l.mu.RUnlock() + sp := trace.SpanFromContext(ctx) + if elem, ok := l.cache[key]; ok { l.lru.MoveToFront(elem) - sp := trace.SpanFromContext(ctx) sp.SetAttributes(attribute.Int("cache.grid", key), attribute.Bool("cache.hit", true)) return elem.Value.(*entry).value, true } - sp := trace.SpanFromContext(ctx) sp.SetAttributes(attribute.Int("cache.grid", key), attribute.Bool("cache.hit", false)) return 0, false } diff --git a/pkg/grib2/message.go b/pkg/grib2/message.go index 3afbc08..5602647 100644 --- a/pkg/grib2/message.go +++ b/pkg/grib2/message.go @@ -261,15 +261,15 @@ func NewSimplePackingMessageReaderFromMessage(r io.ReaderAt, m IndexedMessage, o type SimplePackingMessageReaderOptions func(r *simplePackingMessageReader) -func WithBoundaryCache(minLat, maxLat, minLon, maxLon float32, store cache.Store) SimplePackingMessageReaderOptions { +func WithBoundaryCache(minLat, maxLat, minLon, maxLon float32, newStore func() cache.Store) SimplePackingMessageReaderOptions { return func(r *simplePackingMessageReader) { - r.cache = cache.NewBoundary(minLat, maxLat, minLon, maxLon, r.spr, store) + r.cache = cache.NewBoundary(minLat, maxLat, minLon, maxLon, r.spr, newStore()) } } -func WithCustomCacheStrategy(inCache func(lat, lon float32) bool, store cache.Store) SimplePackingMessageReaderOptions { +func WithCustomCacheStrategy(inCache func(lat, lon float32) bool, newStore func() cache.Store) SimplePackingMessageReaderOptions { return func(r *simplePackingMessageReader) { - r.cache = cache.NewCustom(inCache, r.spr, store) + r.cache = cache.NewCustom(inCache, r.spr, newStore()) } }