package lazycache

import "golazy.dev/lazycache"

Package lazycache provides a small application cache contract.

lazycache owns key construction, the enabled/disabled switch, standardized statistics, and typed convenience helpers. It intentionally does not import a concrete backend. Conventional applications receive the default in-memory backend through lazyapp, while custom setups can pass any Backend to New.

Constants

const LazyDevCacheOffPath

const LazyDevCacheOffPath = "/cache/off"

const LazyDevCacheOnPath

const LazyDevCacheOnPath = "/cache/on"

const LazyDevCachePath

const LazyDevCachePath = "/cache"

Variables

ErrMiss reports that a key is not available from the cache.

var ErrMiss = errors.New("lazycache: miss")

Functions

func Get[T any]

Get returns a cached value with a concrete type.

func Get[T any](cache *Cache, parts ...any) (T, error)

func Key

Key builds the canonical cache key for a list of parts.

func Key(parts ...any) (string, error)

func PrometheusCollector

PrometheusCollector returns a collector for cache statistics.

func PrometheusCollector(cache *Cache) func(io.Writer) error

func Set[T any]

Set stores a typed value under the key built from parts.

func Set[T any](cache *Cache, value T, parts ...any) error

func WithCache

WithCache returns a context carrying cache.

func WithCache(ctx context.Context, cache *Cache) context.Context

func WritePrometheus

WritePrometheus writes cache statistics using the Prometheus text exposition format.

func WritePrometheus(w io.Writer, cache *Cache) error

Types

type Backend

Backend is the storage boundary used by Cache.

type Backend interface {
	Get(key string) (any, error)
	Set(key string, value any) error
	Stats() Stats
}

type Cache

Cache wraps a backend with GoLazy key building and on/off switching.

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

FromContext returns the cache carried by ctx.

func FromContext(ctx context.Context) (*Cache, bool)
func New

New creates a cache around a backend.

func New(options Options) (*Cache, error)
func (c *Cache) Enabled

Enabled reports whether reads and writes are active.

func (c *Cache) Enabled() bool
func (c *Cache) Get

Get returns a cached value for the key built from parts.

func (c *Cache) Get(parts ...any) (any, error)
func (c *Cache) Keys

Keys returns the backend keys when the backend exposes them.

func (c *Cache) Keys() []string
func (c *Cache) Off

Off disables reads and turns writes into no-ops.

func (c *Cache) Off()
func (c *Cache) On

On enables reads and writes.

func (c *Cache) On()
func (c *Cache) Set

Set stores value under the key built from parts.

func (c *Cache) Set(value any, parts ...any) error
func (c *Cache) Stats

Stats returns the backend statistics.

func (c *Cache) Stats() Stats

type KeyLister

KeyLister is an optional backend capability used by development tooling.

type KeyLister interface {
	Keys() []string
}

type Stats

Stats is the common cache statistics shape returned by every backend.

type Stats struct {
	Entries		int	`json:"entries"`
	MaxEntries	int	`json:"max_entries"`
	Hits		uint64	`json:"hits"`
	Misses		uint64	`json:"misses"`
	Sets		uint64	`json:"sets"`
	Evictions	uint64	`json:"evictions"`
}

Directories

Path Synopsis
lazycache/inmemorycache Package inmemorycache provides the default in-process lazycache backend.