keymod

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package keymod provides functions for modifying keys.

This package is particularly useful when working with distributed caching systems like Redis Cluster, where keys containing "{hashTag}" are ensured to exist on the same node. This allows related keys to be stored together, enabling multi-key operations like transactions and Lua scripts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Modify added in v0.7.0

func Modify(key string, modifiers ...Mod) string

Modify applies the given Mod functions to a key.

This can be used to apply multiple modifications to a key, such as adding a prefix and a hash tag.

Example:

key := Modify("profile", PrefixModifier("user123", ":"), HashTagModifier("user123")) // "{user123}user123:profile"

Types

type Mod added in v0.7.0

type Mod func(string) string

Mod is a function that modifies a key.

This can be used to add prefixes, suffixes, or hash tags to keys.

func WithChain added in v0.7.0

func WithChain(modifiers ...Mod) Mod

WithChain chains multiple Mod functions together into a single Mod.

This can be used to create complex key modifications in a more readable way.

Example:

modifier := WithChain(PrefixModifier("user123", ":"), HashTagModifier("user123"))
key := modifier("profile") // "{user123}user123:profile"

func WithHashTag added in v0.7.0

func WithHashTag(text string) Mod

WithHashTag wraps the given text in curly braces and prepends it to the key.

This is useful when working with Redis Cluster, as keys with the same hash tag are guaranteed to be on the same node. This allows for multi-key operations to be performed atomically.

Example:

userHashTagModifier := WithHashTag("user123")
userKey := userHashTagModifier("profile") // "{user123}profile"

func WithPrefix added in v0.7.0

func WithPrefix(prefix string, separator string) Mod

WithPrefix prepends the given prefix to the key.

This is useful when working with namespaced keys, where the prefix represents a namespace. The separator is used to separate the prefix from the key.

Example:

userPrefixModifier := WithPrefix("user123", ":")
userKey := userPrefixModifier("profile") // "user123:profile"

func WithSuffix added in v0.7.0

func WithSuffix(suffix string, separator string) Mod

WithSuffix appends the given suffix to the key.

This is useful when working with namespaced keys, where the suffix represents a namespace. The separator is used to separate the key from the suffix.

Example:

userSuffixModifier := WithSuffix("profile", ":")
userKey := userSuffixModifier("user123") // "user123:profile"

Jump to

Keyboard shortcuts

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