cache

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DelKeyScript = `` /* 145-byte string literal not displayed */

	RenewExpireScript = `` /* 242-byte string literal not displayed */

)
View Source
const (
	KeyNoExists int64 = -2
	NoOperate   int64 = 0
	ReNewExp    int64 = 1
)

Variables

View Source
var AcquireLockFailed = errors.New("acquire distribute lock failed")

Functions

This section is empty.

Types

type Cache added in v1.3.2

type Cache struct {
	Network          string `yaml:"network" json:"network"`                   // 网络类型, tcp or unix,默认tcp
	Addr             string `yaml:"addr" json:"addr"`                         // Redis地址 eg: localhost:6379
	User             string `yaml:"user" json:"user"`                         // 用户, redis6.0开始支持
	Pwd              string `yaml:"pwd" json:"pwd"`                           // 密码
	MinFreeConnCount int    `yaml:"minFreeConnCount" json:"minFreeConnCount"` // 最小闲置连接数, 默认是3
	MaxOpenConnCount int    `yaml:"maxOpenConnCount" json:"maxOpenConnCount"` // 最大连接数, 默认是10
	FreeMaxLifetime  int    `yaml:"freeMaxLifetime" json:"freeMaxLifetime"`   // 闲置连接存活的最大时间, -1表示无限制, 默认是10分钟, 单位: 秒
	ConnMaxLifetime  int    `yaml:"connMaxLifetime" json:"connMaxLifetime"`   // 连接存活最大时长, 默认是1小时, 单位: 秒
	Database         int    `yaml:"database" json:"database"`                 // 数据库
	ConnectTimeout   int    `yaml:"connectTimeout" json:"connectTimeout"`     // 建立连接超时时间, 默认是5秒, 单位: 秒
	ReadTimeout      int    `yaml:"readTimeout" json:"readTimeout"`           // 读取超时时间, -1表示无超时, 默认是3秒, 单位: 秒
	WriteTimeout     int    `yaml:"writeTimeout" json:"writeTimeout"`         // 写超时时间, 默认和ReadTimeout保持一致, 单位: 秒
	BusyWaitTimeOut  int    `yaml:"busyWaitTimeOut" json:"busyWaitTimeOut"`   // 当所有连接都处在繁忙状态时, 客户端等待可用连接的最大等待时长,默认为3秒
	MaxRetries       int    `yaml:"maxRetries" json:"maxRetries"`             // 最大尝试次数, -1表示不重试, 默认是3
	PeriodMillSec    int    `yaml:"periodSeconds" json:"periodSeconds"`       // 分布式锁, 多久续一次分布式锁时长, 单位: ms, 默认是3s
	AddMillSec       int    `yaml:"addSeconds" json:"addSeconds"`             // 分布式锁每次添加多长时间, 单位: ms, 默认是: 5s
}

Cache 缓存配置.

type DistributeKeyParam added in v1.4.1

type DistributeKeyParam struct {
	Key         string        // 分布式Key
	AcquireTime time.Duration // 获取锁超时时间
	LockTime    time.Duration // 锁的超时时间
	Period      time.Duration // watchDog多久执行一次
	Fn          func() error  // 执行函数
}

type RedisClient

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

func NewRedisClient

func NewRedisClient(client *redis.Client, rawConfig *Cache) *RedisClient

func SetUp added in v1.1.4

func SetUp(conf Cache) (client *RedisClient, err error)

SetUp 初始化缓存.

func (*RedisClient) AsyncFlushDB

func (r *RedisClient) AsyncFlushDB() error

AsyncFlushDB 异步清空DB.

func (*RedisClient) Close

func (r *RedisClient) Close() error

Close 关闭连接池.

func (*RedisClient) Decr

func (r *RedisClient) Decr(key string) (int64, error)

Decr 减1.

func (*RedisClient) DecrBy

func (r *RedisClient) DecrBy(key string, decr int64) (int64, error)

DecrBy 减少指定数量.

func (*RedisClient) Delete

func (r *RedisClient) Delete(keys ...string) error

Delete 删除指定key.

func (*RedisClient) DoWithDistributeLock added in v1.4.1

func (r *RedisClient) DoWithDistributeLock(req *DistributeKeyParam) error

