Documentation
¶
Overview ¶
Package redisclient wraps github.com/redis/go-redis/v9 with a key prefix, JSON marshalling of values, error wrapping and a set of higher-level helpers: bounded ("with eviction") sets and hashes, a simple distributed lock and a one-execution-per-window rate limiter.
RedisClient embeds *redis.Client, so raw commands remain available, while the wrapper methods apply the prefix via Key. Provider is the interface implemented by RedisClient and is what consumers should depend on.
rc, err := redisclient.New(&redisclient.Config{
Server: "redis://localhost:6379/0",
Password: "secret",
})
if err != nil {
return err
}
defer rc.Close()
c := rc.WithPrefix("myapp") // keys become "/myapp/<key>"
if err := c.Set(ctx, "user:1", user, time.Hour); err != nil {
return err
}
var u User
if err := c.Get(ctx, "user:1", &u); redisclient.IsNotFoundError(err) {
// missing
}
Config YAML/JSON fields: server (redis:// or rediss:// URL), ttl, client_tls {cert, key, trusted_ca}, user, password. Password from Config overrides credentials embedded in the URL.
Values: string and []byte are stored as-is, everything else is JSON encoded (see Marshal / UnmarshalStringCmd). Get and HGet return ErrNotFound for a missing key; the other commands return the wrapped go-redis error. WithPrefix returns a child that shares the connection and whose Close is a no-op; only the root client returned by New closes the connection.
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 is returned by Get and HGet for a missing key or field.
Functions ¶
func IsNotFoundError ¶
IsNotFoundError reports whether err is (or wraps) ErrNotFound, or whose message contains "not found".
func Marshal ¶
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 ¶
NewRedisClient builds a raw *redis.Client from cfg: the URL is parsed, TLS is configured from ClientTLS files, Password/User override the URL credentials, and maintenance notifications are disabled. The connection is established lazily.
Types ¶
type Config ¶
type Config struct {
// Server is the redis:// or rediss:// URL (see redis.ParseURL).
Server string `json:"server,omitempty" yaml:"server,omitempty"`
// TTL is informational for callers; the client itself applies no default expiry.
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 is the ACL user name; only applied when Password is set.
User string `json:"user,omitempty" yaml:"user,omitempty"`
// Password overrides the credentials embedded in Server.
Password string `json:"password,omitempty" yaml:"password,omitempty"`
}
Config specifies the Redis connection.
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
}
Provider is the Redis operations interface implemented by *RedisClient; depend on it rather than on the concrete type. All keys are relative to the client prefix. See the RedisClient methods for per-operation details.
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 ¶
RedisClient implements Provider on top of an embedded *redis.Client. The wrapper methods prefix keys (see Key) and wrap errors; the embedded client's own methods are also reachable but bypass the prefix. Create it with New or NewWithClient, and derive namespaced children with WithPrefix.
func New ¶
func New(cfg *Config) (*RedisClient, error)
New creates a root RedisClient (no prefix) from cfg. The returned client owns the connection: its Close closes it.
func NewWithClient ¶
func NewWithClient(client *redis.Client) (*RedisClient, error)
NewWithClient wraps an existing *redis.Client without a prefix. The caller keeps ownership of the connection: Close is a no-op. The error is always nil.
func (*RedisClient) Close ¶
func (c *RedisClient) Close() error
Close closes the underlying connection when this client owns it (created by New); for clients from NewWithClient or WithPrefix it is a no-op. Close errors are logged, never returned; the client must not be used afterwards.
func (*RedisClient) Del ¶
func (c *RedisClient) Del(ctx context.Context, key string) error
Del removes key; a missing key is not an error.
func (*RedisClient) Expire ¶
func (c *RedisClient) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
Expire sets the TTL of key and reports whether the key existed.
func (*RedisClient) Get ¶
Get loads the value stored under key into v, which must be a non-nil pointer (see UnmarshalStringCmd). It returns ErrNotFound for a missing key.
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) HGet ¶
HGet returns a field of the hash at key, or ErrNotFound when the field or the hash is missing.
func (*RedisClient) HGetAll ¶
HGetAll returns all fields of the hash at key (empty map when missing).
func (*RedisClient) HSetMany ¶
HSetMany sets several fields of the hash at key in one HSET command. An empty map is a no-op.
func (*RedisClient) HSetWithEviction ¶
func (c *RedisClient) HSetWithEviction(ctx context.Context, hashKey, orderListKey string, maxFields int64, field string, value any) error
HSetWithEviction sets field in the hash at hashKey and records the insertion order in the list at orderListKey; when the list grows beyond maxFields the oldest field is deleted from the hash. The steps are separate commands (not atomic) and errors from the eviction step are ignored.
func (*RedisClient) Key ¶
func (c *RedisClient) Key(key string) string
Key returns the full Redis key for key: the prefix joined with key using path.Join (so "a//b" and "../x" are cleaned), or key itself when there is no prefix.
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) LPop ¶
LPop removes and returns the first element of the list at key. An empty or missing list yields a wrapped redis.Nil error, not ErrNotFound.
func (*RedisClient) LRange ¶
LRange returns the elements of the list at key between the start and stop indexes (inclusive, negative counts from the end).
func (*RedisClient) LTrim ¶
LTrim keeps only the elements of the list at key between start and stop.
func (*RedisClient) Ping ¶
func (c *RedisClient) Ping(ctx context.Context) error
Ping checks connectivity to the server.
func (*RedisClient) RPop ¶
RPop removes and returns the last element of the list at key. An empty or missing list yields a wrapped redis.Nil error, not ErrNotFound.
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 (c *RedisClient) SAddWithEviction(ctx context.Context, key string, listKey string, limit int64, member string) error
SAddWithEviction adds member to the set at key and records the insertion order in the list at listKey; when the list grows beyond limit the oldest member is removed from both. The steps are separate commands (not atomic) and errors from the eviction step are ignored.
func (*RedisClient) ScanKeys ¶
ScanKeys returns keys matching pattern (relative to the prefix) using SCAN in batches of 100, stopping once at least limit keys were collected (limit <= 0 means 1000) or the scan completes. It may return up to 99 keys more than limit.
func (*RedisClient) Set ¶
Set stores v under key (see Marshal for the encoding) with the given expiration; 0 means no expiry and redis.KeepTTL keeps the existing one.
func (*RedisClient) SubKey ¶
func (c *RedisClient) SubKey(key string) string
SubKey strips the client prefix from a full Redis key, inverting Key.
func (*RedisClient) TryAcquireRateLimit ¶
func (c *RedisClient) TryAcquireRateLimit(ctx context.Context, key string, window time.Duration) (bool, time.Duration, error)
TryAcquireRateLimit implements a sliding-window limiter that allows one execution per window for key, using the sorted set "ratelimit:<key>" and the "ratelimit_window:<key>" value. Every call, allowed or not, records its timestamp, so continuous denied attempts keep the window busy. It returns (true, 0, nil) when allowed, otherwise (false, time until the window resets, nil). The commands are pipelined but not transactional.
func (*RedisClient) TryLock ¶
func (c *RedisClient) TryLock(ctx context.Context, key string, timeout time.Duration) (bool, time.Duration, error)
TryLock attempts to acquire the lock "lock:<key>" with SET NX EX, expiring after timeout. It returns (true, 0, nil) when acquired, otherwise (false, remaining TTL, nil). The lock is not owner-bound: any caller can release it with ReleaseLock.
func (*RedisClient) WithPrefix ¶
func (c *RedisClient) WithPrefix(prefix string) *RedisClient
WithPrefix returns a child client sharing the connection whose keys are namespaced under "/<prefix>/" (surrounding spaces and slashes are trimmed). The child's Close is a no-op. Prefixes do not nest: the parent prefix is ignored.
func (*RedisClient) ZAdd ¶
ZAdd adds member with score to the sorted set at key, updating the score if the member exists. The go-redis error is returned unwrapped.
func (*RedisClient) ZIncrBy ¶
func (c *RedisClient) ZIncrBy(ctx context.Context, key string, increment float64, member string) error
ZIncrBy adds increment to the score of member in the sorted set at key. The go-redis error is returned unwrapped.
func (*RedisClient) ZRem ¶
ZRem removes members from the sorted set at key. The go-redis error is returned unwrapped.
func (*RedisClient) ZRemRangeByRank ¶
func (c *RedisClient) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error)
ZRemRangeByRank removes the elements of the sorted set at key between the start and stop ranks (0-based, ascending by score) and returns the count removed. Use (0, -N-1) to keep only the top N. The go-redis error is returned unwrapped.
func (*RedisClient) ZRevRangeWithScores ¶
func (c *RedisClient) ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error)
ZRevRangeWithScores returns the elements of the sorted set at key between the start and stop ranks ordered by score from high to low, with scores. Use (0, N-1) for the top N and (-N, -1) for the bottom N.