Documentation
¶
Index ¶
- Constants
- Variables
- func Float64ToInt64(f float64) (int64, error)
- func IdScoreToZ(is *IdScore) (redis.Z, bool, error)
- func Int64ToFloat64(i int64) (float64, error)
- func IsDefaultZero[T any](t T) bool
- func IsRedisNil(err error) bool
- func IsRedisNoGroup(err error) bool
- func MapKV[K comparable, V any, T KeyValuer[K, V]](values []T) map[K]V
- func NewTimeZ(id int64, ts time.Time) redis.Z
- func NewZ(member string, score int64) redis.Z
- func RedisDecode(typ reflect.Type, strVal string) (any, error)
- func RedisEncode(value any) (string, error)
- func RedisInt64Decoder(s string) (int64, error)
- func RedisInt64Encoder(s int64) (string, error)
- func RedisInt64SliceDecoder(s string) ([]int64, error)
- func RedisInt64SliceEncoder(s []int64) (string, error)
- func RedisStringDecoder(s string) (string, error)
- func RedisStringEncoder(s string) (string, error)
- func RedisStringSliceDecoder(s string) ([]string, error)
- func RedisStringSliceEncoder(s []string) (string, error)
- func RedisUint64Decoder(s string) (uint64, error)
- func RedisUint64Encoder(s uint64) (string, error)
- func RedisValueDecode[T any](val any, decoder func(string) (T, error)) (exist bool, t T, err error)
- func TypeDecoder[T any](typ reflect.Type, strVal string) (t T, err error)
- func TypeEncoder[T any](value T) (string, error)
- type BatchIterator
- type Decoder
- type Encoder
- type IdScore
- type IsNil
- type KeyValuer
- type KeyValuers
- type MS
- type MapLoader
- type RedisAtomicPointer
- type RedisBatchValue
- type RedisClient
- func (c *RedisClient) C() *redis.Client
- func (c *RedisClient) Del(ctx context.Context, keys ...string) error
- func (c *RedisClient) Exist(ctx context.Context, key string) bool
- func (c *RedisClient) Get(ctx context.Context, key string, typ reflect.Type) (any, bool, error)
- func (c *RedisClient) GetIds(ctx context.Context, key string) ([]int64, bool, error)
- func (c *RedisClient) GetStringWithNil(ctx context.Context, key string) (string, error)
- func (c *RedisClient) GetValues(ctx context.Context, typ reflect.Type, keys ...string) (hitList []bool, values any, errr error)
- func (c *RedisClient) HDel(ctx context.Context, key string, fields ...string) error
- func (c *RedisClient) HDels(ctx context.Context, key string, fields ...string) error
- func (c *RedisClient) HGet(ctx context.Context, key, field string, typ reflect.Type) (any, bool, error)
- func (c *RedisClient) HGets(ctx context.Context, key string, typ reflect.Type, fields ...string) (hitList []bool, cached any, err error)
- func (c *RedisClient) HGetsAll(ctx context.Context, key string, typ reflect.Type) (any, error)
- func (c *RedisClient) HSet(ctx context.Context, key, field string, value any) error
- func (c *RedisClient) HSetMap(ctx context.Context, key string, m map[string]any) error
- func (c *RedisClient) HSets(ctx context.Context, key string, fieldValues ...any) error
- func (c *RedisClient) HSetsOnce(ctx context.Context, key string, fieldValues ...any) error
- func (c *RedisClient) InitStreamGroup(ctx context.Context, streamName string, groupName string) error
- func (c *RedisClient) QueueAck(ctx context.Context, stream, group string, ids ...string) error
- func (c *RedisClient) QueuePush(ctx context.Context, stream string, minId string, ...) error
- func (c *RedisClient) QueuePushMaxLen(ctx context.Context, stream string, maxLen int64, values ...map[string]any) error
- func (c *RedisClient) QueueRead(ctx context.Context, stream string, timeout time.Duration, count int64, ...) ([]redis.XMessage, error)
- func (c *RedisClient) QueueReadGroup(ctx context.Context, stream, group, worker string, timeout time.Duration, ...) ([]redis.XMessage, error)
- func (c *RedisClient) ResetZSet(ctx context.Context, key string, zs ...redis.Z) error
- func (c *RedisClient) Set(ctx context.Context, key string, value any, expire ...time.Duration) error
- func (c *RedisClient) SetValues(ctx context.Context, pairs ...any) error
- func (c *RedisClient) SetZSet(ctx context.Context, key string, zs ...redis.Z) error
- func (c *RedisClient) String() string
- func (c *RedisClient) Version() common.Version
- func (c *RedisClient) ZScoreByPaddedIds(ctx context.Context, key string, paddedIds ...string) (map[int64]int64, error)
- type RedisDecoder
- type RedisEncoder
- type RedisFloatDecoder
- type RedisFloatEncoder
- type RedisHasher
- func NewHashReader[K comparable, V any](client redis.Cmdable, fieldEncoder RedisEncoder[K], ...) *RedisHasher[K, V]
- func NewRedisHasher[K comparable, V any](client redis.Cmdable, fieldEncoder RedisEncoder[K], ...) *RedisHasher[K, V]
- func NewStringInt64Hasher(c redis.Cmdable) *RedisHasher[string, int64]
- func (t *RedisHasher[K, V]) FieldDecoder(decoder RedisDecoder[K]) *RedisHasher[K, V]
- func (t *RedisHasher[K, V]) FieldEncoder(encoder RedisEncoder[K]) *RedisHasher[K, V]
- func (t *RedisHasher[K, V]) HDel(ctx context.Context, key string, fields ...K) (int64, error)
- func (t *RedisHasher[K, V]) HDelFieldString(ctx context.Context, key string, fieldStrings ...string) (int64, error)
- func (t *RedisHasher[K, V]) HExists(ctx context.Context, key string, field K) (bool, error)
- func (t *RedisHasher[K, V]) HExpire(ctx context.Context, key string, seconds int64, fields ...K) ([]bool, error)
- func (t *RedisHasher[K, V]) HGet(ctx context.Context, key string, field K) (hit bool, value V, err error)
- func (t *RedisHasher[K, V]) HGetAll(ctx context.Context, key string) (map[K]V, error)
- func (t *RedisHasher[K, V]) HGetAllAndSetIfNA(ctx context.Context, key string, ...) (map[K]V, error)
- func (t *RedisHasher[K, V]) HGetAndSet(ctx context.Context, key string, ...) (v V, err error)
- func (t *RedisHasher[K, V]) HGetAndSetIfNA(ctx context.Context, key string, ...) (v V, err error)
- func (t *RedisHasher[K, V]) HGetByFieldString(ctx context.Context, key string, fieldString string) (hit bool, value V, err error)
- func (t *RedisHasher[K, V]) HGetByFieldStrings(ctx context.Context, key string, fieldStrings ...string) (hitList []bool, cached map[K]V, err error)
- func (t *RedisHasher[K, V]) HGets(ctx context.Context, key string, fields ...K) (notcached []K, cached map[K]V, err error)
- func (t *RedisHasher[K, V]) HGetsAndSets(ctx context.Context, key string, ...) (map[K]V, error)
- func (t *RedisHasher[K, V]) HGetsAndSetsIfNA(ctx context.Context, key string, ...) (map[K]V, error)
- func (t *RedisHasher[K, V]) HIncrBy(ctx context.Context, key string, k K, delta int64) (int64, error)
- func (t *RedisHasher[K, V]) HIncrByFloat(ctx context.Context, key string, k K, delta float64) (float64, error)
- func (t *RedisHasher[K, V]) HKeys(ctx context.Context, key string) ([]K, error)
- func (t *RedisHasher[K, V]) HKeysAndSetIfNA(ctx context.Context, key string, ...) (fields []K, err error)
- func (t *RedisHasher[K, V]) HMustSets(ctx context.Context, key string, values []KeyValuer[K, V], mustFields []K, ...) error
- func (t *RedisHasher[K, V]) HMustSetsMap(ctx context.Context, key string, valueMap map[K]V, mustFields []K, ...) error
- func (t *RedisHasher[K, V]) HSet(ctx context.Context, key string, field K, value V) error
- func (t *RedisHasher[K, V]) HSets(ctx context.Context, key string, values []KeyValuer[K, V]) error
- func (t *RedisHasher[K, V]) HSetsIter(ctx context.Context, key string, values iter.Seq2[K, V]) error
- func (t *RedisHasher[K, V]) HSetsMap(ctx context.Context, key string, valueMap map[K]V) error
- func (t *RedisHasher[K, V]) LoadAndSetAll(ctx context.Context, key string, ...) (all map[K]V, err error)
- func (t *RedisHasher[K, V]) ValueDecoder(decoder RedisDecoder[V]) *RedisHasher[K, V]
- func (t *RedisHasher[K, V]) ValueEncoder(encoder RedisEncoder[V]) *RedisHasher[K, V]
- type RedisKey
- type RedisList
- func (l *RedisList[V]) LLen(ctx context.Context, key string) (int64, error)
- func (l *RedisList[V]) LPop(ctx context.Context, key string) (V, error)
- func (l *RedisList[V]) LPopCount(ctx context.Context, key string, count int) ([]V, error)
- func (l *RedisList[V]) LPush(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) LPushInPipe(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) LPushX(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) LRange(ctx context.Context, key string, start, stop int64) ([]V, error)
- func (l *RedisList[V]) LRem(ctx context.Context, key string, count int64, v V) (int64, error)
- func (l *RedisList[V]) RPop(ctx context.Context, key string) (V, error)
- func (l *RedisList[V]) RPopCount(ctx context.Context, key string, count int) ([]V, error)
- func (l *RedisList[V]) RPush(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) RPushInPipe(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) RPushX(ctx context.Context, key string, vs ...V) (int64, error)
- func (l *RedisList[V]) WithBatchSize(size int) *RedisList[V]
- type RedisLock
- func (l *RedisLock) Fetch(cctx context.Context) (lockingValue string, err error)
- func (l *RedisLock) FetchOrRefresh(cctx context.Context) (lockingValue string, err error)
- func (l *RedisLock) Refresh(cctx context.Context) (err error)
- func (l *RedisLock) Release() (err error)
- func (l *RedisLock) String() string
- type RedisMSZSet
- func (ms *RedisMSZSet[M, S]) WithBatchSize(batchSize int) *RedisMSZSet[M, S]
- func (ms *RedisMSZSet[M, S]) ZAdd(ctx context.Context, key string, msmap map[M]S) (int64, error)
- func (ms *RedisMSZSet[M, S]) ZCount(ctx context.Context, key string, min, max string) (count int64, err error)
- func (ms *RedisMSZSet[M, S]) ZIncrBy(ctx context.Context, key string, incrBy float64, m M) (s S, err error)
- func (ms *RedisMSZSet[M, S]) ZMScore(ctx context.Context, key string, inputs ...M) (map[M]S, error)
- func (ms *RedisMSZSet[M, S]) ZRangeArgs(ctx context.Context, args *ZArgs, placeHolder ...M) ([]M, error)
- func (ms *RedisMSZSet[M, S]) ZRangeArgsWithScores(ctx context.Context, args *ZArgs, placeHolder ...M) ([]MS[M, S], error)
- func (ms *RedisMSZSet[M, S]) ZScore(ctx context.Context, key string, m M) (s S, err error)
- type RedisMsg
- func (m RedisMsg) ID(key string, nilIsOk ...bool) (tools.ID, error)
- func (m RedisMsg) Int64(key string, nilIsOk ...bool) (i int64, err error)
- func (m RedisMsg) IsEmpty() bool
- func (m RedisMsg) IsStopSignal() bool
- func (m RedisMsg) Map() map[string]interface{}
- func (m RedisMsg) StopSignal() RedisMsg
- func (m RedisMsg) String(key string, nilIsOk ...bool) (string, error)
- type RedisSet
- func (s *RedisSet[V]) SAdd(ctx context.Context, key string, vs ...V) (int64, error)
- func (s *RedisSet[V]) SAddInBatch(ctx context.Context, key string, vs ...V) (int64, error)
- func (s *RedisSet[V]) SAddWithEmpty(ctx context.Context, key string, emptyValue V, vs ...V) (int64, error)
- func (s *RedisSet[V]) SAddWithEmptyInBatch(ctx context.Context, key string, emptyValue V, vs ...V) (int64, error)
- func (s *RedisSet[V]) SIsMember(ctx context.Context, key string, v V) (bool, error)
- func (s *RedisSet[V]) SRem(ctx context.Context, key string, vs ...V) (int64, error)
- func (s *RedisSet[V]) SRemInBatch(ctx context.Context, key string, vs ...V) (int64, error)
- func (s *RedisSet[V]) WithBatchSize(size int) *RedisSet[V]
- type RedisString
- func NewRedisString[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueEncoder RedisEncoder[V], ...) *RedisString[K, V]
- func NewStringReader[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueDecoder RedisDecoder[V]) *RedisString[K, V]
- func NewStringWriter[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueEncoder RedisEncoder[V]) *RedisString[K, V]
- func (s *RedisString[K, V]) Client(client redis.Cmdable) *RedisString[K, V]
- func (s *RedisString[K, V]) Get(ctx context.Context, k K) (exist bool, t V, err error)
- func (s *RedisString[K, V]) GetAndSet(ctx context.Context, ...) (v V, err error)
- func (s *RedisString[K, V]) Gets(ctx context.Context, ks ...K) (notcached []K, cached map[K]V, err error)
- func (s *RedisString[K, V]) GetsAndSets(ctx context.Context, dbLoader MapLoader[K, V], ks ...K) (map[K]V, error)
- func (s *RedisString[K, V]) KeyEncoder(encoder RedisEncoder[K]) *RedisString[K, V]
- func (s *RedisString[K, V]) Set(ctx context.Context, k K, v V, expire ...time.Duration) error
- func (s *RedisString[K, V]) Sets(ctx context.Context, values map[K]V) error
- func (s *RedisString[K, V]) ValueDecoder(decoder RedisDecoder[V]) *RedisString[K, V]
- func (s *RedisString[K, V]) ValueEncoder(encoder RedisEncoder[V]) *RedisString[K, V]
- type RedisTree
- type RedisZDecoder
- type RedisZEncoder
- type RedisZSet
- func (zs *RedisZSet[Z]) BZPopMin(ctx context.Context, timeout time.Duration, key string) ([]Z, error)
- func (zs *RedisZSet[Z]) WithBatchSize(size int) *RedisZSet[Z]
- func (zs *RedisZSet[Z]) ZAdd(ctx context.Context, key string, vs ...Z) (int64, error)
- func (zs *RedisZSet[Z]) ZAddCall(op func(client redis.Cmdable, vals ...redis.Z) (int64, error), vs ...Z) (int64, error)
- func (zs *RedisZSet[Z]) ZAddNX(ctx context.Context, key string, vs ...Z) (int64, error)
- func (zs *RedisZSet[Z]) ZAddNXWithEmpty(ctx context.Context, key string, emptyValue Z, vs ...Z) (int64, error)
- func (zs *RedisZSet[Z]) ZAddWithEmpty(ctx context.Context, key string, emptyValue Z, vs ...Z) (int64, error)
- func (zs *RedisZSet[Z]) ZCard(ctx context.Context, key string) (int64, error)
- func (zs *RedisZSet[Z]) ZCount(ctx context.Context, key string, min, max string) (int64, error)
- func (zs *RedisZSet[Z]) ZPopMin(ctx context.Context, key string, count ...int64) ([]Z, error)
- func (zs *RedisZSet[Z]) ZRangeArgs(ctx context.Context, args *ZArgs) ([]Z, error)
- func (zs *RedisZSet[Z]) ZRangeWithScores(ctx context.Context, args *ZArgs) ([]Z, error)
- type Z
- type ZArgs
- func (a *ZArgs) ByIndex() *ZArgs
- func (a *ZArgs) ByLEX() *ZArgs
- func (a *ZArgs) ByScore() *ZArgs
- func (a *ZArgs) Fw() *ZArgs
- func (a *ZArgs) MaxString() string
- func (a *ZArgs) MinString() string
- func (a *ZArgs) Paginate(begin, size int64) *ZArgs
- func (a *ZArgs) Range(min, max any) *ZArgs
- func (a *ZArgs) Redis() redis.ZRangeArgs
- func (a *ZArgs) Rev() *ZArgs
- func (a *ZArgs) String() string
- func (a *ZArgs) WithKey(key string) *ZArgs
- type ZB
- type Zs
Constants ¶
const ( ModelsBatchSize = 100 BatchSize = 500 TypeKeyNone = "none" TypeKeyStream = "stream" RdbStatusOK = "OK" ErrBUSYGROUPKeyword = "BUSYGROUP" // DefaultStartScore zset default score range DefaultStartScore = "0" DefaultStopScore = "+inf" StringPlaceHolder = "_" IdPlaceHolder = 0 NegIdPlaceHolder = -1 // HashExpireMinVersion min version of redis-server starting support HExpire HashExpireMinVersion = common.Version(7004000) )
Variables ¶
var ( ErrInvalidValue = errors.New("rds: invalid value object") ErrExpectingString = errors.New("rds: expecting string") )
var EmptyZ = redis.Z{Score: -1, Member: "0"}
Functions ¶
func Float64ToInt64 ¶
func IdScoreToZ ¶
IdScoreToZ is the RedisZEncoder for *IdScore: Id becomes the member, Score the score.
func Int64ToFloat64 ¶
func IsDefaultZero ¶ added in v0.1.3
IsDefaultZero 判断泛型值 t 是否为其类型的默认零值(如 int 的 0、string 的 ""、指针的 nil)。
func IsRedisNil ¶
func IsRedisNoGroup ¶
func MapKV ¶
func MapKV[K comparable, V any, T KeyValuer[K, V]](values []T) map[K]V
func RedisEncode ¶
func RedisInt64Decoder ¶
func RedisInt64Encoder ¶
func RedisInt64SliceDecoder ¶
func RedisInt64SliceEncoder ¶
func RedisStringDecoder ¶
func RedisStringEncoder ¶
func RedisStringSliceDecoder ¶
func RedisStringSliceEncoder ¶
func RedisUint64Decoder ¶
func RedisUint64Encoder ¶
func RedisValueDecode ¶
func TypeEncoder ¶
Types ¶
type BatchIterator ¶
type BatchIterator[T any] struct { // contains filtered or unexported fields }
func NewBatchIterator ¶
func NewBatchIterator[T any](ts []T, size ...int) *BatchIterator[T]
func (*BatchIterator[T]) Current ¶
func (b *BatchIterator[T]) Current() []T
func (*BatchIterator[T]) HasNext ¶
func (b *BatchIterator[T]) HasNext() bool
func (*BatchIterator[T]) Next ¶
func (b *BatchIterator[T]) Next() []T
func (*BatchIterator[T]) String ¶
func (b *BatchIterator[T]) String() string
type Encoder ¶ added in v0.2.0
func (Encoder[T, R]) EncodesAsAny ¶ added in v0.2.0
func (Encoder[T, R]) EncodesTo ¶ added in v0.2.0
func (e Encoder[T, R]) EncodesTo(dest []R, ts ...T) error
func (Encoder[T, R]) EncodesToAsAny ¶ added in v0.2.0
type IdScore ¶
IdScore represents a zset element: an int64 member ID paired with an int64 score.
func ZToIdScore ¶
ZToIdScore is the RedisZDecoder for *IdScore: parses the member back into the ID.
type KeyValuer ¶
type KeyValuer[K comparable, V any] interface { Key() K Value() V }
type KeyValuers ¶ added in v0.2.0
type KeyValuers[K comparable, V any] []KeyValuer[K, V]
type MS ¶ added in v0.2.0
type MS[M comparable, S any] struct { M M S S }
MS is an ordered member-score pair returned by ZRangeArgsWithScores. It implements KeyValuer, so it can be converted with ConvertKV to reuse the KeyValuers iterators.
type MapLoader ¶
type MapLoader[K comparable, V any] func(ctx context.Context, ks ...K) (map[K]V, error)
MapLoader 是一个从 Redis 加载数据的回调函数类型。 接收一组 key,返回 key-value 映射,用于批量回源加载缺失数据。
type RedisAtomicPointer ¶
type RedisAtomicPointer[T any] struct { // contains filtered or unexported fields }
func NewAtomicPointer ¶
func NewAtomicPointer[T any](newT func(client *RedisClient, batchSize ...int) *T) *RedisAtomicPointer[T]
func (*RedisAtomicPointer[T]) Get ¶
func (a *RedisAtomicPointer[T]) Get(client *RedisClient, batchSize ...int) *T
type RedisBatchValue ¶
type RedisBatchValue[V any] struct { // contains filtered or unexported fields }
RedisBatchValue 批量操作的泛型辅助结构体,封装了编码/解码器和批次大小, 用于将大量数据拆分为小批次进行 Redis 操作,避免单次操作数据量过大。
type RedisClient ¶
func ConnectRedis ¶
func ConnectRedis(ctx context.Context, url string) (*RedisClient, error)
func (*RedisClient) C ¶
func (c *RedisClient) C() *redis.Client
func (*RedisClient) GetStringWithNil ¶
func (*RedisClient) GetValues ¶
func (c *RedisClient) GetValues(ctx context.Context, typ reflect.Type, keys ...string) (hitList []bool, values any, errr error)
GetValues returns hitList: same length as the input keys, indicates whether each key hit the cache values: array of decoded values or nil (not hit or cached nil)
func (*RedisClient) HGets ¶
func (c *RedisClient) HGets(ctx context.Context, key string, typ reflect.Type, fields ...string) (hitList []bool, cached any, err error)
HGets returns cached as map[string]anyOftyp
func (*RedisClient) InitStreamGroup ¶
func (c *RedisClient) InitStreamGroup(ctx context.Context, streamName string, groupName string) error
InitStreamGroup workers should call this at start procedure
func (*RedisClient) QueuePushMaxLen ¶
func (*RedisClient) QueueReadGroup ¶
func (*RedisClient) SetValues ¶
func (c *RedisClient) SetValues(ctx context.Context, pairs ...any) error
func (*RedisClient) String ¶
func (c *RedisClient) String() string
func (*RedisClient) Version ¶
func (c *RedisClient) Version() common.Version
func (*RedisClient) ZScoreByPaddedIds ¶
type RedisDecoder ¶
func NewTypeDecoder ¶
func NewTypeDecoder[T any](typ reflect.Type) RedisDecoder[T]
type RedisEncoder ¶
RedisEncoder returns "" means ignoring
type RedisFloatDecoder ¶
RedisFloatDecoder decoder for ZSet score
type RedisFloatEncoder ¶
RedisFloatEncoder encoder for ZSet score
type RedisHasher ¶
type RedisHasher[K comparable, V any] struct { // contains filtered or unexported fields }
func NewHashReader ¶
func NewHashReader[K comparable, V any](client redis.Cmdable, fieldEncoder RedisEncoder[K], fieldDecoder RedisDecoder[K], valueDecoder RedisDecoder[V]) *RedisHasher[K, V]
func NewRedisHasher ¶
func NewRedisHasher[K comparable, V any]( client redis.Cmdable, fieldEncoder RedisEncoder[K], fieldDecoder RedisDecoder[K], valueEncoder RedisEncoder[V], valueDecoder RedisDecoder[V]) *RedisHasher[K, V]
func NewStringInt64Hasher ¶
func NewStringInt64Hasher(c redis.Cmdable) *RedisHasher[string, int64]
func (*RedisHasher[K, V]) FieldDecoder ¶
func (t *RedisHasher[K, V]) FieldDecoder(decoder RedisDecoder[K]) *RedisHasher[K, V]
func (*RedisHasher[K, V]) FieldEncoder ¶
func (t *RedisHasher[K, V]) FieldEncoder(encoder RedisEncoder[K]) *RedisHasher[K, V]
func (*RedisHasher[K, V]) HDelFieldString ¶
func (*RedisHasher[K, V]) HGetAll ¶
func (t *RedisHasher[K, V]) HGetAll(ctx context.Context, key string) (map[K]V, error)
func (*RedisHasher[K, V]) HGetAllAndSetIfNA ¶
func (*RedisHasher[K, V]) HGetAndSet ¶
func (*RedisHasher[K, V]) HGetAndSetIfNA ¶
func (*RedisHasher[K, V]) HGetByFieldString ¶
func (*RedisHasher[K, V]) HGetByFieldStrings ¶
func (*RedisHasher[K, V]) HGets ¶
func (t *RedisHasher[K, V]) HGets(ctx context.Context, key string, fields ...K) (notcached []K, cached map[K]V, err error)
func (*RedisHasher[K, V]) HGetsAndSets ¶
func (t *RedisHasher[K, V]) HGetsAndSets(ctx context.Context, key string, dbLoader func(cctx context.Context, kks ...K) (map[K]V, error), ks ...K) (map[K]V, error)
HGetsAndSets read data from hashes:key using ks as the fields. If any fields do not exist, use dbLoader to read the corresponding data from the database and store it in the cache. Note that dbLoader should not return an error if the corresponding data does not exist in the database.
func (*RedisHasher[K, V]) HGetsAndSetsIfNA ¶
func (t *RedisHasher[K, V]) HGetsAndSetsIfNA(ctx context.Context, key string, allLoader func(ctx context.Context) (map[K]V, error), flagField K, kks ...K) (map[K]V, error)
HGetsAndSetsIfNA 检查key是否存在,如果不存在则通过allLoader获取所有key的数据并保存 redis的hash无法存储0个field,所以当使用hash存储对象时,如果不做特殊处理,将无法缓存不存在的key(如:数据库中不存在)。 此时通过使用在field中增加一个特殊值flag(不会与真实filed重复),用来保证没有其他field时也可以使得key在redis中。 而flag对应的值则是V的缺省值。 读取时,可以通过HMGet每次都将flag读取出来,一旦flag不存在,就说明key不存在。如果flag存在,而可以说明被读取的field是否真实存在。
func (*RedisHasher[K, V]) HIncrByFloat ¶ added in v0.2.0
func (*RedisHasher[K, V]) HKeys ¶
func (t *RedisHasher[K, V]) HKeys(ctx context.Context, key string) ([]K, error)
func (*RedisHasher[K, V]) HKeysAndSetIfNA ¶
func (*RedisHasher[K, V]) HMustSetsMap ¶
func (t *RedisHasher[K, V]) HMustSetsMap(ctx context.Context, key string, valueMap map[K]V, mustFields []K, placeholderVal V) error
func (*RedisHasher[K, V]) HSet ¶
func (t *RedisHasher[K, V]) HSet(ctx context.Context, key string, field K, value V) error
func (*RedisHasher[K, V]) HSetsMap ¶
func (t *RedisHasher[K, V]) HSetsMap(ctx context.Context, key string, valueMap map[K]V) error
func (*RedisHasher[K, V]) LoadAndSetAll ¶ added in v0.2.0
func (*RedisHasher[K, V]) ValueDecoder ¶
func (t *RedisHasher[K, V]) ValueDecoder(decoder RedisDecoder[V]) *RedisHasher[K, V]
func (*RedisHasher[K, V]) ValueEncoder ¶
func (t *RedisHasher[K, V]) ValueEncoder(encoder RedisEncoder[V]) *RedisHasher[K, V]
type RedisList ¶
type RedisList[V any] struct { RedisBatchValue[V] // contains filtered or unexported fields }
RedisList 是 Redis List 数据结构的泛型封装,支持批量操作。 通过组合 RedisBatchValue 实现自动分批编码/解码。
func NewRedisList ¶ added in v0.2.0
func NewRedisList[V any](client redis.Cmdable, encoder RedisEncoder[V], decoder RedisDecoder[V]) *RedisList[V]
func (*RedisList[V]) LPushInPipe ¶ added in v0.2.0
func (*RedisList[V]) LRange ¶
LRange 获取 Redis List 中指定范围内的元素并解码为泛型类型。 start 和 stop 为索引,支持负数索引(如 -1 表示最后一个元素)。
func (*RedisList[V]) RPushInPipe ¶ added in v0.2.0
RPushInPipe 将数据从右侧推入 Redis List。支持批量推入,自动按批次大小拆分。 返回操作后列表的长度。
func (*RedisList[V]) WithBatchSize ¶
WithBatchSize 设置批量操作时的批次大小,返回自身以支持链式调用。
type RedisLock ¶
type RedisLock struct {
// contains filtered or unexported fields
}
func NewRedisLock ¶
func (*RedisLock) FetchOrRefresh ¶
type RedisMSZSet ¶
type RedisMSZSet[M comparable, S any] struct { // contains filtered or unexported fields }
RedisMSZSet is a generic Redis sorted-set wrapper that keeps members (M) and scores (S) as separate first-class types: scores can be read and updated independently via ZScore/ZMScore/ZIncrBy. Use NewIDZSet for the common int64/int64 case, or NewMSZSet with custom codecs.
func NewIDZSet ¶
func NewIDZSet(client redis.Cmdable) *RedisMSZSet[int64, int64]
NewIDZSet creates a RedisMSZSet[int64, int64]: both members and scores are int64.
func NewMSZSet ¶
func NewMSZSet[M comparable, S any](client redis.Cmdable, memberEncoder RedisEncoder[M], memberDecoder RedisDecoder[M], scoreEncoder RedisFloatEncoder[S], scoreDecoder RedisFloatDecoder[S], batchSize ...int) *RedisMSZSet[M, S]
NewMSZSet creates a RedisMSZSet with custom member/score codecs. batchSize optionally overrides the write batch size.
func (*RedisMSZSet[M, S]) WithBatchSize ¶ added in v0.1.1
func (ms *RedisMSZSet[M, S]) WithBatchSize(batchSize int) *RedisMSZSet[M, S]
WithBatchSize sets the write batch size.
func (*RedisMSZSet[M, S]) ZAdd ¶ added in v0.1.1
ZAdd upserts the members with their scores (existing members get their score updated) and returns the number of new members.
func (*RedisMSZSet[M, S]) ZCount ¶
func (ms *RedisMSZSet[M, S]) ZCount(ctx context.Context, key string, min, max string) (count int64, err error)
ZCount counts the members with scores in [min, max]. Bounds are inclusive by default; prefix a bound with "(" to exclude it.
func (*RedisMSZSet[M, S]) ZIncrBy ¶
func (ms *RedisMSZSet[M, S]) ZIncrBy(ctx context.Context, key string, incrBy float64, m M) (s S, err error)
ZIncrBy increments the score of member m by incrBy and returns the new score.
func (*RedisMSZSet[M, S]) ZMScore ¶
func (ms *RedisMSZSet[M, S]) ZMScore(ctx context.Context, key string, inputs ...M) (map[M]S, error)
ZMScore returns the scores of the given members (deduplicated) as a map. Members whose scores fail to decode are skipped.
func (*RedisMSZSet[M, S]) ZRangeArgs ¶
func (ms *RedisMSZSet[M, S]) ZRangeArgs(ctx context.Context, args *ZArgs, placeHolder ...M) ([]M, error)
ZRangeArgs returns the members in the range without scores. placeHolder optionally filters placeholder members from the result.
func (*RedisMSZSet[M, S]) ZRangeArgsWithScores ¶ added in v0.2.0
func (ms *RedisMSZSet[M, S]) ZRangeArgsWithScores(ctx context.Context, args *ZArgs, placeHolder ...M) ([]MS[M, S], error)
ZRangeArgsWithScores returns the ordered member-score pairs (MS[M, S]) in the range. placeHolder filters placeholder members like in ZRangeArgs. Note: Redis does not support WITHSCORES combined with BYLEX; use ZRangeArgs for lex ranges.
type RedisMsg ¶
type RedisMsg map[string]interface{}
func (RedisMsg) IsStopSignal ¶
func (RedisMsg) StopSignal ¶
type RedisSet ¶
type RedisSet[V any] struct { RedisBatchValue[V] // contains filtered or unexported fields }
func NewRedisSet ¶
func NewRedisSet[V any](client redis.Cmdable, encoder RedisEncoder[V], decoder RedisDecoder[V]) *RedisSet[V]
func (*RedisSet[V]) SAddInBatch ¶ added in v0.2.0
func (*RedisSet[V]) SAddWithEmpty ¶
func (*RedisSet[V]) SAddWithEmptyInBatch ¶ added in v0.2.0
func (*RedisSet[V]) SRemInBatch ¶ added in v0.2.0
func (*RedisSet[V]) WithBatchSize ¶
type RedisString ¶
type RedisString[K comparable, V any] struct { RedisKey[K] // contains filtered or unexported fields }
func NewRedisString ¶
func NewRedisString[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueEncoder RedisEncoder[V], valueDecoder RedisDecoder[V]) *RedisString[K, V]
func NewStringReader ¶
func NewStringReader[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueDecoder RedisDecoder[V]) *RedisString[K, V]
func NewStringWriter ¶
func NewStringWriter[K comparable, V any](client redis.Cmdable, keyEncoder RedisEncoder[K], valueEncoder RedisEncoder[V]) *RedisString[K, V]
func (*RedisString[K, V]) Client ¶
func (s *RedisString[K, V]) Client(client redis.Cmdable) *RedisString[K, V]
func (*RedisString[K, V]) Get ¶
func (s *RedisString[K, V]) Get(ctx context.Context, k K) (exist bool, t V, err error)
func (*RedisString[K, V]) Gets ¶
func (s *RedisString[K, V]) Gets(ctx context.Context, ks ...K) (notcached []K, cached map[K]V, err error)
func (*RedisString[K, V]) GetsAndSets ¶
func (s *RedisString[K, V]) GetsAndSets(ctx context.Context, dbLoader MapLoader[K, V], ks ...K) (map[K]V, error)
func (*RedisString[K, V]) KeyEncoder ¶
func (s *RedisString[K, V]) KeyEncoder(encoder RedisEncoder[K]) *RedisString[K, V]
func (*RedisString[K, V]) Sets ¶
func (s *RedisString[K, V]) Sets(ctx context.Context, values map[K]V) error
func (*RedisString[K, V]) ValueDecoder ¶
func (s *RedisString[K, V]) ValueDecoder(decoder RedisDecoder[V]) *RedisString[K, V]
func (*RedisString[K, V]) ValueEncoder ¶
func (s *RedisString[K, V]) ValueEncoder(encoder RedisEncoder[V]) *RedisString[K, V]
type RedisTree ¶ added in v0.1.3
type RedisTree[K1 comparable, K2 comparable, V any] struct { // contains filtered or unexported fields }
RedisTree 是 Redis 二级树形数据结构的泛型封装。 第一级 key(K1)映射到第二级 key 列表([]K2),第二级 key(K2)映射到实际数据(V)。 适用于父子关系数据的缓存场景,如分类-商品、用户-订单等。
func NewRedisTree ¶ added in v0.1.3
func NewRedisTree[K1 comparable, K2 comparable, V any]( parentRS *RedisString[K1, []K2], parentLoader MapLoader[K1, []K2], childRS *RedisString[K2, V], childLoader MapLoader[K2, V], isChildNil ...IsNil[V]) *RedisTree[K1, K2, V]
NewRedisTree 创建一个新的 RedisTree 实例。 isChildNil 为可选参数,用于自定义判断子级值是否为空的逻辑; 如果不提供,则默认使用 IsDefaultZero 判断。
type RedisZEncoder ¶
RedisZEncoder returns ok==false means ignoring the value
type RedisZSet ¶
type RedisZSet[Z any] struct { // contains filtered or unexported fields }
RedisZSet is a generic Redis sorted-set wrapper for values of type Z, where Z carries both the member and the score. Use NewIdScoreZSet for the common {Id, Score} case, or NewRedisZSet with custom codecs.
func NewIdScoreZSet ¶
NewIdScoreZSet creates a RedisZSet[*IdScore] with the built-in codecs.
func NewRedisZSet ¶
func NewRedisZSet[Z any](client redis.Cmdable, encoder RedisZEncoder[Z], decoder RedisZDecoder[Z], batchSize ...int) *RedisZSet[Z]
NewRedisZSet creates a RedisZSet[Z] with custom encode/decode functions. encoder returns ok=false to skip a value. batchSize optionally overrides the write batch size.
func (*RedisZSet[Z]) BZPopMin ¶
func (zs *RedisZSet[Z]) BZPopMin(ctx context.Context, timeout time.Duration, key string) ([]Z, error)
BZPopMin blocks up to timeout for the member with the lowest score. The redis.Nil timeout error is converted to a nil slice, so nil means "no result".
func (*RedisZSet[Z]) WithBatchSize ¶
WithBatchSize sets the write batch size; values are sent in batches to avoid oversized commands.
func (*RedisZSet[Z]) ZAdd ¶
ZAdd upserts values (existing members get their score updated) and returns the number of new members.
func (*RedisZSet[Z]) ZAddCall ¶
func (zs *RedisZSet[Z]) ZAddCall(op func(client redis.Cmdable, vals ...redis.Z) (int64, error), vs ...Z) (int64, error)
ZAddCall runs a custom add op (e.g. ZAddNX) in batches over the encoded values.
func (*RedisZSet[Z]) ZAddNX ¶
ZAddNX adds values only for members not already present and returns the number of new members.
func (*RedisZSet[Z]) ZAddNXWithEmpty ¶
func (zs *RedisZSet[Z]) ZAddNXWithEmpty(ctx context.Context, key string, emptyValue Z, vs ...Z) (int64, error)
ZAddNXWithEmpty behaves like ZAddNX but writes emptyValue when vs is empty.
func (*RedisZSet[Z]) ZAddWithEmpty ¶
func (zs *RedisZSet[Z]) ZAddWithEmpty(ctx context.Context, key string, emptyValue Z, vs ...Z) (int64, error)
ZAddWithEmpty behaves like ZAdd but writes emptyValue when vs is empty.
func (*RedisZSet[Z]) ZCount ¶
ZCount counts the members with scores in [min, max]. Bounds are inclusive by default; prefix a bound with "(" to exclude it.
func (*RedisZSet[Z]) ZPopMin ¶
ZPopMin removes and returns up to count members with the lowest scores (default 1), in ascending order.
func (*RedisZSet[Z]) ZRangeArgs ¶ added in v0.2.0
ZRangeArgs returns the members in the range without scores, mirroring RedisMSZSet.ZRangeArgs. Note: decoded Z values only carry the member; Score is 0. Use ZRangeWithScores when scores are needed.
func (*RedisZSet[Z]) ZRangeWithScores ¶
ZRangeWithScores returns the full Z entries (member + score) in the range; the ordering is controlled by ZArgs (ByIndex/ByScore/ByLEX). Note: Redis does not support WITHSCORES combined with BYLEX; use ZRangeArgs for lex ranges.
type ZArgs ¶
type ZArgs struct {
// contains filtered or unexported fields
}
ZArgs describes a ZRANGE query: key, range bounds, ordering mode, direction and pagination. Build it fluently, e.g. NewZArgs(key).ByScore().Rev().Range(min, max).
func NewZArgs ¶ added in v0.1.1
NewZArgs creates a ZArgs for the given key (index mode, forward by default).
func (*ZArgs) ByIndex ¶
ByIndex selects range by index (the default). With REV, indices are interpreted on the reversed set (0 = highest score).
func (*ZArgs) ByLEX ¶
ByLEX selects range by lexicographical order. Only meaningful when all members share the same score.
func (*ZArgs) MaxString ¶
MaxString renders the range maximum as a Redis boundary string. Panics on unsupported boundary types.
func (*ZArgs) MinString ¶
MinString renders the range minimum as a Redis boundary string. Panics on unsupported boundary types.
func (*ZArgs) Range ¶
Range sets the range bounds. min is always the smaller value and max the larger one; with REV the effective interval on the wire becomes (max, min).
func (*ZArgs) Redis ¶
func (a *ZArgs) Redis() redis.ZRangeArgs
Redis converts the query into go-redis ZRangeArgs. Redis expects "ZRANGE key max min BYSCORE REV": with REV the min/max positions are swapped on the wire. go-redis v9.14.0 swapped them automatically in appendArgs, but v9.21.0 removed that and leaves it to the caller, so swap here to match String(). Index ranges with REV need no swap (indices are interpreted on the reversed set).
func (*ZArgs) Rev ¶
Rev sets reverse order (highest first). For REV with BYSCORE/BYLEX, pass the bounds in ascending order; they are swapped for the wire automatically.
type ZB ¶
type ZB struct {
V any
AsScore bool // true for score range, false for lexicographical range
Excluded bool
}
ZB is a boundary value for SCORE or LEX ranges used by ZRange. Use ForScore/ForLex to pick the range mode and Val/Infinite to set the value.
func (*ZB) Boundary ¶
Boundary renders the boundary for a Redis command. isMin selects the open-ended default: -inf/- for a minimum, +inf/+ for a maximum. Panics on unsupported value types.