lotus/chain/events/message_cache.go

44 lines
864 B
Go
Raw Normal View History

2021-08-04 00:10:30 +00:00
package events
import (
"context"
"sync"
2023-09-21 15:37:02 +00:00
"github.com/hashicorp/golang-lru/arc/v2"
2021-08-04 00:10:30 +00:00
"github.com/ipfs/go-cid"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/api"
2021-08-04 00:10:30 +00:00
)
type messageCache struct {
api EventAPI
blockMsgLk sync.Mutex
2023-09-21 15:37:02 +00:00
blockMsgCache *arc.ARCCache[cid.Cid, *api.BlockMessages]
2021-08-04 00:10:30 +00:00
}
func newMessageCache(a EventAPI) *messageCache {
2023-09-21 15:37:02 +00:00
blsMsgCache, _ := arc.NewARC[cid.Cid, *api.BlockMessages](500)
2021-08-04 00:10:30 +00:00
return &messageCache{
api: a,
2021-08-04 00:10:30 +00:00
blockMsgCache: blsMsgCache,
}
}
func (c *messageCache) ChainGetBlockMessages(ctx context.Context, blkCid cid.Cid) (*api.BlockMessages, error) {
c.blockMsgLk.Lock()
defer c.blockMsgLk.Unlock()
msgs, ok := c.blockMsgCache.Get(blkCid)
2021-08-04 00:10:30 +00:00
var err error
if !ok {
msgs, err = c.api.ChainGetBlockMessages(ctx, blkCid)
2021-08-04 00:10:30 +00:00
if err != nil {
return nil, err
}
c.blockMsgCache.Add(blkCid, msgs)
2021-08-04 00:10:30 +00:00
}
return msgs, nil
2021-08-04 00:10:30 +00:00
}