redisclient

package
v0.36.378 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound defines not found error

Functions

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true, if error is NotFound

func Marshal

func Marshal(v any) (any, error)

Marshal marshals the value into a format suitable for Redis storage. string and []byte are stored as-is, while other types are marshaled to JSON.

func NewRedisClient

func NewRedisClient(cfg *Config) (*redis.Client, error)

func UnmarshalStringCmd

func UnmarshalStringCmd(val *redis.StringCmd, v any) error

UnmarshalStringCmd unmarshals the value from Redis storage into the provided variable. string and []byte are converted as-is, while JSON is unmarshaled into the target type.

Types

type Config

type Config struct {
	Server string        `json:"server,omitempty" yaml:"server,omitempty"`
	TTL    time.Duration `json:"ttl,omitempty" yaml:"ttl,omitempty"`
	// ClientTLS describes the TLS certs used to connect to the cluster
	ClientTLS *gserver.TLSInfo `json:"client_tls,omitempty" yaml:"client_tls,omitempty"`
	User      string           `json:"user,omitempty" yaml:"user,omitempty"`
	Password  string           `json:"password,omitempty" yaml:"password,omitempty"`
}

Config specifies configuration of the redis.

type DistributedLock

type DistributedLock interface {
	// TryLock attempts to acquire a lock with the given key and timeout
	// Returns true if lock was acquired, false otherwise, and remaining time if lock exists
	TryLock(ctx context.Context, key string, timeout time.Duration) (bool, time.Duration, error)

	// ReleaseLock releases the lock for the given key
	// Returns true if lock was released, false if lock didn't exist or was already released
	ReleaseLock(ctx context.Context, key string) (bool, error)

	// IsLocked checks if a lock exists for the given key
	IsLocked(ctx context.Context, key string) (bool, error)
}

DistributedLock provides distributed locking functionality

type Provider

type Provider interface {
	io.Closer

	RateLimiter
	DistributedLock

	// Value operations
	Get(ctx context.Context, key string, value any) error
	Set(ctx context.Context, key string, value any, expiration time.Duration) error
	Del(ctx context.Context, key string) error

	// List operations
	LPush(ctx context.Context, key string, values ...any) error
	RPush(ctx context.Context, key string, values ...any) error
	LPop(ctx context.Context, key string) (string, error)
	RPop(ctx context.Context, key string) (string, error)
	LRange(ctx context.Context, key string, start, stop int64) ([]string, error)
	LTrim(ctx context.Context, key string, start, stop int64) error
	LLen(ctx context.Context, key string) (int64, error)
	LIndex(ctx context.Context, key string, index int64) (string, error)

	// Set operations
	SAdd(ctx context.Context, key string, members ...any) error
	SRem(ctx context.Context, key string, members ...any) error
	SIsMember(ctx context.Context, key string, member any) (bool, error)
	SMembers(ctx context.Context, key string) ([]string, error)
	SCard(ctx context.Context, key string) (int64, error)
	SAddWithEviction(ctx context.Context, key string, listKey string, limit int64, member string) error

	// Sorted Set (ZSet) operations
	ZAdd(ctx context.Context, key string, score float64, member string) error
	ZIncrBy(ctx context.Context, key string, increment float64, member string) error
	ZRem(ctx context.Context, key string, members ...any) error
	ZCard(ctx context.Context, key string) (int64, error)
	// ZRevRangeWithScores returns the specified range of elements in the sorted set stored at key,
	// by index, with scores ordered from high to low.
	// To get top N elements, use ZRevRangeWithScores(key, 0, N-1)
	// To get bottom N elements, use ZRevRangeWithScores(key, -N, -1)
	ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error)
	// ZRemRangeByRank removes all elements in the sorted set stored at key
	// within the given indexes.
	// Start and stop are 0-based indexes, with 0 being the first element.
	// To remove all ranks below the top N elements, use ZRemRangeByRank(key, 0, -maxN-1)
	ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error)

	// Hash operations
	HSetMany(ctx context.Context, key string, vals map[string]any) error
	HSet(ctx context.Context, key string, field string, value any) error
	HGet(ctx context.Context, key string, field string) (string, error)
	HGetAll(ctx context.Context, key string) (map[string]string, error)
	HDel(ctx context.Context, key string, fields ...string) error
	HExists(ctx context.Context, key string, field string) (bool, error)
	HKeys(ctx context.Context, key string) ([]string, error)
	HVals(ctx context.Context, key string) ([]string, error)
	HSetWithEviction(ctx context.Context, hashKey, orderListKey string, maxFields int64, field string, value any) error

	// Metadata
	ScanKeys(ctx context.Context, pattern string, limit int) ([]string, error)
	Exists(ctx context.Context, key string) (bool, error)
	Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
	TTL(ctx context.Context, key string) (time.Duration, error)

	Ping(ctx context.Context) error
}

