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:
Steven Allen
2023-03-13 15:29:09 -07:00
committed by GitHub
parent 97a9921cdd
commit dcb49dc8ee
17 changed files with 95 additions and 116 deletions
+4 -4
View File
@@ -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 {
+4 -4
View File
@@ -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())