cache

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cache provides reusable in-memory caching primitives.

Cache is a capacity-bounded TTL cache with Prometheus metrics and singleflight de-duplication; consumers compose their own value semantics (negative caching, optional wrapping, etc.) on top of it.

VersionedCache lazily fetches and caches lists of values keyed by a version tag, de-duplicating concurrent loads and respecting context cancellation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	TTL *ttlcache.Cache[K, V]
	SF  singleflight.Group
	// contains filtered or unexported fields
}

Cache is a capacity-bounded TTL cache with a Prometheus size gauge and a singleflight group for de-duplicating concurrent loads.

TTL and SF are exposed so consumers can use the underlying ttlcache and singleflight APIs directly without thin re-wrapping.

func New

func New[K comparable, V any](options Options) *Cache[K, V]

New constructs a Cache with WithDisableTouchOnHit (pure TTL, no LRU touch).

func (*Cache[K, V]) Collect

func (c *Cache[K, V]) Collect(ch chan<- prometheus.Metric)

Collect implements prom.Collector.

func (*Cache[K, V]) Describe

func (c *Cache[K, V]) Describe(ch chan<- *prometheus.Desc)

Describe implements prom.Collector.

func (*Cache[K, V]) Start

func (c *Cache[K, V]) Start() error

Start runs the cache eviction goroutine; should be invoked in a goroutine.

func (*Cache[K, V]) Stop

func (c *Cache[K, V]) Stop()

Stop halts the eviction goroutine.

type Options

type Options struct {
	// MetricsNamespace is prepended to MetricsName in the exported gauge.
	MetricsNamespace string

	// MetricsName is the Prometheus gauge name for the cache size.
	MetricsName string

	// MetricsHelp is the Prometheus gauge help string.
	MetricsHelp string

	// Capacity caps the number of entries; LRU eviction kicks in at the limit.
	Capacity uint64
}

Options configures a Cache.

type SingleFlightCache added in v1.4.0

type SingleFlightCache[T any] struct {
	// contains filtered or unexported fields
}

SingleFlightCache lazily fetches and caches a list of values keyed by a version tag.

Fetches are de-duplicated across concurrent callers via an internal singleflight group, and respect the caller's context for cancellation. Failed fetches are not cached.

func NewSingleFlightCache added in v1.4.0

func NewSingleFlightCache[T any](fetch func(tag string) (T, error)) *SingleFlightCache[T]

NewSingleFlightCache creates a cache that uses fetch to populate missing entries.

func (*SingleFlightCache[T]) Get added in v1.4.0

func (c *SingleFlightCache[T]) Get(ctx context.Context, tag string) (T, error)

Get returns the cached entry for a tag, fetching it if absent.

Jump to

Keyboard shortcuts

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