DoWithDistributeLock 使用分布式锁执行任务.

func (*RedisClient) Exist

func (r *RedisClient) Exist(key string) (bool, error)

Exist 确认key是否存在.

func (*RedisClient) Expire added in v1.4.1

func (r *RedisClient) Expire(key string, timeoutSeconds int) error

Expire 设置过期时间.

func (*RedisClient) ExpireAt added in v1.4.1

func (r *RedisClient) ExpireAt(key string, expireAt time.Time) error

ExpireAt 设置过期时间.

func (*RedisClient) FlushDB

func (r *RedisClient) FlushDB() error

FlushDB 清空DB.

func (*RedisClient) GetBool

func (r *RedisClient) GetBool(key string) (types.NullBool, error)

GetBool GetBool.

func (*RedisClient) GetClient added in v1.4.1

func (r *RedisClient) GetClient() *redis.Client

GetClient 获取原生client.

func (*RedisClient) GetFloat64

func (r *RedisClient) GetFloat64(key string) (types.NullFloat64, error)

GetFloat64 GetFloat64.

func (*RedisClient) GetInt

func (r *RedisClient) GetInt(key string) (types.NullInt, error)

GetInt GetInt.

func (*RedisClient) GetInt64

func (r *RedisClient) GetInt64(key string) (types.NullInt64, error)

GetInt64 GetInt64.

func (*RedisClient) GetJson

func (r *RedisClient) GetJson(key string, dst interface{}) (bool, error)

GetJson GetJson.

func (*RedisClient) GetStr

func (r *RedisClient) GetStr(key string) (types.NullString, error)

GetStr GetStr.

func (*RedisClient) GetTime

func (r *RedisClient) GetTime(key string) (types.NullTime, error)

GetTime GetTime.

func (*RedisClient) HDel added in v1.4.2

func (r *RedisClient) HDel(key string, fields ...string) (int64, error)

HDel Redis命令hdel.

func (*RedisClient) HExists added in v1.4.2

func (r *RedisClient) HExists(key string, field string) (bool, error)

HExists Redis命令hexists.

func (*RedisClient) HGetAll

func (r *RedisClient) HGetAll(key string) (map[string]string, error)

HGetAll Redis命令hgetall.

func (*RedisClient) HGetAllWIthMap

func (r *RedisClient) HGetAllWIthMap(key string) (map[string]string, error)

HGetAllWIthMap Redis命令hgetall, 返回值第一个是key是否存在, 第二个是错误.

func (*RedisClient) HGetBool

func (r *RedisClient) HGetBool(key string, field string) (types.NullBool, error)

HGetBool Redis命令hget.

func (*RedisClient) HGetFloat64

func (r *RedisClient) HGetFloat64(key string, field string) (types.NullFloat64, error)

HGetFloat64 Redis命令hget.

func (*RedisClient) HGetInt

func (r *RedisClient) HGetInt(key string, field string) (types.NullInt, error)

HGetInt Redis命令hget.

func (*RedisClient) HGetInt64

func (r *RedisClient) HGetInt64(key string, field string) (types.NullInt64, error)

HGetInt64 Redis命令hget.

func (*RedisClient) HGetJson

func (r *RedisClient) HGetJson(key string, field string, dst interface{}) (bool, error)

HGetJson Redis命令hget.

func (*RedisClient) HGetStr

func (r *RedisClient) HGetStr(key string, field string) (types.NullString, error)

HGetStr Redis命令hget.

func (*RedisClient) HGetTime

func (r *RedisClient) HGetTime(key string, field string) (types.NullTime, error)

HGetTime Redis命令hget.

func (*RedisClient) HKeys

func (r *RedisClient) HKeys(key string) ([]string, error)

HKeys Redis命令hkeys.

func (*RedisClient) HLen

func (r *RedisClient) HLen(key string) (int64, error)

HLen Redis命令hlen.

func (*RedisClient) HSet

func (r *RedisClient) HSet(key string, info map[string]interface{}) error

HSet Redis命令Hset.

func (*RedisClient) HSetJson

func (r *RedisClient) HSetJson(key, field string, value interface{}) error

HSetJson Redis命令Hset.

func (*RedisClient) Heartbeat added in v1.1.5

