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 ¶
- func New(kind Kind, opts ...options.Option[Config]) (interfaces.ByteCache, error)
- func NewFromConfig(cfg *Config) (interfaces.ByteCache, error)
- func WithDefaultTTL(ttl time.Duration) options.Option[Config]
- func WithOpTimeout(d time.Duration) options.Option[Config]
- func WithRedisAddr(addr string) options.Option[Config]
- func WithRedisDB(db int) options.Option[Config]
- func WithRedisFailureMode(mode int) options.Option[Config]
- func WithRedisPassword(password string) options.Option[Config]
- func WithRedisPoolSize(size int) options.Option[Config]
- func WithRedisTLS(enabled bool) options.Option[Config]
- type Config
- type Kind
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
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 ¶
WithDefaultTTL sets the default TTL for the cache.
func WithOpTimeout ¶
WithOpTimeout sets the per-operation context deadline for cache commands. Zero disables per-operation timeouts (inherits caller's context).
func WithRedisAddr ¶
WithRedisAddr sets the Redis server address.
func WithRedisDB ¶
WithRedisDB sets the Redis database number.
func WithRedisFailureMode ¶
WithRedisFailureMode sets the Redis failure mode. 0 = FailureModeBypass (silent), 1 = FailureModeError (logged).
func WithRedisPassword ¶
WithRedisPassword sets the Redis authentication password.
func WithRedisPoolSize ¶
WithRedisPoolSize sets the Redis connection pool size.
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.
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. |