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

Variables

var ErrMiss

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

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

Functions

func BuildVersionFromContext

BuildVersionFromContext returns the cache-key build version for ctx.

func BuildVersionFromContext(ctx context.Context) string

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 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 WithBuildVersion

WithBuildVersion returns a context carrying the application build version used by cache keys.

func WithBuildVersion(ctx context.Context, version string) context.Context

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) Entries

Entries returns inspectable backend entries when the backend exposes them.

func (c *Cache) Entries() []EntryInfo
func (c *Cache) Entry

Entry returns an inspectable backend entry when the backend exposes it.

func (c *Cache) Entry(key string) (EntryDetail, error)
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
func (c *Cache) Subscribe

Subscribe returns a channel of cache events and an unsubscribe function. Slow subscribers may miss events; the cache never blocks request handling for development observers.

func (c *Cache) Subscribe() (<-chan Event, func())

type EntryDetail

EntryDetail describes one cached value with a development-friendly body.

type EntryDetail struct {
	EntryInfo
	Content		string	`json:"content"`
	ContentType	string	`json:"content_type"`
}

type EntryInfo

EntryInfo describes one cached value without exposing its body.

type EntryInfo struct {
	Key		string		`json:"key"`
	SizeBytes	int64		`json:"size_bytes"`
	CreatedAt	time.Time	`json:"created_at"`
	UpdatedAt	time.Time	`json:"updated_at"`
	LastAccessedAt	time.Time	`json:"last_accessed_at"`
	Hits		uint64		`json:"hits"`
	Sets		uint64		`json:"sets"`
}

type Event

Event describes a cache operation observed by development tooling.

type Event struct {
	Kind	EventKind	`json:"kind"`
	Key	string		`json:"key,omitempty"`
	Enabled	bool		`json:"enabled"`
	Stats	Stats		`json:"stats"`
	Entry	*EntryInfo	`json:"entry,omitempty"`
	At	time.Time	`json:"at"`
}

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"`
	SizeBytes	int64	`json:"size_bytes"`
	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.