func (r *RedisClient) Heartbeat() error

Heartbeat ping连接.

func (*RedisClient) Incr

func (r *RedisClient) Incr(key string) (int64, error)

Incr 加1.

func (*RedisClient) IncrBy

func (r *RedisClient) IncrBy(key string, incr int64) (int64, error)

IncrBy 添加指定数量.

func (*RedisClient) LAllMemberFloat64

func (r *RedisClient) LAllMemberFloat64(key string) ([]int64, error)

LAllMemberFloat64 获取列表全部内容.

func (*RedisClient) LAllMemberInt

func (r *RedisClient) LAllMemberInt(key string) ([]int, error)

LAllMemberInt 获取列表全部内容.

func (*RedisClient) LAllMemberInt64

func (r *RedisClient) LAllMemberInt64(key string) ([]int64, error)

LAllMemberInt64 获取列表全部内容.

func (*RedisClient) LAllMemberStr

func (r *RedisClient) LAllMemberStr(key string) ([]string, error)

LAllMemberStr 获取列表全部内容.

func (*RedisClient) LFirstMemberFloat64

func (r *RedisClient) LFirstMemberFloat64(key string) (types.NullFloat64, error)

func (*RedisClient) LFirstMemberInt

func (r *RedisClient) LFirstMemberInt(key string) (types.NullInt, error)

LFirstMemberInt 获取第一个元素, 如果值是nil, 代表列表为空或key不存在.

func (*RedisClient) LFirstMemberInt64

func (r *RedisClient) LFirstMemberInt64(key string) (types.NullInt64, error)

func (*RedisClient) LFirstMemberStr

func (r *RedisClient) LFirstMemberStr(key string) (types.NullString, error)

LFirstMemberStr 获取第一个元素, 如果值是nil, 代表列表为空或key不存在.

func (*RedisClient) LLen

func (r *RedisClient) LLen(key string) (int64, error)

LLen 列表长度.

func (*RedisClient) LMembersFloat64

func (r *RedisClient) LMembersFloat64(key string, start, end int) ([]float64, error)

LMembersFloat64 获取列表全部内容.

func (*RedisClient) LMembersInt

func (r *RedisClient) LMembersInt(key string, start, end int) ([]int, error)

LMembersInt 获取列表全部内容.

func (*RedisClient) LMembersInt64

func (r *RedisClient) LMembersInt64(key string, start, end int) ([]int64, error)

LMembersInt64 获取列表全部内容.

func (*RedisClient) LMembersStr

func (r *RedisClient) LMembersStr(key string, start, end int) ([]string, error)

LMembersStr 获取列表全部内容.

func (*RedisClient) LPop

func (r *RedisClient) LPop(key string) (types.NullString, error)

LPop 从头部删除元素.

func (*RedisClient) LPopWithCount

func (r *RedisClient) LPopWithCount(key string, count int) ([]string, error)

LPopWithCount 从头部删除元素.

func (*RedisClient) LPush

func (r *RedisClient) LPush(key string, values ...interface{}) error

LPush 往列表插入值.

func (*RedisClient) LRem

func (r *RedisClient) LRem(key string, value interface{}) error

LRem 删除列表中所有与value相等的元素.

func (*RedisClient) LRemFirstOne

func (r *RedisClient) LRemFirstOne(key string, value interface{}) error

LRemFirstOne 从头部开始删除第一个value相等的元素.

func (*RedisClient) LRemLastOne

func (r *RedisClient) LRemLastOne(key string, value interface{}) error

LRemLastOne 从尾部开始删除第一个value相等的元素.

func (*RedisClient) LRemWithCount

func (r *RedisClient) LRemWithCount(key string, value interface{}, count int) error

LRemWithCount 删除列表中与value相等的元素, 删除个数为count.

func (*RedisClient) LTrim

func (r *RedisClient) LTrim(key string, start, end int) error

LTrim 保留指定start, end 范围的元素, 包括边界元素, 其中start, end为列表下标.

func (*RedisClient) MSet

func (r *RedisClient) MSet(data map[string]interface{}) error

MSet 批量Set.

func (*RedisClient) Pipeline added in v1.4.1

func (r *RedisClient) Pipeline() redis.Pipeliner

