cache

package
v1.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cache is Lantern's persistent, bounded, CID-keyed block cache.

The light node runs cache-first with an in-memory hamt.MemBlockStore: fast, but every process restart is cold, so a restarted node re-fetches its whole warm set (the PDP/payments/registry/USDFC contract subtrees) from the gateway/swarm on the first head advance. For a wallet that's fine. For a PDP node - which must prove/settle against warm contract state on a schedule and cannot afford a cold-start stall inside a proving window - it is not.

This package backs the block cache with Badger (pure-Go, already a Lantern dependency via the header store), so the warm set SURVIVES restart. It is CID-keyed exactly like MemBlockStore, so the content-addressed trust property is unchanged: bytes are stored under their CID and verified against it. A peer can never poison this cache because insertion goes through PutVerify on the network path.

Boundedness: a soft byte budget with sampled-LRU eviction plus a pin set (the warm contract subtrees a PDP node must never lose). This is the mid/PDP tier's 2-5 GB persistent footprint - deliberately bigger than the light node's memory cache, to cut the cold-fetch tail to zero in steady state.

It satisfies combined.Cache (hamt.BlockGetter + Put) so it is a drop-in replacement for MemBlockStore in the daemon fetcher.

Index

Constants

View Source
const DefaultSoftCapBytes int64 = 3 << 30

DefaultSoftCapBytes is the default persistent-cache budget for the PDP tier: 3 GiB, the middle of the 2-5 GB target. Eviction keeps usage near this; pinned blocks are exempt.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// SoftCapBytes is the target on-disk block budget. 0 => default (3 GiB).
	// Eviction fires when live block bytes exceed this. Pinned blocks are
	// never evicted, so the effective floor is the pinned set size.
	SoftCapBytes int64
}

Options configures the persistent cache.

type Stats

type Stats struct {
	LiveBytes    int64
	SoftCapBytes int64
	Hits         uint64
	Misses       uint64
	Puts         uint64
	Evictions    uint64
}

Stats is an observability snapshot of the cache.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a persistent, bounded, CID-keyed block cache.

func Open

func Open(path string, opts Options) (*Store, error)

Open opens (or creates) a persistent block cache at path.

func (*Store) Close

func (s *Store) Close() error

Close flushes and closes the underlying Badger DB. Safe to call from any goroutine; concurrent Get/Put in flight when Close is entered are allowed to finish, and any background touch/eviction goroutines already spawned pre-Close are waited on before the DB is torn down.

func (*Store) Get

func (s *Store) Get(_ context.Context, c cid.Cid) ([]byte, error)

Get returns the raw bytes for c, or an error if absent. Bumps the block's lastAccess for LRU. Implements hamt.BlockGetter.

func (*Store) GetBlock

func (s *Store) GetBlock(ctx context.Context, c cid.Cid) (block.Block, error)

GetBlock returns the block in go-block-format shape (for adapters).

func (*Store) Has

func (s *Store) Has(c cid.Cid) bool

Has reports whether a block is present.

func (*Store) Pin

func (s *Store) Pin(c cid.Cid) error

Pin marks c as un-evictable (the PDP warm set: contract subtrees a PDP node must never lose across restarts). Pinning a not-yet-present CID is a no-op that takes effect if/when the block is inserted.

func (*Store) Put

func (s *Store) Put(c cid.Cid, raw []byte) cid.Cid

Put stores raw bytes under c and returns c (for chaining). It does NOT re-hash; the combined fetcher only calls Put after a CID-verified fetch, matching MemBlockStore.Put semantics. Use PutVerify to insert untrusted bytes.

func (*Store) PutVerify

func (s *Store) PutVerify(c cid.Cid, raw []byte) error

PutVerify recomputes c's hash over raw and inserts only on match.

func (*Store) Stats

func (s *Store) Stats() Stats

Stats returns a snapshot of cache counters.

func (*Store) Unpin

func (s *Store) Unpin(c cid.Cid) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL