Skip to content

Commit

Permalink
fix: use a new store for different messages (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
scorix authored Dec 13, 2024
1 parent ef7e12e commit 4f26dab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pkg/grib2/cache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/grib2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down

0 comments on commit 4f26dab

Please sign in to comment.