Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use thread safe LRU implementation #928

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions api/subscriptions/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"fmt"
"sync"

"github.com/hashicorp/golang-lru/simplelru"
lru "github.com/hashicorp/golang-lru/simplelru"
"github.com/vechain/thor/v2/thor"
)

// messageCache is a generic cache that stores messages of any type.
type messageCache[T any] struct {
cache *simplelru.LRU
mu sync.RWMutex
cache *lru.LRU
w sync.Mutex
}

// newMessageCache creates a new messageCache with the specified cache size.
Expand All @@ -27,7 +27,7 @@ func newMessageCache[T any](cacheSize uint32) *messageCache[T] {
if cacheSize == 0 {
cacheSize = 1
}
cache, err := simplelru.NewLRU(int(cacheSize), nil)
cache, err := lru.NewLRU(int(cacheSize), nil)
if err != nil {
// lru.New only throws an error if the number is less than 1
panic(fmt.Errorf("failed to create message cache: %v", err))
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.mu.RLock()
mc.w.Lock()
defer mc.w.Unlock()
msg, ok := mc.cache.Get(id)
mc.mu.RUnlock()
if ok {
return msg.(T), false, nil
}

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