refactor: update cache to the new generic version (#10463)
- Adds type safety. - Reduces allocations. - Fixes the drand cache (was storing by value, but retrieving by pointer)
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
"github.com/ipfs/go-cid"
|
||||
cbg "github.com/whyrusleeping/cbor-gen"
|
||||
"go.opencensus.io/stats"
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
type ApiIpldStore struct {
|
||||
ctx context.Context
|
||||
api apiIpldStoreApi
|
||||
cache *lru.TwoQueueCache
|
||||
cache *lru.TwoQueueCache[cid.Cid, []byte]
|
||||
cacheSize int
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func NewApiIpldStore(ctx context.Context, api apiIpldStoreApi, cacheSize int) (*
|
||||
cacheSize: cacheSize,
|
||||
}
|
||||
|
||||
cache, err := lru.New2Q(store.cacheSize)
|
||||
cache, err := lru.New2Q[cid.Cid, []byte](store.cacheSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func (ht *ApiIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) err
|
||||
|
||||
if a, ok := ht.cache.Get(c); ok {
|
||||
stats.Record(ctx, metrics.IpldStoreCacheHit.M(1))
|
||||
raw = a.([]byte)
|
||||
raw = a
|
||||
} else {
|
||||
bs, err := ht.read(ctx, c)
|
||||
if err != nil {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
client "github.com/influxdata/influxdb1-client/v2"
|
||||
"github.com/ipfs/go-cid"
|
||||
"go.opencensus.io/stats"
|
||||
@@ -41,11 +41,11 @@ type ChainPointCollector struct {
|
||||
ctx context.Context
|
||||
api LotusApi
|
||||
store adt.Store
|
||||
actorDigestCache *lru.TwoQueueCache
|
||||
actorDigestCache *lru.TwoQueueCache[address.Address, string]
|
||||
}
|
||||
|
||||
func NewChainPointCollector(ctx context.Context, store adt.Store, api LotusApi) (*ChainPointCollector, error) {
|
||||
actorDigestCache, err := lru.New2Q(2 << 15)
|
||||
actorDigestCache, err := lru.New2Q[address.Address, string](2 << 15)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func NewChainPointCollector(ctx context.Context, store adt.Store, api LotusApi)
|
||||
|
||||
func (c *ChainPointCollector) actorDigest(ctx context.Context, addr address.Address, tipset *types.TipSet) (string, error) {
|
||||
if code, ok := c.actorDigestCache.Get(addr); ok {
|
||||
return code.(string), nil
|
||||
return code, nil
|
||||
}
|
||||
|
||||
actor, err := c.api.StateGetActor(ctx, addr, tipset.Key())
|
||||
|
||||
Reference in New Issue
Block a user