Documentation
¶
Index ¶
- Variables
- func IsNotFoundError(err error) bool
- func Marshal(v any) (any, error)
- func NewRedisClient(cfg *Config) (*redis.Client, error)
- func UnmarshalStringCmd(val *redis.StringCmd, v any) error
- type Config
- type DistributedLock
- type Provider
- type RateLimiter
- type RedisClient
- func (c *RedisClient) Close() error
- func (c *RedisClient) Del(ctx context.Context, key string) error
- func (c *RedisClient) Exists(ctx context.Context, key string) (bool, error)
- func (c *RedisClient) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
- func (c *RedisClient) Get(ctx context.Context, key string, v any) error
- func (c *RedisClient) GetRateLimitRemainingTime(ctx context.Context, key string) (time.Duration, error)
- func (c *RedisClient) HDel(ctx context.Context, key string, fields ...string) error
- func (c *RedisClient) HExists(ctx context.Context, key string, field string) (bool, error)
- func (c *RedisClient) HGet(ctx context.Context, key string, field string) (string, error)
- func (c *RedisClient) HGetAll(ctx context.Context, key string) (map[string]string, error)
- func (c *RedisClient) HKeys(ctx context.Context, key string) ([]string, error)
- func (c *RedisClient) HSet(ctx context.Context, key string, field string, value any) error
- func (c *RedisClient) HSetMany(ctx context.Context, key string, values map[string]any) error
- func (c *RedisClient) HSetWithEviction(ctx context.Context, hashKey, orderListKey string, maxFields int64, ...) error
- func (c *RedisClient) HVals(ctx context.Context, key string) ([]string, error)
- func (c *RedisClient) IsLocked(ctx context.Context, key string) (bool, error)
- func (c *RedisClient) Key(key string) string
- func (c *RedisClient) Keys(ctx context.Context, pattern string) ([]string, error)
- func (c *RedisClient) LIndex(ctx context.Context, key string, index int64) (string, error)
- func (c *RedisClient) LLen(ctx context.Context, key string) (int64, error)
- func (c *RedisClient) LPop(ctx context.Context, key string) (string, error)
- func (c *RedisClient) LPush(ctx context.Context, key string, values ...any) error
- func (c *RedisClient) LRange(ctx context.Context, key string, start, stop int64) ([]string, error)
- func (c *RedisClient) LTrim(ctx context.Context, key string, start, stop int64) error
- func (c *RedisClient) Ping(ctx context.Context) error
- func (c *RedisClient) RPop(ctx context.Context, key string) (string, error)
- func (c *RedisClient) RPush(ctx context.Context, key string, values ...any) error
- func (c *RedisClient) RawClient() *redis.Client
- func (c *RedisClient) ReleaseLock(ctx context.Context, key string) (bool, error)
- func (c *RedisClient) SAdd(ctx context.Context, key string, members ...any) error
- func (c *RedisClient) SAddWithEviction(ctx context.Context, key string, listKey string, limit int64, member string) error
- func (c *RedisClient) SCard(ctx context.Context, key string) (int64, error)
- func (c *RedisClient) SIsMember(ctx context.Context, key string, member any) (bool, error)
- func (c *RedisClient) SMembers(ctx context.Context, key string) ([]string, error)
- func (c *RedisClient) SRem(ctx context.Context, key string, members ...any) error
- func (c *RedisClient) ScanKeys(ctx context.Context, pattern string, limit int) ([]string, error)
- func (c *RedisClient) Set(ctx context.Context, key string, v any, expiration time.Duration) error
- func (c *RedisClient) SubKey(key string) string
- func (c *RedisClient) TTL(ctx context.Context, key string) (time.Duration, error)
- func (c *RedisClient) TryAcquireRateLimit(ctx context.Context, key string, window time.Duration) (bool, time.Duration, error)
- func (c *RedisClient) TryLock(ctx context.Context, key string, timeout time.Duration) (bool, time.Duration, error)
- func (c *RedisClient) WithPrefix(prefix string) *RedisClient
- func (c *RedisClient) ZAdd(ctx context.Context, key string, score float64, member string) error
- func (c *RedisClient) ZCard(ctx context.Context, key string) (int64, error)
- func (c *RedisClient) ZIncrBy(ctx context.Context, key string, increment float64, member string) error
- func (c *RedisClient) ZRem(ctx context.Context, key string, members ...any) error
- func (c *RedisClient) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error)
- func (c *RedisClient) ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error)
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound defines not found error
Functions ¶
func IsNotFoundError ¶
IsNotFoundError returns true, if error is NotFound
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 ¶
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) 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) HSetWithEviction ¶
func (*RedisClient) Key ¶
func (c *RedisClient) Key(key string) string
func (*RedisClient) Keys ¶
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) 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 ¶
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) SAddWithEviction ¶
func (*RedisClient) SubKey ¶
func (c *RedisClient) SubKey(key string) string
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