caching

package
v2.25.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const MaxVaryHeaders = 100

MaxVaryHeaders bounds a vary record. A response naming more is not stored.

View Source
const MaxVarySets = 8

MaxVarySets bounds the name sets a record keeps. Past it the oldest go.

Variables

View Source
var (
	ErrMissingTTL = errors.New("cache item requires a positive TTL")
	ErrNoKeys     = errors.New("response cache lookup requires at least one key")
	ErrNoItems    = errors.New("response cache write requires at least one item")
)
View Source
var ErrEntryFormat = errors.New("cache entry is not in a known format")

Functions

func DeclaredTag added in v2.20.0

func DeclaredTag(subgraph, tag string) string

A subgraph name carrying the separator would otherwise spell another subgraph's tag, so separators within the scope are doubled and the single one after it is what ends the scope.

func DecodeEntry added in v2.22.0

func DecodeEntry(b []byte) (value []byte, surrogateKeys []string, vary [][]string, err error)

DecodeEntry returns the value as a subslice of b. Exactly one of value and vary is set, by the kind byte: a body decodes to value and surrogateKeys, a record to vary.

func EncodeItem added in v2.24.0

func EncodeItem(item Item) []byte

EncodeItem picks the envelope the item calls for. Empty vary sets are dropped, since a set names at least one header and DecodeEntry refuses a record that says otherwise; with no set left the item is a body.

func Key

func Key(entity, selection Digest) string

Key builds the cache key for one entity within one fetch.

func MergeSurrogateKeys added in v2.22.0

func MergeSurrogateKeys(dst []string, src ...[]string) []string

MergeSurrogateKeys appends every tag of src not already in dst, first seen order.

func MergeVarySets added in v2.24.0

func MergeVarySets(own []string, seen [][]string) [][]string

MergeVarySets is the record to write after a response varying on own: own first, then every set the record held before, so the variants stored under them stay reachable. Names come sorted and deduplicated from Vary, so equal sets are equal slices.

func PrivateKey added in v2.22.0

func PrivateKey(entity, selection, privateID Digest) string

PrivateKey builds the key for one entity within one fetch as seen by one user.

func SortSurrogateKeys added in v2.22.0

func SortSurrogateKeys(surrogateKeys []string)

SortSurrogateKeys groups by tier, coarsest first, keeping the order within a tier.

func SubgraphSurrogateKey added in v2.22.0

func SubgraphSurrogateKey(subgraph string) string

func SubgraphTag added in v2.20.0

func SubgraphTag(subgraph string) string

func TTL

func TTL(headers http.Header, defaultTTL time.Duration) (ttl time.Duration, private bool, ok bool)

func TypeSurrogateKey added in v2.22.0

func TypeSurrogateKey(subgraph, typeName string) string

func TypeTag added in v2.20.0

func TypeTag(subgraph, typeName string) string

func ValidDeclaredSurrogateKey added in v2.22.0

func ValidDeclaredSurrogateKey(tag string) bool

ValidDeclaredSurrogateKey reports whether a declared tag can go in a header. One spelled like a derived key passes: the header is the CDN's contract and carries what the subgraph declared, the index files it as declared.

func VariantKey added in v2.24.0

func VariantKey(base string, vary Digest) string

VariantKey builds the key of one variant of the entry at base, a public or private key, for the request header values vary digests.

func Vary added in v2.24.0

func Vary(headers http.Header) (names []string, star bool)

Types

type Cache

type Cache interface {
	// GetMany looks up every key and returns the ones it found, keyed by the
	// key they were asked for, with the SurrogateKeys they were stored with. A
	// miss is simply absent, never an error and never a zero Item.
	GetMany(ctx context.Context, keys []string) (map[string]Item, error)

	// SetMany stores every item, all of which must carry a positive TTL. When
	// items contains the same key twice, the last one wins. An error means an
	// unspecified subset of the items may already have been stored.
	// In case any of the passed ttls are invalid, SetMany should return
	// without saving any items that may have valid ttl values.
	SetMany(ctx context.Context, items []Item) error
}

Cache is a batch oriented key/value cache.

type Digest added in v2.22.0

type Digest [sha256.Size]byte

Digest is the SHA-256 of one input to a key. Collision resistant, so two inputs cannot end up on one key, and fixed size, so raw inputs never reach the store.

func DigestBytes added in v2.22.0

func DigestBytes(b []byte) Digest

func DigestParts added in v2.22.0

func DigestParts(parts ...[]byte) Digest

DigestParts digests the parts in order with a zero byte between them, so a byte moving from the end of one part to the start of the next cannot go unnoticed.

func DigestString added in v2.22.0

func DigestString(s string) Digest

func VaryDigest added in v2.24.0

func VaryDigest(names []string, sent http.Header) Digest

VaryDigest digests the values sent carries for names, in order, an absent header counting as empty. sent is the request as the subgraph receives it, since that is what its answer could have varied on.

type Item

type Item struct {
	// Key is the key the entry is stored under, exactly as GetMany was asked
	// for it, so an Item carries enough to be acted on once it has been taken
	// out of the map it came in.
	Key string
	// Value is the cached bytes. An entry with an empty Value is still an
	// entry: GetMany omitting the key is the only thing that means a miss.
	Value []byte
	// TTL is how long the entry should live when passed to SetMany, and how
	// much of that life it has left when returned by GetMany, so the same key
	// comes back with less of it on every lookup. It is always positive in
	// both directions: SetMany refuses an item without a positive TTL, and
	// GetMany treats an entry with nothing left to live, or one it finds with
	// no expiry attached at all, as a miss rather than a hit.
	TTL time.Duration
	// Tags are the cache tags the entry was stored under, a secondary index
	// naming what the entry is about so it can later be found by something other
	// than its key for invalidation.
	Tags []string
	// Surrogate keys are what a client response carries so a CDN can purge by them.
	// Stored with the value and returned by GetMany.
	SurrogateKeys []string
	// Vary makes the entry a vary record rather than a body: the sets of
	// request headers responses at this key have varied on, newest first.
	// Each set's values select a variant stored under VariantKey. A record has
	// no Value and no SurrogateKeys.
	Vary [][]string
}

Item is a single cache entry, both on the way in through SetMany and on the way back out of GetMany.

type SetManyError

type SetManyError struct {
	// This depicts the known stored keys in a case of a partial write
	// this means that some more keys could have been written but there is no way to know
	// because the client did not receive a response
	KnownStoredKeys []string
	Err             error
}

func (*SetManyError) Error

func (e *SetManyError) Error() string

func (*SetManyError) Unwrap

func (e *SetManyError) Unwrap() error

Jump to

Keyboard shortcuts

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