redistool

package
v17.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_internal_tool_redistool_redistool_proto protoreflect.FileDescriptor

Functions

func MultiErrors

func MultiErrors(resp []rueidis.RedisResult) []error

func PrefixedInt64Key

func PrefixedInt64Key(prefix string, key int64) string

func UnixDialer

func UnixDialer(addr string, dialer *net.Dialer, tlsConfig *tls.Config) (net.Conn, error)

UnixDialer can be used as DialFn in rueidis.ClientOption.

Types

type BuilderKV

type BuilderKV[K2 any] struct {
	HashKey K2
	// Value is the value to store in Redis.
	Value *ExpiringValue
}

type ErrCacher

type ErrCacher[K any] struct {
	Log           *slog.Logger
	ErrRep        errz.ErrReporter
	Client        rueidis.Client
	ErrMarshaler  ErrMarshaler
	KeyToRedisKey KeyToRedisKey[K]
}

func (*ErrCacher[K]) CacheError

func (c *ErrCacher[K]) CacheError(ctx context.Context, key K, err error, errTTL time.Duration)

func (*ErrCacher[K]) GetError

func (c *ErrCacher[K]) GetError(ctx context.Context, key K) error

type ErrMarshaler

type ErrMarshaler interface {
	// Marshal turns error into []byte.
	Marshal(error) ([]byte, error)
	// Unmarshal turns []byte into error.
	Unmarshal([]byte) (error, error)
}

type ExpiringHash

type ExpiringHash[K1 any, K2 any] interface {
	// GetName returns the name of this hash
	GetName() string
	// SetEX sets the key -> hashKey -> value.
	// Use this method to re-set (i.e. refresh) the value.
	// Safe for concurrent use.
	SetEX(ctx context.Context, key K1, hashKey K2, value []byte, expiresAt time.Time) error
	// Scan iterates key-value pairs for key.
	// Safe for concurrent use.
	Scan(ctx context.Context, key K1, cb ScanCallback) error
	// Len returns number of key-value mappings in the hash identified by key.
	Len(ctx context.Context, key K1) (int64, error)
	// GC returns a function that iterates all relevant stored data and deletes expired entries.
	// The returned function can be called concurrently as it does not interfere with the hash's operation.
	// The function returns number of deleted Redis (hash) keys, including when an error occurred.
	// It only inspects/GCs hashes where it has entries. Other concurrent clients GC same and/or other corresponding hashes.
	// Hashes that don't have a corresponding client (e.g. because it crashed) will expire because of TTL on the hash key.
	GC() func(context.Context) (int, error)
	// GCFor returns a function that iterates the hash for the given keys and deletes expired entries.
	// GCFor is useful when executing GC for specific keys.
	GCFor(keys []K1) func(context.Context) (int, error)
	DelEX(ctx context.Context, key K1, hashkKey K2) error
}

ExpiringHash represents a two-level hash: key K1 -> hashKey K2 -> value []byte. key identifies the hash; hashKey identifies the key in the hash; value is the value for the hashKey.

type ExpiringHashAPI

type ExpiringHashAPI[K1 any, K2 any] interface {
	IOBuilder() IOBuilder[K1, K2]
	Scan(ctx context.Context, key K1, cb ScanCallback) error
	GCFor(keys []K1, transactional bool) func(context.Context) (int, error)
}

ExpiringHashAPI represents a low-level API to work with a two-level hash: key K1 -> hashKey K2 -> value []byte. key identifies the hash; hashKey identifies the key in the hash; value is the value for the hashKey.

type ExpiringValue

type ExpiringValue struct {
	ExpiresAt int64  `protobuf:"varint,1,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"`
	Value     []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
	// contains filtered or unexported fields
}

func (*ExpiringValue) Descriptor deprecated

func (*ExpiringValue) Descriptor() ([]byte, []int)

Deprecated: Use ExpiringValue.ProtoReflect.Descriptor instead.

func (*ExpiringValue) GetExpiresAt

func (x *ExpiringValue) GetExpiresAt() int64

func (*ExpiringValue) GetValue

func (x *ExpiringValue) GetValue() []byte

func (*ExpiringValue) ProtoMessage

func (*ExpiringValue) ProtoMessage()

func (*ExpiringValue) ProtoReflect

func (x *ExpiringValue) ProtoReflect() protoreflect.Message

func (*ExpiringValue) Reset

func (x *ExpiringValue) Reset()

func (*ExpiringValue) String

func (x *ExpiringValue) String() string

type ExpiringValueTimestamp

type ExpiringValueTimestamp struct {
	ExpiresAt int64 `protobuf:"varint,1,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"`
	// contains filtered or unexported fields
}

func (*ExpiringValueTimestamp) Descriptor deprecated

func (*ExpiringValueTimestamp) Descriptor() ([]byte, []int)

Deprecated: Use ExpiringValueTimestamp.ProtoReflect.Descriptor instead.

func (*ExpiringValueTimestamp) GetExpiresAt

func (x *ExpiringValueTimestamp) GetExpiresAt() int64

