valkey

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package valkey is the gas.CacheProvider backend for a Valkey or other Redis-compatible server, built on the valkey-go client. Its Service also implements gas.Service, gas.HealthReporter, and gas.ReadyReporter, and Client exposes the underlying valkey.Client for commands the gas.CacheProvider interface does not cover.

Wiring

New returns a DI constructor that takes a gas.ConfigProvider and a gas.Logger and returns *Service; register it under gas.CacheProvider. The constructor does not return an error, so it is not yet on the shape in CONVENTIONS.md (KI-004 in KNOWN_ISSUES.md). WithConfig supplies the configuration directly and skips binding it from the ConfigProvider.

Configuration

Without WithConfig, the service binds a Config from the ConfigProvider over DefaultConfig when it starts. The cache settings are Addr (default "localhost:6379"), Password (empty for no authentication), DB (default 0), DialTimeout (default five seconds, also the bound on the connection ping), WriteTimeout (default three seconds), ConnRetries (default 0), and ConnRetryInterval (default two seconds). Validate requires a non-empty Addr and a DB of 0 or more.

Lifecycle

Starting the service creates a client and pings the server. If that fails and ConnRetries is positive, it retries up to ConnRetries times, sleeping ConnRetryInterval before the first retry and doubling the wait each time (an interval of 0 or less uses two seconds); starting fails once the retries run out. The ping runs under the start context, and when that context ends during a wait the retries stop and starting fails with an error wrapping both the context's error and the last connection error. Stopping the service closes the client and makes every operation return cache.ErrClosed, as does every operation before the service has started. An operation that is running when the service stops, or when a restart replaces the client, also returns cache.ErrClosed. The client is held in an atomic pointer, so operations can run while the service restarts.

Behavior

Get returns cache.ErrKeyNotFound for a missing key. Set with a positive TTL sets a millisecond expiry on the server; a TTL of 0 or less stores the key without one. Errors from the server are wrapped with the service name and the key. CheckHealth fails only before the service has started or once it has stopped, since the client reconnects on its own; CheckReady pings the server with the caller's context. Client returns nil before the service first connects, and the closed client after it stops.

SPDX-License-Identifier: MIT

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) func(gas.ConfigProvider, gas.Logger) (*Service, error)

New captures options and returns a DI-injectable constructor.

Types

type Config

type Config struct {
	gas.WithGasEnv

	Cache Settings
}

Config holds Valkey cache settings.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

type Option

type Option func(*Service)

Option configures a Service.

func WithConfig

func WithConfig(cfg *Config) Option

WithConfig sets a custom configuration.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is a Valkey-backed cache implementing gas.Service and gas.CacheProvider.

func (*Service) CheckHealth

func (s *Service) CheckHealth(_ context.Context) error

CheckHealth reports liveness. The valkey-go client reconnects internally, so a transient network failure is not a liveness failure; only a service that was never started or has been closed is, and it reports cache.ErrClosed.

func (*Service) CheckReady

func (s *Service) CheckReady(ctx context.Context) error

CheckReady reports readiness by pinging the Valkey server with the caller's context. It returns cache.ErrClosed if the service was never started or is closed before the ping completes.

func (*Service) Client

func (s *Service) Client() valkey.Client

Client returns the underlying valkey.Client for advanced operations. It returns nil before Init first connects; after Close it returns the closed client.

func (*Service) Close

func (s *Service) Close(context.Context) error

Close shuts down the Valkey client.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, key string) error

Delete removes a key from the cache. It returns cache.ErrClosed if the service was never started or is closed before the command completes.

func (*Service) Exists

func (s *Service) Exists(ctx context.Context, key string) (bool, error)

Exists checks whether a key exists in the cache. It returns cache.ErrClosed if the service was never started or is closed before the command completes.

func (*Service) Get

func (s *Service) Get(ctx context.Context, key string) ([]byte, error)

Get returns the value for the given key, or cache.ErrKeyNotFound if the key does not exist. It returns cache.ErrClosed if the service was never started or is closed before the command completes.

func (*Service) Init

func (s *Service) Init(ctx context.Context) error

Init binds the configuration, creates the Valkey client, and verifies connectivity. A config bound from the ConfigProvider is validated by Bind against its validate tags; one passed with WithConfig is used as given. The ping and the retry backoff use ctx, so a canceled or expired ctx stops the retries.

func (*Service) Name

func (s *Service) Name() string

Name returns the service identifier.

func (*Service) Set

func (s *Service) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a value with the given TTL. If ttl is 0, the key is set without expiration. It returns cache.ErrClosed if the service was never started or is closed before the command completes.

type Settings

type Settings struct {
	// Addr is the Valkey server address in host:port format. Required.
	Addr string `validate:"required"`

	// Password is the authentication password. Empty means no auth.
	//nolint:gosec // intentional
	Password string

	// DB is the database number to use (0-15). Must not be negative.
	DB int `validate:"min=0"`

	// DialTimeout is the timeout for establishing new connections.
	DialTimeout time.Duration

	// WriteTimeout is applied to net.Conn.SetDeadline and controls
	// the maximum duration for write operations and periodic PINGs.
	WriteTimeout time.Duration

	// ConnRetries is the number of times to retry connecting on failure.
	// 0 means no retries (fail immediately).
	ConnRetries int

	// ConnRetryInterval is the base interval between connection retry
	// attempts. The interval doubles after each failed attempt.
	ConnRetryInterval time.Duration
}

Settings represents the configuration for the Valkey cache.

Jump to

Keyboard shortcuts

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