diff --git a/carstore/bs.go b/carstore/bs.go index 0095c9fd7..efddcc442 100644 --- a/carstore/bs.go +++ b/carstore/bs.go @@ -45,6 +45,8 @@ type CarStore struct { lscLk sync.Mutex lastShardCache map[models.Uid]*CarShard + + globalCache map[cid.Cid]blockformat.Block } func NewCarStore(meta *gorm.DB, root string) (*CarStore, error) { @@ -68,6 +70,7 @@ func NewCarStore(meta *gorm.DB, root string) (*CarStore, error) { meta: meta, rootDir: root, lastShardCache: make(map[models.Uid]*CarShard), + globalCache: make(map[cid.Cid]blockformat.Block), }, nil } @@ -138,6 +141,10 @@ func unpackCids(b []byte) ([]cid.Cid, error) { return out, nil } +func (cs *CarStore) SetGlobalCacheEntry(k cid.Cid, v blockformat.Block) { + cs.globalCache[k] = v +} + type userView struct { cs *CarStore user models.Uid @@ -594,6 +601,11 @@ func (ds *DeltaSession) Get(ctx context.Context, c cid.Cid) (blockformat.Block, return b, nil } + gcb, ok := ds.cs.globalCache[c] + if ok { + return gcb, nil + } + return ds.base.Get(ctx, c) } @@ -603,6 +615,11 @@ func (ds *DeltaSession) Has(ctx context.Context, c cid.Cid) (bool, error) { return true, nil } + _, ok = ds.cs.globalCache[c] + if ok { + return true, nil + } + return ds.base.Has(ctx, c) } @@ -616,6 +633,11 @@ func (ds *DeltaSession) GetSize(ctx context.Context, c cid.Cid) (int, error) { return len(b.RawData()), nil } + gcb, ok := ds.cs.globalCache[c] + if ok { + return len(gcb.RawData()), nil + } + return ds.base.GetSize(ctx, c) }