Skip to content

Commit

Permalink
proper record lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed Dec 24, 2024
1 parent c2d8f33 commit 4b6728e
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions api/subscriptions/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// messageCache is a generic cache that stores messages of any type.
type messageCache[T any] struct {
cache *lru.LRU
w sync.RWMutex
w sync.Mutex
}

// newMessageCache creates a new messageCache with the specified cache size.
Expand All @@ -41,16 +41,9 @@ func newMessageCache[T any](cacheSize uint32) *messageCache[T] {
// it will generate the message and add it to the cache. The second return value
// indicates whether the message is newly generated.
func (mc *messageCache[T]) GetOrAdd(id thor.Bytes32, createMessage func() (T, error)) (T, bool, error) {
mc.w.RLock()
msg, ok := mc.cache.Peek(id)
mc.w.RUnlock()
if ok {
return msg.(T), false, nil
}

mc.w.Lock()
defer mc.w.Unlock()
msg, ok = mc.cache.Peek(id)
msg, ok := mc.cache.Get(id)
if ok {
return msg.(T), false, nil
}
Expand Down

0 comments on commit 4b6728e

Please sign in to comment.