type RateLimiter

type RateLimiter interface {
	// TryAcquireRateLimit attempts to acquire a rate limit slot for the given key
	// Returns true if rate limit allows the operation, false if rate limit exceeded, and remaining time if exceeded
	TryAcquireRateLimit(ctx context.Context, key string, window time.Duration) (bool, time.Duration, error)

	// GetRateLimitRemainingTime returns the remaining time until the rate limit window resets
	GetRateLimitRemainingTime(ctx context.Context, key string) (time.Duration, error)
}

RateLimiter provides rate limiting functionality

type RedisClient

type RedisClient struct {
	*redis.Client
	// contains filtered or unexported fields
}

func New

func New(cfg *Config) (*RedisClient, error)

New creates a new Redis client with the given options

func NewWithClient

func NewWithClient(client *redis.Client) (*RedisClient, error)

func (*RedisClient) Close

func (c *RedisClient) Close() error

func (*RedisClient) Del

func (c *RedisClient) Del(ctx context.Context, key string) error

func (*RedisClient) Exists

func (c *RedisClient) Exists(ctx context.Context, key string) (bool, error)

func (*RedisClient) Expire

func (c *RedisClient) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)

func (*RedisClient) Get

func (c *RedisClient) Get(ctx context.Context, key string, v any) error

func (*RedisClient) GetRateLimitRemainingTime

func (c *RedisClient) GetRateLimitRemainingTime(ctx context.Context, key string) (time.Duration, error)

GetRateLimitRemainingTime returns the remaining time until the rate limit window resets

func (*RedisClient) HDel

func (c *RedisClient) HDel(ctx context.Context, key string, fields ...string) error

func (*RedisClient) HExists

func (c *RedisClient) HExists(ctx context.Context, key string, field string) (bool, error)

func (*RedisClient) HGet

func (c *RedisClient) HGet(ctx context.Context, key string, field string) (string, error)

func (*RedisClient) HGetAll

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

func (*RedisClient) HKeys

func (c *RedisClient) HKeys(ctx context.Context, key string) ([]string, error)

func (*RedisClient) HSet

func (c *RedisClient) HSet(ctx context.Context, key string, field string, value any) error

func (*RedisClient) HSetMany

func (c *RedisClient) HSetMany(ctx context.Context, key string, values map[string]any) error

Hash operations

func (*RedisClient) HSetWithEviction

func (c *RedisClient) HSetWithEviction(ctx context.Context, hashKey, orderListKey string, maxFields int64, field string, value any) error

func (*RedisClient) HVals

func (c *RedisClient) HVals(ctx context.Context, key string) ([]string, error)

func (*RedisClient) IsLocked

func (c *RedisClient) IsLocked(ctx context.Context, key string) (bool, error)

IsLocked checks if a lock exists for the given key

func (*RedisClient) Key

func (c *RedisClient) Key(key string) string

func (*RedisClient) Keys

func (c *RedisClient) Keys(ctx context.Context, pattern string) ([]string, error)

Keys returns list of keys. This method should be used mostly for testing, as in prod many keys maybe returned. It blocks and scans the entire Redis keyspace — not safe for large production datasets.

func (*RedisClient) LIndex

func (c *RedisClient) LIndex(ctx context.Context, key string, index int64) (string, error)

func (*RedisClient) LLen