Pipeline 获取管道.

func (*RedisClient) RPop

func (r *RedisClient) RPop(key string) (types.NullString, error)

RPop 从尾部删除元素.

func (*RedisClient) RPopLPush

func (r *RedisClient) RPopLPush(src, dst string) (string, error)

RPopLPush Redis命令rpoplpush.

func (*RedisClient) RPopWithCount

func (r *RedisClient) RPopWithCount(key string, count int) ([]string, error)

RPopWithCount 从头部删除元素.

func (*RedisClient) RPush

func (r *RedisClient) RPush(key string, values ...interface{}) error

RPush 往列表插入值.

func (*RedisClient) SAdd

func (r *RedisClient) SAdd(key string, values ...interface{}) error

SAdd Redis命令sadd.

func (*RedisClient) SIsMember

func (r *RedisClient) SIsMember(key string, value interface{}) (bool, error)

SIsMember Redis命令sismember.

func (*RedisClient) SMembers

func (r *RedisClient) SMembers(key string) ([]string, error)

SMembers Redis命令smembers.

func (*RedisClient) SMembersFloat64

func (r *RedisClient) SMembersFloat64(key string) ([]float64, error)

SMembersFloat64 Redis命令smembers.

func (*RedisClient) SMembersInt

func (r *RedisClient) SMembersInt(key string) ([]int, error)

SMembersInt Redis命令smembers.

func (*RedisClient) SMembersInt64

func (r *RedisClient) SMembersInt64(key string) ([]int64, error)

SMembersInt64 Redis命令smembers.

func (*RedisClient) SMembersTime

func (r *RedisClient) SMembersTime(key string) ([]time.Time, error)

SMembersTime Redis命令smembers.

func (*RedisClient) SPopBool

func (r *RedisClient) SPopBool(key string) (types.NullBool, error)

SPopBool Redis命令spop.

func (*RedisClient) SPopFloat64

func (r *RedisClient) SPopFloat64(key string) (float64, error)

SPopFloat64 Redis命令spop.

func (*RedisClient) SPopInt

func (r *RedisClient) SPopInt(key string) (types.NullInt, error)

SPopInt Redis命令spop.

func (*RedisClient) SPopInt64

func (r *RedisClient) SPopInt64(key string) (types.NullInt64, error)

SPopInt64 Redis命令spop.

func (*RedisClient) SPopStr

func (r *RedisClient) SPopStr(key string) (types.NullString, error)

SPopStr Redis命令spop, 返回删除的值.

func (*RedisClient) SRem

func (r *RedisClient) SRem(key string, dst ...interface{}) (int64, error)

SRem Redis命令srem, 返回删除个数.

func (*RedisClient) Scan added in v1.4.1

func (r *RedisClient) Scan(cursor uint64, match string, count int64) ([]string, uint64, error)

Scan 扫描.

func (*RedisClient) ScanPattern added in v1.4.1

func (r *RedisClient) ScanPattern(match string, count int64, ch chan<- string, sleepSeconds int)

ScanPattern 扫描key, 扫描整张表.

func (*RedisClient) Set

func (r *RedisClient) Set(key string, value interface{}, timeoutSeconds int) (err error)

Set redis命令SET, timeoutSeconds <=0, 则认为无过期时间.

func (*RedisClient) SetArgs added in v1.4.1

func (r *RedisClient) SetArgs(key string, value interface{}, args redis.SetArgs) (string, error)

SetArgs 设置 如果设置了NX, 并且key已存在, 返回redis.Nil错误 成功设置返回OK.

func (*RedisClient) SetJson

func (r *RedisClient) SetJson(key string, value interface{}, timeoutSeconds int) (err error)

SetJson 设置Json.

func (*RedisClient) SetNX

func (r *RedisClient) SetNX(key string, value interface{}, timeoutSeconds int) (bool, error)

SetNX 存在不操作, 不存在则设置.

func (*RedisClient) SetWithNoExpire

func (r *RedisClient) SetWithNoExpire(key string, value interface{}) (err error)

SetWithNoExpire redis命令SET, 永久存储.

func (*RedisClient) TTL

func (r *RedisClient) TTL(key string) (types.NullFloat64, error)

