redisutils

package
v0.0.0-...-c7901e9 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package redisutils provides common utilities for Redis-based data providers. It standardizes key building with consistent separator handling.

Example:

kb := redisutils.NewKeyBuilder("myapp:cache")
key := kb.Build("user:123") // returns "myapp:cache:user:123"

Index

Constants

View Source
const (
	// DefaultSeparator is the standard Redis key separator.
	DefaultSeparator = ":"
)

Variables

This section is empty.

Functions

func Del

func Del(ctx context.Context, client redis.UniversalClient, key string) error

Del deletes key (idempotent).

func GetBytes

func GetBytes(ctx context.Context, client redis.UniversalClient, key string, notFoundErr error) ([]byte, error)

GetBytes gets key from Redis and returns notFoundErr when the key doesn't exist.

func SetBytes

func SetBytes(ctx context.Context, client redis.UniversalClient, key string, value []byte, ttl time.Duration) error

SetBytes stores value under key with ttl.

Types

type KeyBuilder

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

KeyBuilder provides thread-safe, efficient Redis key construction with prefix support. The prefix is normalized once during creation to ensure consistent key format.

func NewKeyBuilder

func NewKeyBuilder(prefix string) *KeyBuilder

NewKeyBuilder creates a KeyBuilder with the given prefix. The prefix is normalized: trailing separators are removed and will be added automatically when building keys.

Example:

kb := NewKeyBuilder("myapp:cache")  // or "myapp:cache:"
kb.Build("user:123") // returns "myapp:cache:user:123"

func NewKeyBuilderWithSeparator

func NewKeyBuilderWithSeparator(prefix, separator string) *KeyBuilder

NewKeyBuilderWithSeparator creates a KeyBuilder with custom separator. Use this when you need a different separator (rare case).

func (*KeyBuilder) Build

func (kb *KeyBuilder) Build(key string) string

Build constructs a full Redis key by combining prefix and key with separator. Returns the key unchanged if no prefix is configured. This method is thread-safe and allocation-optimized for common cases.

Example:

kb.Build("user:123")    // "prefix:user:123"
kb.Build("")            // "prefix:"

func (*KeyBuilder) BuildMany

func (kb *KeyBuilder) BuildMany(keys []string) []string

BuildMany constructs multiple Redis keys efficiently. Returns a new slice with prefixed keys; original slice is not modified.

func (*KeyBuilder) HasPrefix

func (kb *KeyBuilder) HasPrefix() bool

HasPrefix returns true if a prefix is configured.

func (*KeyBuilder) Pattern

func (kb *KeyBuilder) Pattern() string

Pattern returns a glob pattern for matching all keys with this prefix. Useful for SCAN or KEYS commands.

Example:

kb.Pattern() // "prefix:*"

func (*KeyBuilder) Prefix

func (kb *KeyBuilder) Prefix() string

Prefix returns the normalized prefix (without trailing separator).

Jump to

Keyboard shortcuts

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