redis

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package redis provides a Redis integration block with a typed caching API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

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

Block is a Redis integration block.

func New

func New(name string, opts ...Option) *Block

New creates a new Redis Block.

block := redis.New("cache",
    redis.WithAddr("localhost:6379"),
    redis.WithPassword("secret"),
    redis.WithDB(0),
    redis.WithKeyPrefix("myapp:"),
)

func (*Block) Delete

func (b *Block) Delete(ctx context.Context, keys ...string) error

Delete removes one or more keys. Missing keys are silently ignored.

func (*Block) Exists

func (b *Block) Exists(ctx context.Context, key string) (bool, error)

Exists reports whether key is present in Redis.

func (*Block) Expire

func (b *Block) Expire(ctx context.Context, key string, ttl time.Duration) error

Expire sets or refreshes the TTL on an existing key.

func (*Block) Get

func (b *Block) Get(ctx context.Context, key string) (string, error)

Get returns the string value associated with key. Returns core.ErrItemNotFound when the key does not exist.

func (*Block) GetJSON

func (b *Block) GetJSON(ctx context.Context, key string, v any) error

GetJSON retrieves the value at key and unmarshals it into v. Returns core.ErrItemNotFound when the key does not exist.

func (*Block) HGet

func (b *Block) HGet(ctx context.Context, key, field string) (string, error)

HGet retrieves a single field from the hash at key. Returns core.ErrItemNotFound when the key or field does not exist.

func (*Block) HGetAll

func (b *Block) HGetAll(ctx context.Context, key string) (map[string]string, error)

HGetAll retrieves all fields and values from the hash at key.

func (*Block) HSet

func (b *Block) HSet(ctx context.Context, key string, fields ...any) error

HSet sets one or more field-value pairs in the hash at key. fields should be passed as alternating field, value pairs:

block.HSet(ctx, "user:1", "name", "Alice", "email", "alice@example.com")

func (*Block) Init

func (b *Block) Init(ctx context.Context) error

Init implements core.Block. It creates the Redis client and verifies connectivity with a PING command.

func (*Block) Name

func (b *Block) Name() string

Name implements core.Block.

func (*Block) Set

func (b *Block) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores value under key with an optional TTL (0 = no expiry).

func (*Block) SetJSON

func (b *Block) SetJSON(ctx context.Context, key string, v any, ttl time.Duration) error

SetJSON marshals v to JSON and stores it under key.

func (*Block) Shutdown

func (b *Block) Shutdown(_ context.Context) error

Shutdown implements core.Block. Closes the connection pool gracefully.

type Option

type Option func(*blockConfig)

Option configures a Redis Block.

func WithAddr

func WithAddr(addr string) Option

WithAddr sets the Redis server address (default: "localhost:6379").

func WithDB

func WithDB(db int) Option

WithDB selects the Redis logical database index (default: 0).

func WithDialTimeout

func WithDialTimeout(d time.Duration) Option

WithDialTimeout sets the timeout for establishing new connections.

func WithKeyPrefix

func WithKeyPrefix(prefix string) Option

WithKeyPrefix prepends a namespace to every key used by this block. Useful for isolating environments (e.g. "prod:", "staging:").

func WithPassword

func WithPassword(password string) Option

WithPassword sets the Redis AUTH password.

func WithPoolSize

func WithPoolSize(n int) Option

WithPoolSize sets the maximum number of idle connections in the pool.

func WithReadTimeout

func WithReadTimeout(d time.Duration) Option

WithReadTimeout sets the per-command read deadline.

func WithTLS

func WithTLS(tlsCfg *tls.Config) Option

WithTLS configures TLS for the Redis connection.

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout sets the per-command write deadline.

Jump to

Keyboard shortcuts

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