Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cache provides a builder for ByteCache implementations with multiple backends.

Use New() or NewFromConfig() to create a cache instance. The builder pattern allows selecting between Redis-backed (or future) implementations at runtime based on configuration.

Example:

// Create a Redis cache with custom TTL
c, err := cache.New(cache.KindRedis, cache.WithDefaultTTL(10*time.Minute))
if err != nil {
    log.Fatal(err)
}
defer c.(io.Closer).Close()

// Create from config
cfg := cache.DefaultConfig()
cfg.Kind = cache.KindRedis
c, err := cache.NewFromConfig(cfg)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(kind Kind, opts ...options.Option[Config]) (interfaces.ByteCache, error)

New creates a ByteCache of the specified kind with optional functional options. Returns an error if the kind is unknown.

The returned ByteCache may implement io.Closer. Call type assertion and Close() to clean up resources.

Example:

c, err := cache.New(cache.KindRedis, cache.WithDefaultTTL(10*time.Minute))
if err != nil {
    return err
}
defer c.(io.Closer).Close()

func NewFromConfig

func NewFromConfig(cfg *Config) (interfaces.ByteCache, error)

NewFromConfig creates a ByteCache from a Config struct. Returns an error if the kind is unknown.

func WithDefaultTTL

func WithDefaultTTL(ttl time.Duration) options.Option[Config]

WithDefaultTTL sets the default TTL for the cache.

func WithOpTimeout

func WithOpTimeout(d time.Duration) options.Option[Config]

WithOpTimeout sets the per-operation context deadline for cache commands. Zero disables per-operation timeouts (inherits caller's context).

func WithRedisAddr

func WithRedisAddr(addr string) options.Option[Config]

WithRedisAddr sets the Redis server address.

func WithRedisDB

func WithRedisDB(db int) options.Option[Config]

WithRedisDB sets the Redis database number.

func WithRedisFailureMode

func WithRedisFailureMode(mode int) options.Option[Config]

WithRedisFailureMode sets the Redis failure mode. 0 = FailureModeBypass (silent), 1 = FailureModeError (logged).

func WithRedisPassword

func WithRedisPassword(password string) options.Option[Config]

WithRedisPassword sets the Redis authentication password.

func WithRedisPoolSize

func WithRedisPoolSize(size int) options.Option[Config]

WithRedisPoolSize sets the Redis connection pool size.

func WithRedisTLS

func WithRedisTLS(enabled bool) options.Option[Config]

WithRedisTLS enables (or disables) an encrypted connection to Redis. Required by managed Redis such as ElastiCache with an auth token.

Types

type Config

type Config struct {
	// Logger is the structured logger passed to the underlying cache backend.
	// If nil, error logging is silently skipped.
	Logger interfaces.Logger

	// Redis holds Redis-specific configuration.
	Redis RedisConfig

	// Kind specifies which cache implementation to use.
	Kind Kind

	// DefaultTTL is the expiration applied when callers pass zero to Set.
	DefaultTTL time.Duration

	// OpTimeout is the per-operation context deadline for cache commands.
	// Zero means no per-operation timeout (inherit caller's context).
	OpTimeout time.Duration
}

Config is the configuration for the Redis cache backend.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default cache configuration with KindRedis and 5-minute TTL.

func (*Config) ToOptions

func (c *Config) ToOptions() []options.Option[Config]

ToOptions converts this Config to a slice of Option functions. Useful for converting configs to builder options.

type Kind

type Kind int

Kind specifies which cache implementation to use.

const (
	// KindRedis uses a Redis-backed cache with singleflight support. Suitable
	// for distributed deployments with shared cache.
	KindRedis Kind = iota
)

func (Kind) String

func (k Kind) String() string

String returns the string representation of Kind.

type RedisConfig

type RedisConfig struct {
	// Addr is the Redis server address in host:port format.
	Addr string

	// Password is the Redis authentication password. Empty means no auth.
	Password string

	// DB is the Redis database number to select after connecting.
	DB int

	// PoolSize is the maximum number of connections in the pool.
	PoolSize int

	// MinIdleConns is the minimum number of idle connections kept open.
	MinIdleConns int

	// FailureMode controls whether cache errors are logged (1=Error) or
	// silently absorbed (0=Bypass). Maps to redis.FailureMode enum:
	// 0=FailureModeBypass, 1=FailureModeError.
	FailureMode int

	// TLS enables an encrypted connection to Redis (required by managed Redis
	// such as ElastiCache with an auth token). Off for local dev.
	TLS bool
}

RedisConfig holds Redis-specific configuration. This is a subset of redis.Config, adapted for the parent cache package.

Directories

Path Synopsis
Package decorators provides cache decorators for cross-cutting concerns.
Package decorators provides cache decorators for cross-cutting concerns.
Package redis provides a Redis-backed cache implementation with singleflight support.
Package redis provides a Redis-backed cache implementation with singleflight support.

Jump to

Keyboard shortcuts

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