cache

package
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache builds a ByteCache from config: the always-miss stub (the default) or Redis. The concrete backends live in the stub/ and redis/ subpackages.

Index

Constants

View Source
const (
	// KindStub misses every Get and drops every write (the default).
	KindStub = client.KindStub

	// KindRedis caches in Redis.
	KindRedis client.Kind = "redis"
)

Variables

This section is empty.

Functions

func Decorate added in v0.0.7

func Decorate(
	base interfaces.ByteCache,
	decs decorate.Decorations,
	name string,
	opts ...client.Option,
) (interfaces.ByteCache, error)

Decorate runs Get, Set and Delete of base through the client tier as the client named name, and counts each Get as a hit or a miss (cache_hits_total / cache_misses_total, labelled by cache). A cache is fail-open and never retried: an operation the tier refuses (an open breaker, a timeout) reads as a miss or a dropped write. When base can invalidate (interfaces.CacheInvalidator), so can the result, through the tier.

func NewFromConfig

func NewFromConfig(
	ctx context.Context,
	cfg Config,
	deps Deps,
) (interfaces.ByteCache, error)

NewFromConfig builds the cache cfg.Kind selects and runs it through the client tier, without I/O. An unknown kind is CodeInvalidInput.

Types

type Config

type Config struct {
	// Kind selects the backend; empty means KindStub.
	Kind client.Kind `yaml:"kind" mapstructure:"kind"`

	// Name labels the client in decoration overrides, metrics, spans and logs; empty
	// uses the family name ("cache").
	Name string `yaml:"name" mapstructure:"name"`

	// Redis configures KindRedis.
	Redis RedisConfig `yaml:"redis" mapstructure:"redis"`

	// DefaultTTL applies when a caller passes a zero TTL.
	DefaultTTL time.Duration `yaml:"default_ttl" mapstructure:"default_ttl"`

	// OpTimeout bounds each cache command; zero inherits the caller's deadline.
	OpTimeout time.Duration `yaml:"op_timeout" mapstructure:"op_timeout"`
}

Config selects and tunes the cache.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the stub kind, a 5-minute default TTL and a 500ms op timeout.

type Deps added in v0.0.7

type Deps struct {
	// Decorations decorates the client from the decoration config (decoration.clients)
	// and supplies its observability.
	decorate.Decorations
}

Deps carries the cache's runtime collaborators.

type RedisConfig

type RedisConfig struct {
	// Addr is the Redis host:port.
	Addr string `yaml:"addr" mapstructure:"addr"`

	// Password is the Redis auth token.
	Password string `yaml:"password" mapstructure:"password"`

	// DB is the Redis database number.
	DB int `yaml:"db" mapstructure:"db"`

	// PoolSize is the connection pool size; zero means 10 * GOMAXPROCS.
	PoolSize int `yaml:"pool_size" mapstructure:"pool_size"`

	// MinIdleConns keeps this many idle connections warm.
	MinIdleConns int `yaml:"min_idle_conns" mapstructure:"min_idle_conns"`

	// FailureMode picks how a Redis error is absorbed: 0 bypasses silently, 1 logs.
	FailureMode int `yaml:"failure_mode" mapstructure:"failure_mode"`

	// TLS encrypts the connection (required by managed Redis with an auth token).
	TLS bool `yaml:"tls" mapstructure:"tls"`
}

RedisConfig configures the Redis backend.

Directories

Path Synopsis
Package redis provides a Redis-backed cache implementation with singleflight support.
Package redis provides a Redis-backed cache implementation with singleflight support.
Package stub is the zero-infrastructure ByteCache: every Get misses and every write is dropped, so a graph boots and runs uncached without Redis.
Package stub is the zero-infrastructure ByteCache: every Get misses and every write is dropped, so a graph boots and runs uncached without Redis.

Jump to

Keyboard shortcuts

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