Skip to content

Commit

Permalink
use thread safe LRu implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed Dec 19, 2024
1 parent 5867f22 commit 27e8e20
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
20 changes: 5 additions & 15 deletions api/subscriptions/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ package subscriptions

import (
"fmt"
"sync"

"github.com/hashicorp/golang-lru/simplelru"
"github.com/hashicorp/golang-lru/v2"
"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.Cache[thor.Bytes32, interface{}]
}

// newMessageCache creates a new messageCache with the specified cache size.
Expand All @@ -27,7 +25,7 @@ func newMessageCache[T any](cacheSize uint32) *messageCache[T] {
if cacheSize == 0 {
cacheSize = 1
}
cache, err := simplelru.NewLRU(int(cacheSize), nil)
cache, err := lru.New[thor.Bytes32, interface{}](int(cacheSize))
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 +39,8 @@ 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) {

Check failure on line 41 in api/subscriptions/message_cache.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

unnecessary leading newline (whitespace)
mc.mu.RLock()
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)
msg, ok := mc.cache.Peek(id)
if ok {
return msg.(T), false, nil
}
Expand All @@ -60,6 +50,6 @@ func (mc *messageCache[T]) GetOrAdd(id thor.Bytes32, createMessage func() (T, er
var zero T
return zero, false, err
}
mc.cache.Add(id, newMsg)
mc.cache.ContainsOrAdd(id, newMsg)
return newMsg, true, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/go-stack/stack v1.7.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huin/goupnp v0.0.0-20171109214107-dceda08e705b // indirect
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvK
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down

0 comments on commit 27e8e20

Please sign in to comment.