Documentation
¶
Index ¶
- func AcrossMinute(key string) bool
- func AcrossSecond(key string) bool
- func AcrossTime(key string, duration time.Duration) bool
- func DefaultNameCreator(config Config, types RedisType, name ...string) string
- func LastUseTime(key string, update bool) time.Time
- func SetDefaultConnection(r *Redis)
- func SetLastUseTime(key string, t time.Time)
- type Cache
- func (c *Cache[T]) Clear() error
- func (c *Cache[T]) ClearExpired() error
- func (c *Cache[T]) Delete(keys ...string) error
- func (c *Cache[T]) Exists(key string) bool
- func (c *Cache[T]) Get(key string) (T, bool)
- func (c *Cache[T]) GetOrSet(key string, factory func(key string) (T, time.Duration)) T
- func (c *Cache[T]) GetTTL(key string) (time.Duration, bool)
- func (c *Cache[T]) Keys() ([]string, error)
- func (c *Cache[T]) Length() int
- func (c *Cache[T]) Set(key string, value T, expire time.Duration) error
- func (c *Cache[T]) SetTTL(key string, expire time.Duration) error
- type CacheConfig
- type Config
- type Lock
- type LockConfig
- type Queue
- func (q *Queue[T]) Add(value T) error
- func (q *Queue[T]) AddDelayed(value T, delay time.Duration) error
- func (q *Queue[T]) Clear() error
- func (q *Queue[T]) Complete(value T) error
- func (q *Queue[T]) DelayedLength() int
- func (q *Queue[T]) Fail(value T, err error) error
- func (q *Queue[T]) Length() int
- func (q *Queue[T]) ProcessingLength() int
- func (q *Queue[T]) StartWorker(handler func(value T) error)
- func (q *Queue[T]) StartWorkers(count int, handler func(value T) error)
- func (q *Queue[T]) Take() (T, bool)
- type QueueConfig
- type Redis
- func (r *Redis) AcrossMinute(key string) bool
- func (r *Redis) AcrossSecond(key string) bool
- func (r *Redis) AcrossTime(key string, duration time.Duration) bool
- func (r *Redis) CleanSafeTypeMap(expireDuration time.Duration) error
- func (r *Redis) Close() error
- func (r *Redis) CreateName(redisType RedisType, name ...string) string
- func (r *Redis) DeleteLastUseTime(keys ...string) error
- func (r *Redis) Deserialize(data []byte, v interface{}) error
- func (r *Redis) Do(commandName string, args ...interface{}) (interface{}, error)
- func (r *Redis) DoWithConn(conn redis.Conn, commandName string, args ...interface{}) (interface{}, error)
- func (r *Redis) GetConn() redis.Conn
- func (r *Redis) GetConnWithContext(ctx context.Context) (redis.Conn, error)
- func (r *Redis) GetSafeTypeMap() *RedisTypeMap[int64]
- func (r *Redis) LastUseTime(key string, update bool) time.Time
- func (r *Redis) NewList(name string) *RedisList
- func (r *Redis) NewLock(name string, config ...LockConfig) *Lock
- func (r *Redis) NewMap(name string) *RedisMap
- func (r *Redis) NewNumberMap(name string) *RedisNumberMap
- func (r *Redis) NewSet(name string) *RedisSet
- func (r *Redis) NewZSet(name string) *RedisZSet
- func (r *Redis) Serialize(v interface{}) ([]byte, error)
- func (r *Redis) SetLastUseTime(key string, t time.Time) error
- type RedisBuilder
- type RedisList
- func (l *RedisList) Clear() error
- func (l *RedisList) DeleteIndex(index int) error
- func (l *RedisList) DeleteRange(start, end int) error
- func (l *RedisList) DeleteValue(value interface{}, count int) error
- func (l *RedisList) Exists() bool
- func (l *RedisList) Get(start, end int) ([]interface{}, error)
- func (l *RedisList) Index(index int) (interface{}, bool)
- func (l *RedisList) IsEmpty() bool
- func (l *RedisList) IsNotEmpty() bool
- func (l *RedisList) Iterator(batchSize int) <-chan interface{}
- func (l *RedisList) Length() int
- func (l *RedisList) Pop() (interface{}, bool)
- func (l *RedisList) Push(value interface{}) error
- func (l *RedisList) SafeUpset(index int, value interface{}) (interface{}, bool, error)
- func (l *RedisList) Set(index int, value interface{}) error
- func (l *RedisList) Shift() (interface{}, bool)
- func (l *RedisList) Unshift(value interface{}) error
- type RedisMap
- func (m *RedisMap) Clear() error
- func (m *RedisMap) Delete(keys ...string) error
- func (m *RedisMap) Exists(key string) bool
- func (m *RedisMap) Get(key string) (interface{}, bool)
- func (m *RedisMap) IsEmpty() bool
- func (m *RedisMap) IsNotEmpty() bool
- func (m *RedisMap) Iterator(batchSize int) ...
- func (m *RedisMap) Keys() ([]string, error)
- func (m *RedisMap) Length() int
- func (m *RedisMap) Set(key string, value interface{}) error
- func (m *RedisMap) ToArray() (map[string]interface{}, error)
- type RedisNumberMap
- func (nm *RedisNumberMap) Clear() error
- func (nm *RedisNumberMap) Decrement(key string, delta float64) (float64, error)
- func (nm *RedisNumberMap) Delete(keys ...string) error
- func (nm *RedisNumberMap) Exists(key string) bool
- func (nm *RedisNumberMap) Get(key string) (float64, bool)
- func (nm *RedisNumberMap) Increment(key string, delta float64) (float64, error)
- func (nm *RedisNumberMap) IsEmpty() bool
- func (nm *RedisNumberMap) IsNotEmpty() bool
- func (nm *RedisNumberMap) Keys() ([]string, error)
- func (nm *RedisNumberMap) Length() int
- func (nm *RedisNumberMap) Set(key string, value float64) error
- func (nm *RedisNumberMap) ToArray() (map[string]float64, error)
- type RedisSet
- func (s *RedisSet) Add(values ...interface{}) error
- func (s *RedisSet) Clear() error
- func (s *RedisSet) Exists(value interface{}) bool
- func (s *RedisSet) IsEmpty() bool
- func (s *RedisSet) IsNotEmpty() bool
- func (s *RedisSet) Iterator(batchSize int) <-chan interface{}
- func (s *RedisSet) Length() int
- func (s *RedisSet) Remove(values ...interface{}) error
- func (s *RedisSet) ToArray() ([]interface{}, error)
- type RedisType
- type RedisTypeList
- func (tl *RedisTypeList[T]) Clear() error
- func (tl *RedisTypeList[T]) DeleteIndex(index int) error
- func (tl *RedisTypeList[T]) DeleteRange(start, end int) error
- func (tl *RedisTypeList[T]) DeleteValue(value T, count int) error
- func (tl *RedisTypeList[T]) Exists() bool
- func (tl *RedisTypeList[T]) Get(start, end int) ([]T, error)
- func (tl *RedisTypeList[T]) Index(index int) (T, bool)
- func (tl *RedisTypeList[T]) IsEmpty() bool
- func (tl *RedisTypeList[T]) IsNotEmpty() bool
- func (tl *RedisTypeList[T]) Iterator(batchSize int) <-chan T
- func (tl *RedisTypeList[T]) Length() int
- func (tl *RedisTypeList[T]) Pop() (T, bool)
- func (tl *RedisTypeList[T]) Push(value T) error
- func (tl *RedisTypeList[T]) SafeUpset(index int, value T) (T, bool, error)
- func (tl *RedisTypeList[T]) Set(index int, value T) error
- func (tl *RedisTypeList[T]) Shift() (T, bool)
- func (tl *RedisTypeList[T]) Unshift(value T) error
- type RedisTypeMap
- func (tm *RedisTypeMap[T]) Clear() error
- func (tm *RedisTypeMap[T]) Delete(keys ...string) error
- func (tm *RedisTypeMap[T]) Exists(key string) bool
- func (tm *RedisTypeMap[T]) Get(key string) (T, bool)
- func (tm *RedisTypeMap[T]) IsEmpty() bool
- func (tm *RedisTypeMap[T]) IsNotEmpty() bool
- func (tm *RedisTypeMap[T]) Iterator(batchSize int) ...
- func (tm *RedisTypeMap[T]) Keys() ([]string, error)
- func (tm *RedisTypeMap[T]) Length() int
- func (tm *RedisTypeMap[T]) SafeUpset(key string, value T) (T, bool, error)
- func (tm *RedisTypeMap[T]) Set(key string, value T) error
- func (tm *RedisTypeMap[T]) ToArray() (map[string]T, error)
- type RedisTypeSet
- func (ts *RedisTypeSet[T]) Add(values ...T) error
- func (ts *RedisTypeSet[T]) Clear() error
- func (ts *RedisTypeSet[T]) Exists(value T) bool
- func (ts *RedisTypeSet[T]) IsEmpty() bool
- func (ts *RedisTypeSet[T]) IsNotEmpty() bool
- func (ts *RedisTypeSet[T]) Iterator(batchSize int) <-chan T
- func (ts *RedisTypeSet[T]) Length() int
- func (ts *RedisTypeSet[T]) Remove(values ...T) error
- func (ts *RedisTypeSet[T]) ToArray() ([]T, error)
- type RedisTypeZSet
- func (tz *RedisTypeZSet[T]) Add(value T, score float64) error
- func (tz *RedisTypeZSet[T]) Clear() error
- func (tz *RedisTypeZSet[T]) IncrementScore(value T, delta float64) (float64, error)
- func (tz *RedisTypeZSet[T]) IsEmpty() bool
- func (tz *RedisTypeZSet[T]) IsNotEmpty() bool
- func (tz *RedisTypeZSet[T]) Iterator(batchSize int) <-chan ZSetItem[T]
- func (tz *RedisTypeZSet[T]) IteratorFilterByScore(min, max float64) <-chan ZSetItem[T]
- func (tz *RedisTypeZSet[T]) Length() int
- func (tz *RedisTypeZSet[T]) RangeByRank(start, stop int) ([]T, error)
- func (tz *RedisTypeZSet[T]) RangeByScore(min, max float64) ([]T, error)
- func (tz *RedisTypeZSet[T]) Remove(values ...T) error
- func (tz *RedisTypeZSet[T]) RemoveRangeByScore(min, max float64) error
- func (tz *RedisTypeZSet[T]) Score(value T) (v float64, exist bool)
- type RedisZSet
- func (z *RedisZSet) Add(value interface{}, score float64) error
- func (z *RedisZSet) Clear() error
- func (z *RedisZSet) IncrementScore(value interface{}, delta float64) (float64, error)
- func (z *RedisZSet) IsEmpty() bool
- func (z *RedisZSet) IsNotEmpty() bool
- func (z *RedisZSet) Iterator(batchSize int) ...
- func (z *RedisZSet) Length() int
- func (z *RedisZSet) RangeByRank(start, stop int) ([]interface{}, error)
- func (z *RedisZSet) RangeByScore(min, max float64, withScores bool) ([]interface{}, error)
- func (z *RedisZSet) Remove(values ...interface{}) error
- func (z *RedisZSet) RemoveRangeByScore(min, max float64) error
- func (z *RedisZSet) Score(value interface{}) (float64, bool)
- type Serializer
- type SerializerFunc
- type TestRedis
- type ZSetItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcrossTime ¶
AcrossTime 是否跨越了指定的时间间隔(全局函数)
func DefaultNameCreator ¶
DefaultNameCreator 默认名称创建器
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache 缓存
func NewCache ¶
func NewCache[T any](name string, config CacheConfig, r ...*Redis) *Cache[T]
NewCache 创建缓存(全局函数)
type CacheConfig ¶
CacheConfig 缓存配置
type Config ¶
type Config struct {
Prefix string // 项目前缀
NameCreator func(config Config, types RedisType, name ...string) string // 名称创建器
MaxIdle int // 最大空闲连接数
MaxActive int // 最大连接数
IdleTimeout time.Duration // 空闲连接超时时间
MaxLifeTime time.Duration // 活跃连接超时时间
Serializer SerializerFunc // 序列化器
SafeTypeMapName string // 安全类型映射名称
}
Config Redis 配置
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock 分布式锁
func (*Lock) StartRefreshLoop ¶
func (l *Lock) StartRefreshLoop() chan struct{}
StartRefreshLoop 启动自动刷新锁的循环
type LockConfig ¶
type LockConfig struct {
WaitTime time.Duration // 最长的锁时间,获得锁后,如果在这个时间内没有释放锁,视为出错,自动释放锁
RetryTime time.Duration // 尝试获取锁的间隔时间
MaxGetLockWaitTime time.Duration // 获取锁最长等待时间
}
LockConfig 锁配置
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue 队列
func NewQueue ¶
func NewQueue[T any](name string, config QueueConfig, r ...*Redis) *Queue[T]
NewQueue 创建队列(全局函数)
func (*Queue[T]) AddDelayed ¶
AddDelayed 添加延迟任务
func (*Queue[T]) ProcessingLength ¶
ProcessingLength 获取处理中队列长度
func (*Queue[T]) StartWorker ¶
StartWorker 启动工作线程
func (*Queue[T]) StartWorkers ¶
StartWorkers 启动多个工作线程
type QueueConfig ¶
type QueueConfig struct {
MaxLength int // 队列最大长度,0 表示不限制
MaxWaitTime time.Duration // 队列阻塞等待时间,0 表示不阻塞
MaxRetry int // 队列重试次数,0 表示不重试
ErrorHandler func(value interface{}, err error, storage func(value interface{})) time.Duration // 错误处理器
}
QueueConfig 队列配置
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis Redis 客户端
func (*Redis) AcrossTime ¶
AcrossTime 是否跨越了指定的时间间隔
func (*Redis) CleanSafeTypeMap ¶
CleanSafeTypeMap 清理安全类型映射中的过期数据
func (*Redis) CreateName ¶
CreateName 创建 Redis 键名
func (*Redis) DeleteLastUseTime ¶
DeleteLastUseTime 删除上次使用时间
func (*Redis) Deserialize ¶
Deserialize 反序列化
func (*Redis) DoWithConn ¶
func (r *Redis) DoWithConn(conn redis.Conn, commandName string, args ...interface{}) (interface{}, error)
DoWithConn 使用指定连接执行 Redis 命令
func (*Redis) GetConnWithContext ¶
GetConnWithContext 获取带上下文的连接
func (*Redis) GetSafeTypeMap ¶
func (r *Redis) GetSafeTypeMap() *RedisTypeMap[int64]
GetSafeTypeMap 获取安全类型映射
func (*Redis) LastUseTime ¶
LastUseTime 获取上次使用时间
func (*Redis) NewLock ¶
func (r *Redis) NewLock(name string, config ...LockConfig) *Lock
NewLock 创建分布式锁
func (*Redis) NewNumberMap ¶
func (r *Redis) NewNumberMap(name string) *RedisNumberMap
NewNumberMap 创建数字型哈希表
type RedisBuilder ¶
type RedisBuilder struct {
// contains filtered or unexported fields
}
Builder 构建器
func (*RedisBuilder) Config ¶
func (b *RedisBuilder) Config(config Config) *RedisBuilder
Config 设置配置
type RedisList ¶
type RedisList struct {
// contains filtered or unexported fields
}
RedisList Redis 列表
func (*RedisList) DeleteIndex ¶
DeleteIndex 删除指定索引的元素(通过设置标记并移除)
func (*RedisList) DeleteRange ¶
DeleteRange 删除指定范围外的元素
func (*RedisList) DeleteValue ¶
DeleteValue 删除指定值的元素
type RedisMap ¶
type RedisMap struct {
// contains filtered or unexported fields
}
RedisMap Redis 哈希表
type RedisNumberMap ¶
type RedisNumberMap struct {
// contains filtered or unexported fields
}
RedisNumberMap 数字型 Redis 哈希表
func (*RedisNumberMap) Decrement ¶
func (nm *RedisNumberMap) Decrement(key string, delta float64) (float64, error)
Decrement 减少数值
func (*RedisNumberMap) Increment ¶
func (nm *RedisNumberMap) Increment(key string, delta float64) (float64, error)
Increment 增加数值
func (*RedisNumberMap) IsNotEmpty ¶
func (nm *RedisNumberMap) IsNotEmpty() bool
IsNotEmpty 判断哈希表是否不为空
type RedisSet ¶
type RedisSet struct {
// contains filtered or unexported fields
}
RedisSet Redis 集合
type RedisTypeList ¶
type RedisTypeList[T any] struct { // contains filtered or unexported fields }
RedisTypeList 类型化的 Redis 列表
func NewTypeList ¶
func NewTypeList[T any](name string, r ...*Redis) *RedisTypeList[T]
NewTypeList 创建类型化列表(全局函数)
func (*RedisTypeList[T]) DeleteIndex ¶
func (tl *RedisTypeList[T]) DeleteIndex(index int) error
DeleteIndex 删除指定索引的元素
func (*RedisTypeList[T]) DeleteRange ¶
func (tl *RedisTypeList[T]) DeleteRange(start, end int) error
DeleteRange 删除指定范围外的元素
func (*RedisTypeList[T]) DeleteValue ¶
func (tl *RedisTypeList[T]) DeleteValue(value T, count int) error
DeleteValue 删除指定值的元素
func (*RedisTypeList[T]) Get ¶
func (tl *RedisTypeList[T]) Get(start, end int) ([]T, error)
Get 获取指定范围的元素
func (*RedisTypeList[T]) Index ¶
func (tl *RedisTypeList[T]) Index(index int) (T, bool)
Index 获取指定索引的元素
func (*RedisTypeList[T]) IsNotEmpty ¶
func (tl *RedisTypeList[T]) IsNotEmpty() bool
IsNotEmpty 判断列表是否不为空
func (*RedisTypeList[T]) Iterator ¶
func (tl *RedisTypeList[T]) Iterator(batchSize int) <-chan T
Iterator 获取迭代器
func (*RedisTypeList[T]) SafeUpset ¶
func (tl *RedisTypeList[T]) SafeUpset(index int, value T) (T, bool, error)
SafeUpset 安全更新
func (*RedisTypeList[T]) Set ¶
func (tl *RedisTypeList[T]) Set(index int, value T) error
Set 设置指定索引的元素
func (*RedisTypeList[T]) Unshift ¶
func (tl *RedisTypeList[T]) Unshift(value T) error
Unshift 从左侧推入元素
type RedisTypeMap ¶
type RedisTypeMap[T any] struct { // contains filtered or unexported fields }
RedisTypeMap 类型化的 Redis 哈希表
func NewTypeMap ¶
func NewTypeMap[T any](name string, r ...*Redis) *RedisTypeMap[T]
NewTypeMap 创建类型化哈希表(全局函数)
func (*RedisTypeMap[T]) IsNotEmpty ¶
func (tm *RedisTypeMap[T]) IsNotEmpty() bool
IsNotEmpty 判断哈希表是否不为空
func (*RedisTypeMap[T]) Iterator ¶
func (tm *RedisTypeMap[T]) Iterator(batchSize int) <-chan struct { Key string Value T }
Iterator 获取迭代器
func (*RedisTypeMap[T]) SafeUpset ¶
func (tm *RedisTypeMap[T]) SafeUpset(key string, value T) (T, bool, error)
SafeUpset 安全更新(多实例安全)
func (*RedisTypeMap[T]) ToArray ¶
func (tm *RedisTypeMap[T]) ToArray() (map[string]T, error)
ToArray 获取所有键值对
type RedisTypeSet ¶
type RedisTypeSet[T any] struct { // contains filtered or unexported fields }
RedisTypeSet 类型化的 Redis 集合
func NewTypeSet ¶
func NewTypeSet[T any](name string, r ...*Redis) *RedisTypeSet[T]
NewTypeSet 创建类型化集合(全局函数)
func (*RedisTypeSet[T]) IsNotEmpty ¶
func (ts *RedisTypeSet[T]) IsNotEmpty() bool
IsNotEmpty 判断集合是否不为空
func (*RedisTypeSet[T]) Iterator ¶
func (ts *RedisTypeSet[T]) Iterator(batchSize int) <-chan T
Iterator 获取迭代器
type RedisTypeZSet ¶
type RedisTypeZSet[T any] struct { // contains filtered or unexported fields }
RedisTypeZSet 类型化的 Redis 有序集合
func NewTypeZSet ¶
func NewTypeZSet[T any](name string, r ...*Redis) *RedisTypeZSet[T]
NewTypeZSet 创建类型化有序集合(全局函数)
func (*RedisTypeZSet[T]) Add ¶
func (tz *RedisTypeZSet[T]) Add(value T, score float64) error
Add 添加元素
func (*RedisTypeZSet[T]) IncrementScore ¶
func (tz *RedisTypeZSet[T]) IncrementScore(value T, delta float64) (float64, error)
IncrementScore 增加元素的分数
func (*RedisTypeZSet[T]) IsNotEmpty ¶
func (tz *RedisTypeZSet[T]) IsNotEmpty() bool
IsNotEmpty 判断有序集合是否不为空
func (*RedisTypeZSet[T]) Iterator ¶
func (tz *RedisTypeZSet[T]) Iterator(batchSize int) <-chan ZSetItem[T]
Iterator 获取迭代器
func (*RedisTypeZSet[T]) IteratorFilterByScore ¶
func (tz *RedisTypeZSet[T]) IteratorFilterByScore(min, max float64) <-chan ZSetItem[T]
IteratorFilterByScore 按分数过滤的迭代器
func (*RedisTypeZSet[T]) RangeByRank ¶
func (tz *RedisTypeZSet[T]) RangeByRank(start, stop int) ([]T, error)
RangeByRank 按排名范围获取元素
func (*RedisTypeZSet[T]) RangeByScore ¶
func (tz *RedisTypeZSet[T]) RangeByScore(min, max float64) ([]T, error)
RangeByScore 按分数范围获取元素
func (*RedisTypeZSet[T]) RemoveRangeByScore ¶
func (tz *RedisTypeZSet[T]) RemoveRangeByScore(min, max float64) error
RemoveRangeByScore 按分数范围删除元素
func (*RedisTypeZSet[T]) Score ¶
func (tz *RedisTypeZSet[T]) Score(value T) (v float64, exist bool)
Score 获取元素的分数
type RedisZSet ¶
type RedisZSet struct {
// contains filtered or unexported fields
}
RedisZSet Redis 有序集合
func (*RedisZSet) IncrementScore ¶
IncrementScore 增加元素的分数
func (*RedisZSet) RangeByRank ¶
RangeByRank 按排名范围获取元素
func (*RedisZSet) RangeByScore ¶
RangeByScore 按分数范围获取元素
func (*RedisZSet) RemoveRangeByScore ¶
RemoveRangeByScore 按分数范围删除元素
type Serializer ¶
Serializer 序列化接口
type SerializerFunc ¶
type SerializerFunc interface {
Serialize(v interface{}) ([]byte, error)
Deserialize(data []byte, v interface{}) error
}
SerializerFunc 序列化函数接口
var DefaultSerializer SerializerFunc = &defaultSerializer{}
DefaultSerializer 默认序列化器实例
type TestRedis ¶
type TestRedis struct {
Redis *Redis
MiniRedis *miniredis.Miniredis
// contains filtered or unexported fields
}
TestRedis 测试用的 Redis 实例
func NewTestRedis ¶
NewTestRedis 创建测试用的 Redis 实例 通过环境变量 USE_REAL_REDIS=1 可以使用真实 Redis (localhost:16379) 通过环境变量 REDIS_ADDR 可以自定义 Redis 地址
func (*TestRedis) FastForward ¶
FastForward 快进时间(仅在 miniredis 模式下有效)