Documentation
¶
Overview ¶
Package cache builds a ByteCache from config: the always-miss stub (the default), Redis, or a bounded in-process memory cache. The concrete backends live in the stub/, redis/ and memory/ subpackages.
Index ¶
Constants ¶
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 ¶
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"`
// Memory configures KindMemory.
Memory MemoryConfig `yaml:"memory" mapstructure:"memory"`
// 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 MemoryConfig ¶ added in v0.0.42
type MemoryConfig struct {
// MaxEntries bounds how many values it holds; zero means memory.DefaultMaxEntries.
MaxEntries int `yaml:"max_entries" mapstructure:"max_entries"`
}
MemoryConfig configures the in-process memory backend.
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 memory is the in-process ByteCache: a bounded, least-recently-used map whose entries expire at their TTL.
|
Package memory is the in-process ByteCache: a bounded, least-recently-used map whose entries expire at their TTL. |
|
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. |