cache

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides 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 the primitive.

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.

Jump to

Keyboard shortcuts

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