func (*ExpiringValueTimestamp) ProtoMessage

func (*ExpiringValueTimestamp) ProtoMessage()

func (*ExpiringValueTimestamp) ProtoReflect

func (x *ExpiringValueTimestamp) ProtoReflect() protoreflect.Message

func (*ExpiringValueTimestamp) Reset

func (x *ExpiringValueTimestamp) Reset()

func (*ExpiringValueTimestamp) String

func (x *ExpiringValueTimestamp) String() string

type IOBuilder added in v17.4.0

type IOBuilder[K1 any, K2 any] interface {
	// Set enqueues a HSET command for each key in keys. Does nothing if no keys or kvs provided.
	Set(keys []K1, ttl time.Duration, kvs ...BuilderKV[K2])
	// Unset enqueues a HDEL command for each key. Does nothing if no keys or no hashKeys provided.
	Unset(keys []K1, hashKeys ...K2)
	// Do executes enqueued commands. Does nothing if no commands have been enqueued.
	Do(context.Context) error
}

IOBuilder allows to batch Set and Unset commands. Can be reused after the Do method is called.

type KeyToRedisKey

type KeyToRedisKey[K any] func(key K) string

KeyToRedisKey is used to convert typed key (key1 or key2) into a string. HSET key1 key2 value.

type RPCAPI

type RPCAPI interface {
	Log() *slog.Logger
	HandleProcessingError(msg string, err error)
	RequestKey() []byte
}

type RedisExpiringHash

type RedisExpiringHash[K1 comparable, K2 comparable] struct {
	// contains filtered or unexported fields
}

func NewRedisExpiringHash

func NewRedisExpiringHash[K1 comparable, K2 comparable](name string, client rueidis.Client, key1ToRedisKey KeyToRedisKey[K1],
	key2ToRedisKey KeyToRedisKey[K2], ttl time.Duration, m otelmetric.Meter, transactionalGC bool) (*RedisExpiringHash[K1, K2], error)

func (*RedisExpiringHash[K1, K2]) DelEX added in v17.1.0

func (h *RedisExpiringHash[K1, K2]) DelEX(ctx context.Context, key K1, hashKey K2) error

func (*RedisExpiringHash[K1, K2]) GC

func (h *RedisExpiringHash[K1, K2]) GC() func(context.Context) (int, error)

func (*RedisExpiringHash[K1, K2]) GCFor

func (h *RedisExpiringHash[K1, K2]) GCFor(keys []K1) func(context.Context) (int, error)

func (*RedisExpiringHash[K1, K2]) GetName

func (h *RedisExpiringHash[K1, K2]) GetName() string

func (*RedisExpiringHash[K1, K2]) Len

func (h *RedisExpiringHash[K1, K2]) Len(ctx context.Context, key K1) (size int64, retErr error)

func (*RedisExpiringHash[K1, K2]) Scan

func (h *RedisExpiringHash[K1, K2]) Scan(ctx context.Context, key K1, cb ScanCallback) error

func (*RedisExpiringHash[K1, K2]) SetEX

func (h *RedisExpiringHash[K1, K2]) SetEX(ctx context.Context, key K1, hashKey K2, value []byte, expiresAt time.Time) error

type RedisExpiringHashAPI

type RedisExpiringHashAPI[K1 any, K2 any] struct {
	// contains filtered or unexported fields
}

func NewRedisExpiringHashAPI

func NewRedisExpiringHashAPI[K1 any, K2 any](name string, client rueidis.Client, key1ToRedisKey KeyToRedisKey[K1], key2ToRedisKey KeyToRedisKey[K2], m otelmetric.Meter) (*RedisExpiringHashAPI[K1, K2], error)

func (*RedisExpiringHashAPI[K1, K2]) GCFor

func (h *RedisExpiringHashAPI[K1, K2]) GCFor(keys []K1, transactional bool) func(context.Context) (int, error)

func (*RedisExpiringHashAPI[K1, K2]) IOBuilder added in v17.4.0

func (h *RedisExpiringHashAPI[K1, K2]) IOBuilder() IOBuilder[K1, K2]

func (*RedisExpiringHashAPI[K1, K2]) Scan

func (h *RedisExpiringHashAPI[K1, K2]) Scan(ctx context.Context, key K1, cb ScanCallback) error

type ScanCallback

type ScanCallback func(rawHashKey string, value []byte, err error) (bool, error)

type TokenLimiter

type TokenLimiter struct {
	// contains filtered or unexported fields
}

TokenLimiter is a redis-based rate limiter implementing the algorithm in https://redislabs.com/redis-best-practices/basic-rate-limiting/

func NewTokenLimiter

func NewTokenLimiter(redisClient rueidis.Client, keyPrefix string,
	limitPerMinute uint64, getAPI func(context.Context) RPCAPI) *TokenLimiter

NewTokenLimiter returns a new TokenLimiter

func (*TokenLimiter) Allow

func (l *TokenLimiter) Allow(ctx context.Context) bool

Allow consumes one limitable event from the token in the context

Jump to

Keyboard shortcuts

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