cache

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache holds pushed object bytes between delivery and the moment the Teranode cluster pulls and validates them.

This is a cache, not a store of record: the store of record is the cluster's own, after validation. The working set is therefore delivery-rate × validation lag (seconds of traffic), so entries are held by hash with a TTL and a byte ceiling, and eviction is a normal event rather than data loss (a missed pull falls back to the cluster's ordinary peer-pull path).

The cache is sharded 64 ways by the first key byte. Keys are content hashes, so the spread is uniform and no shard's mutex sees more than ~1/64th of the put/get rate; a single global lock convulses long before the megaobject-per- second rates the tx lane is sized for.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is safe for concurrent use: ingest writes while retrieval reads.

Its Get/Put/TTL shape is the seam a sibling module substitutes (see retrieval.Store): anything honouring these semantics — including the copy-on-Put rule — can stand in for the in-process implementation.

func New

func New(o Options) *Cache

New returns a ready cache.

func (*Cache) Get

func (c *Cache) Get(key Key) (body []byte, class string, ok bool)

Get returns the stored body, or ok=false if absent or expired.

func (*Cache) Has

func (c *Cache) Has(key Key) bool

Has reports presence without counting a hit or miss.

func (*Cache) Put

func (c *Cache) Put(key Key, class string, body []byte)

Put stores body under key. Storing an existing key refreshes it and keeps the original bytes — re-delivery of an object we already hold is a no-op, which is what makes A/B failover and reconnects harmless.

The bytes are COPIED. Callers hand in the slice objfmt.Reader.Next returned, which aliases the reader's buffer and is overwritten by later objects on the same connection — retaining it uncopied corrupts cached entries in place, and the retrieval plane would serve those bytes to the cluster.

func (*Cache) PutOwned

func (c *Cache) PutOwned(key Key, class string, body []byte)

PutOwned stores body WITHOUT copying: ownership transfers to the cache and the caller must never mutate the slice again. It exists for the tx hot path, where the lane already makes one immutable copy per object and a second copy per million transactions a second is pure GC pressure. Every other caller wants Put.

func (*Cache) Stats

func (c *Cache) Stats() Stats

Stats returns a snapshot of the cache's size and counters, aggregated over all shards.

type Generational

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

Generational is the tx-lane variant of the cache: a sharded two-generation map with wholesale rotation instead of an LRU.

The LRU cache costs ~340 bytes of heap per 70-byte transaction (entry struct, list element, map bucket) and its pointer graph is what the garbage collector scans; at a million puts per second the process pins its memory limit and collapses into continuous GC. Here an entry is map[Key][]byte and nothing else: no per-entry timestamps, no list, no struct. Aging is generational — every ttl/2, or when the current generation reaches half the byte budget, the previous generation is dropped wholesale and the current one demoted — so eviction is O(1) amortized and precision degrades only in the direction that does not matter: an entry lives between ttl/2 and ttl.

The semantics fit exactly what the tx cache is for: serving subtree_data member lookups for subtrees completed in the last few seconds. It is a recency window, not a store.

A nil *Generational is a disabled cache: it holds nothing, admits everything (so it de-duplicates nothing) and misses every lookup. A bridge that carries the transaction lane only runs that way (-tx-cache-bytes 0).

func NewGenerational

func NewGenerational(o Options) *Generational

NewGenerational returns a generational cache. MaxBytes bounds the LIVE total (both generations); TTL bounds entry age at rotation cadence ttl/2.

func (*Generational) Admit added in v0.12.0

func (g *Generational) Admit(key Key, body []byte) bool

Admit stores body without copying unless key is already held in either generation, and reports whether it was new. A false return is a duplicate: the tx lane's re-delivery check (A/B failover, reconnect resend) rides on the lookup the insert needs anyway, so transactions never enter the bridge's seen-registry, whose subtree and block entries are the reverse path's origin filter and must not be flushed by transaction churn. The caller must not mutate body afterwards either way: a duplicate held in the current generation is replaced by it (the bytes are identical).

func (*Generational) Get

func (g *Generational) Get(key Key) (body []byte, class string, ok bool)

Get returns the stored body. The class of every entry is "tx".

func (*Generational) Has

func (g *Generational) Has(key Key) bool

Has reports presence without counting a hit or miss.

func (*Generational) Put

func (g *Generational) Put(key Key, class string, body []byte)

Put stores a copy of body (for callers whose slice aliases a reader buffer).

func (*Generational) PutOwned

func (g *Generational) PutOwned(key Key, _ string, body []byte)

PutOwned stores body without copying; ownership transfers to the cache.

func (*Generational) Stats

func (g *Generational) Stats() Stats

Stats returns a snapshot aggregated over all shards. Evicted counts entries dropped by generation rotation.

type Key

type Key [32]byte

Key identifies an object by its hash.

type Options

type Options struct {
	MaxBytes int64
	TTL      time.Duration
}

Options configure a Cache. Zero values pick sane defaults.

type Stats

type Stats struct {
	Entries                                int
	Bytes, MaxBytes                        int64
	Stored, Hits, Misses, Evicted, Expired uint64
	// Duplicates counts admissions refused because the key was already held
	// (Generational.Admit). Always zero for Cache.
	Duplicates uint64
}

Stats is a snapshot for logging and metrics.

Jump to

Keyboard shortcuts

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