TTL 获取指定key的过期时间, 返回时间, 单位: s 如果结果是nil, 则key不存在 如果结果值-1, 则key无过期时间 否则值就是key的过期时间.

func (*RedisClient) WatchHeartbeat added in v1.1.4

func (r *RedisClient) WatchHeartbeat()

WatchHeartbeat 监测心跳和重连.

func (*RedisClient) ZAdd added in v1.3.6

func (r *RedisClient) ZAdd(key string, members ...*redis.Z) error

ZAdd Redis命令 zadd.

func (*RedisClient) ZCount

func (r *RedisClient) ZCount(key string, scoreMin, scoreMax string) (int64, error)

ZCount Redis命令zcount, 返回score 值在 min 和 max 之间的成员的数量.

func (*RedisClient) ZCountWithFloat64 added in v1.3.8

func (r *RedisClient) ZCountWithFloat64(key string, scoreMin, scoreMax float64) (int64, error)

func (*RedisClient) ZCountWithInt added in v1.3.8

func (r *RedisClient) ZCountWithInt(key string, scoreMin, scoreMax int) (int64, error)

func (*RedisClient) ZCountWithInt32 added in v1.3.8

func (r *RedisClient) ZCountWithInt32(key string, scoreMin, scoreMax int32) (int64, error)

func (*RedisClient) ZCountWithInt64 added in v1.3.8

func (r *RedisClient) ZCountWithInt64(key string, scoreMin, scoreMax int64) (int64, error)

func (*RedisClient) ZRange added in v1.3.6

func (r *RedisClient) ZRange(key string, start, end int64) ([]string, error)

ZRange Redis命令zrange, 包括start, end 边界值, 返回按照score升序.

func (*RedisClient) ZRangeByScore added in v1.3.6

func (r *RedisClient) ZRangeByScore(key string, scoreMin, scoreMax string, reverse bool) ([]redis.Z, error)

ZRangeByScore Redis命令zrange, 包括start, end 边界值, 返回按照score排序.

func (*RedisClient) ZRangeWithCount added in v1.3.8

func (r *RedisClient) ZRangeWithCount(key string, count int64) ([]string, error)

ZRangeWithCount 返回按照score升序.

func (*RedisClient) ZRank added in v1.3.8

func (r *RedisClient) ZRank(key string, member string) (int64, error)

ZRank Redis命令zrank, 返回按照score升序, 返回排名.

func (*RedisClient) ZRem

func (r *RedisClient) ZRem(key string, members ...interface{}) (int64, error)

ZRem Redis命令zrem, 删除指定member的field.

func (*RedisClient) ZRemRangeByRank added in v1.3.6

func (r *RedisClient) ZRemRangeByRank(key string, start, end int64) (uint64, error)

ZRemRangeByRank Redis命令ZREMRANGEBYRANK 根据排名删除 按照score升序.

func (*RedisClient) ZRemRangeByRankCount added in v1.3.8

func (r *RedisClient) ZRemRangeByRankCount(key string, count int64) (uint64, error)

ZRemRangeByRankCount 根据排名升序删除 排名按照score升序.

func (*RedisClient) ZRemRangeByScore added in v1.3.6

func (r *RedisClient) ZRemRangeByScore(key string, min, max string) (uint64, error)

ZRemRangeByScore Redis命令ZREMRANGEBYSCORE.

func (*RedisClient) ZRevRange added in v1.3.8

func (r *RedisClient) ZRevRange(key string, start, end int64) ([]string, error)

ZRevRange Redis命令zrevrange, 包括start, end 边界值, 返回按照score降序.

func (*RedisClient) ZRevRangeWithCount added in v1.3.8

func (r *RedisClient) ZRevRangeWithCount(key string, count int64) ([]string, error)

ZRevRangeWithCount Redis命令zrevrange, 返回按照score降序.

func (*RedisClient) ZRevRank added in v1.3.8

func (r *RedisClient) ZRevRank(key string, member string) (int64, error)

ZRevRank Redis命令zrevrank, 按照score降序, 返回排名.

func (*RedisClient) ZScore

func (r *RedisClient) ZScore(key string, value string) (types.NullFloat64, error)

ZScore Redis命令zscore.

Jump to

Keyboard shortcuts

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