func (c *RedisClient) LLen(ctx context.Context, key string) (int64, error)

func (*RedisClient) LPop

func (c *RedisClient) LPop(ctx context.Context, key string) (string, error)

func (*RedisClient) LPush

func (c *RedisClient) LPush(ctx context.Context, key string, values ...any) error

func (*RedisClient) LRange

func (c *RedisClient) LRange(ctx context.Context, key string, start, stop int64) ([]string, error)

func (*RedisClient) LTrim

func (c *RedisClient) LTrim(ctx context.Context, key string, start, stop int64) error

func (*RedisClient) Ping

func (c *RedisClient) Ping(ctx context.Context) error

func (*RedisClient) RPop

func (c *RedisClient) RPop(ctx context.Context, key string) (string, error)

func (*RedisClient) RPush

func (c *RedisClient) RPush(ctx context.Context, key string, values ...any) error

func (*RedisClient) RawClient

func (c *RedisClient) RawClient() *redis.Client

RawClient returns the raw Redis client This is useful for using the client in a context where the Provider interface is not used

func (*RedisClient) ReleaseLock

func (c *RedisClient) ReleaseLock(ctx context.Context, key string) (bool, error)

ReleaseLock releases a distributed lock by deleting the lock key Note: This is a simple implementation. For production use, you might want to verify that the lock belongs to the current process before releasing it

func (*RedisClient) SAdd

func (c *RedisClient) SAdd(ctx context.Context, key string, members ...any) error

func (*RedisClient) SAddWithEviction

func (c *RedisClient) SAddWithEviction(ctx context.Context, key string, listKey string, limit int64, member string) error

func (*RedisClient) SCard

func (c *RedisClient) SCard(ctx context.Context, key string) (int64, error)

func (*RedisClient) SIsMember

func (c *RedisClient) SIsMember(ctx context.Context, key string, member any) (bool, error)

func (*RedisClient) SMembers

func (c *RedisClient) SMembers(ctx context.Context, key string) ([]string, error)

func (*RedisClient) SRem

func (c *RedisClient) SRem(ctx context.Context, key string, members ...any) error

func (*RedisClient) ScanKeys

func (c *RedisClient) ScanKeys(ctx context.Context, pattern string, limit int) ([]string, error)

ScanKeys returns list of keys.

func (*RedisClient) Set

func (c *RedisClient) Set(ctx context.Context, key string, v any, expiration time.Duration) error

func (*RedisClient) SubKey

func (c *RedisClient) SubKey(key string) string

func (*RedisClient) TTL

func (c *RedisClient) TTL(ctx context.Context, key string) (time.Duration, error)

func (*RedisClient) TryAcquireRateLimit

func (c *RedisClient) TryAcquireRateLimit(ctx context.Context, key string, window time.Duration) (bool, time.Duration, error)

TryAcquireRateLimit implements a sliding window rate limiter using Redis sorted sets This allows only one execution per window duration

func (*RedisClient) TryLock

func (c *RedisClient) TryLock(ctx context.Context, key string, timeout time.Duration) (bool, time.Duration, error)

TryLock attempts to acquire a distributed lock using Redis SET with NX and EX options This implements a simple but effective distributed lock pattern

func (*RedisClient) WithPrefix

func (c *RedisClient) WithPrefix(prefix string) *RedisClient

NewWithPrefix creates a new Redis client with the given options and prefix

func (*RedisClient) ZAdd

func (c *RedisClient) ZAdd(ctx context.Context, key string, score float64, member string) error

func (*RedisClient) ZCard

func (c *RedisClient) ZCard(ctx context.Context, key string) (int64, error)

func (*RedisClient) ZIncrBy

func (c *RedisClient) ZIncrBy(ctx context.Context, key string, increment float64, member string) error

func (*RedisClient) ZRem

func (c *RedisClient) ZRem(ctx context.Context, key string, members ...any) error

func (*RedisClient) ZRemRangeByRank

func (c *RedisClient) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error)

func (*RedisClient) ZRevRangeWithScores

func (c *RedisClient) ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error)

Jump to

Keyboard shortcuts

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