redis

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client implements the cache.Cache interface using Redis as the backend.

func NewClient

func NewClient(cfg *Config) (*Client, error)

NewClient creates a new Redis cache client. Validates configuration and establishes connection.

func (*Client) Close

func (c *Client) Close() error

Close closes the Redis client and releases resources. After calling Close, the client should not be used. Close is idempotent - calling it multiple times is safe.

func (*Client) CompareAndSet

func (c *Client) CompareAndSet(ctx context.Context, key string, expectedValue, newValue []byte, ttl time.Duration) (bool, error)

CompareAndSet atomically compares and swaps a value. Returns (success, error):

  • success=true: Value was updated (comparison matched)
  • success=false: Value was NOT updated (comparison failed)

Special case: expectedValue=nil means "set only if key doesn't exist" (acquire lock). Uses Lua script for atomicity.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, key string) error

Delete removes a key from the cache. Does not return error if key doesn't exist.

func (*Client) Get

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

Get retrieves a value from the cache. Returns cache.ErrNotFound if the key doesn't exist.

func (*Client) GetOrSet

func (c *Client) GetOrSet(ctx context.Context, key string, value []byte, ttl time.Duration) (storedValue []byte, wasSet bool, err error)

GetOrSet atomically gets an existing value or sets a new one. Returns (storedValue, wasSet, error):

  • wasSet=true: Value was newly set (first-time processing)
  • wasSet=false: Value already existed (duplicate detected)
  • storedValue: Always returns the value in cache (current or newly set)

Uses Redis SET NX GET for atomicity.

func (*Client) Health

func (c *Client) Health(ctx context.Context) error

Health checks if the Redis connection is healthy. Uses PING command to verify connectivity.

func (*Client) Set

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

Set stores a value in the cache with the specified TTL. TTL of 0 means no expiration (use with caution). Returns cache.ErrInvalidTTL if TTL is negative.

func (*Client) Stats

func (c *Client) Stats() (map[string]any, error)

Stats returns Redis server statistics. Includes metrics from Redis INFO command.

type Config

type Config struct {
	// Host is the Redis server hostname or IP address.
	Host string `config:"host" required:"true"`

	// Port is the Redis server port (default: 6379).
	Port int `config:"port" default:"6379"`

	// Password for Redis authentication (optional).
	// Should be provided via environment variable: CACHE_REDIS_PASSWORD
	Password string `config:"password"`

	// Database number to use (default: 0).
	// Redis supports databases 0-15 by default.
	Database int `config:"database" default:"0"`

	// PoolSize is the maximum number of socket connections (default: 10).
	// Higher values allow more concurrent operations but consume more resources.
	PoolSize int `config:"pool_size" default:"10"`

	// DialTimeout is the timeout for establishing new connections (default: 5s).
	DialTimeout time.Duration `config:"dial_timeout" default:"5s"`

	// ReadTimeout is the timeout for socket reads (default: 3s).
	// -1 disables timeout.
	ReadTimeout time.Duration `config:"read_timeout" default:"3s"`

	// WriteTimeout is the timeout for socket writes (default: 3s).
	// -1 disables timeout.
	WriteTimeout time.Duration `config:"write_timeout" default:"3s"`

	// MaxRetries is the maximum number of retries before giving up (default: 3).
	// -1 disables retries.
	MaxRetries int `config:"max_retries" default:"3"`

	// MinRetryBackoff is the minimum backoff between retries (default: 8ms).
	MinRetryBackoff time.Duration `config:"min_retry_backoff" default:"8ms"`

	// MaxRetryBackoff is the maximum backoff between retries (default: 512ms).
	MaxRetryBackoff time.Duration `config:"max_retry_backoff" default:"512ms"`
}

Config holds Redis-specific configuration options.

func (*Config) Address

func (c *Config) Address() string

Address returns the Redis server address in "host:port" format.

func (*Config) Validate

func (c *Config) Validate() error

Validate performs fail-fast validation of Redis configuration. Returns error if configuration is invalid.

Jump to

Keyboard shortcuts

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