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
- func Del(ctx context.Context, client redis.UniversalClient, key string) error
- func GetBytes(ctx context.Context, client redis.UniversalClient, key string, ...) ([]byte, error)
- func SetBytes(ctx context.Context, client redis.UniversalClient, key string, value []byte, ...) error
- type KeyBuilder
Constants ¶
const (
// DefaultSeparator is the standard Redis key separator.
DefaultSeparator = ":"
)
Variables ¶
This section is empty.
Functions ¶
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).