diff --git a/api/subscriptions/message_cache.go b/api/subscriptions/message_cache.go index 0ef85bf94..3fe0cb5a9 100644 --- a/api/subscriptions/message_cache.go +++ b/api/subscriptions/message_cache.go @@ -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. @@ -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)) @@ -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) { - 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 } @@ -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 } diff --git a/go.mod b/go.mod index f3032a927..a22df0ad6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 648890cc0..b8d225cd1 100644 --- a/go.sum +++ b/go.sum @@ -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=