rdb

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	C_CONN_IDLE_TIMEOVER = 5 * time.Minute // 空闲多久回收连接

	C_INF_MAX = "+inf" // 正无穷
	C_INF_MIN = "-inf" // 负无穷

	C_DB_CLUSTER = "_cluster_" // 如果使用集群部署,Config只能有一个且name='_cluster_'

	C_TICK_OUTPUT_STAT_INTERVAL = 1 * time.Minute // 一分钟统计输出一次命令调用情况
)

==================== 常量定义

Variables

View Source
var (
	ErrNil                      = redis.ErrNil
	ErrNameRepeated             = errors.New("ErrConfigNameRepeated")
	ErrInvalidTTL               = errors.New("ErrInvalidTTL")
	ErrZsetAddArrInvalid        = errors.New("ErrZsetAddArrInvalid")
	ErrHashGetMapResultMismatch = errors.New("ErrHashGetMapResultMismatch")
	ErrInvalidCoding            = errors.New("ErrInvalidCoding")
)

==================== 错误定义

Functions

func Command

func Command(dbName string, coding ECoding, command string, args ...interface{}) *reply

Command 直接执行命令

func Connector

func Connector(dbName string) redis.Conn

Connector 获取指定pool的连接 !!!注意释放!!!

func Decode

func Decode(encoding ECoding, b []byte, v interface{}) error

func Encode

func Encode(encoding ECoding, v interface{}) (interface{}, error)

func Exec

func Exec(sends ...sender) *reply

Exec 事务原子操作,统一返回结果 ( sender.Reply 赋值不起作用)

func Install

func Install(confs []*Config) ctl.IControler

安装控制器

func Pipe

func Pipe(sends ...sender) error

Pipe Pipelining管道批量并发操作,统一返回结果 (非原子)

func RegistCoder

func RegistCoder(name ECoding, coder Coder)

RegistCoder 注册编解码器

func Reply

func Reply(rep interface{}, err error, coding ECoding, fullCmd string) *reply

func Sender

func Sender(k IKeyer, send func(), rep ...func(rps []*Resp)) sender

> Sender

Types

type Bit

type Bit struct{ Key }

func (*Bit) BitCount

func (k *Bit) BitCount(beginEnd ...int64) *reply

BitCount 被设置为 1 的位的数量

func (*Bit) BitOpt

func (k *Bit) BitOpt(opt, destKey string, keys ...string) *reply

opt 包含 and、or、xor、not

func (*Bit) GetBit

func (k *Bit) GetBit(offset int64) *reply

GetBit 特定位取值 0 or 1

func (*Bit) SetBit

func (k *Bit) SetBit(offset, val_0_or_1 int64) *reply

SetBit 特定位设置 0 or 1

type ClusterPool

type ClusterPool struct{ redisc.Cluster } //

func (*ClusterPool) Mode

func (*ClusterPool) Mode() EMode

type Coder

type Coder struct {
	Encoder func(v interface{}) ([]byte, error)
	Decoder func(data []byte, v interface{}) error
}

> 编码器接口

type Config

type Config struct {
	Name      string `json:"name"` // 常量 C_DB_CLUSTER 是唯一的
	Addr      string `json:"addr"`
	DbIdx     int64  `json:"index"` // 0-15
	Passwd    string `json:"passwd"`
	MaxIdle   int64  `json:"max_idle"`
	MaxActive int64  `json:"max_active"`
	Wait      bool   `json:"wait"` // 如果当前的连接已经达到MaxActive值的话, 如果true=>线程将会等待, 否则直接报错.
}

> Redis配置

type ECoding

type ECoding = string // 编码类型
const (
	ECod_None      ECoding = ""
	ECod_Json      ECoding = "json"
	ECod_Msgpack   ECoding = "msgpack"
	ECod_ProtoBuff ECoding = "pb"
)

type EMode

type EMode int //

> 连接模式

const (
	EMode_Normal  EMode = iota + 1 // 单库
	EMode_Cluster                  // 集群
)

type ESetMode

type ESetMode int //

> 值的设置模式

const (
	ESet_Update      ESetMode // 正常设置
	ESet_WhenExist            // 当值存在时才进行设置
	ESet_WhenNoExist          // 当值不存在时才进行设置
	ESet_WhenValueLT          // 当新的分值比当前分值小才设置,不存在则新增
	ESet_WhenValueGT          // 当新的分值比当前分值大才设置,不存在则新增
)

type ESort

type ESort int //

> 排序类型

const (
	ESort_Asc  ESort = iota + 1 // 正序 从小到大
	ESort_Desc                  // 倒序 从大到小
)

type Hash

type Hash struct{ Key }

func (*Hash) Del

func (k *Hash) Del(fields ...interface{}) *reply

Hash.Del 字段删除()

func (*Hash) Exists

func (k *Hash) Exists(field interface{}) *reply

Hash.Exists 判断字段是否存在

func (*Hash) Fields

func (k *Hash) Fields() *reply

Hash.Fields 返回所有字段

func (*Hash) Get

func (k *Hash) Get(field interface{}) *reply

Hash.Get 获取指定字段值

func (*Hash) GetAll

func (k *Hash) GetAll() *reply

Hash.GetAll 设置对象的字段及值(reply.XxxMap)

func (*Hash) GetMap

func (k *Hash) GetMap(fields ...interface{}) *reply

Hash.GetMap 返回多个字段以及值(reply.HashMap)

func (*Hash) GetStruct

func (k *Hash) GetStruct() *reply

Hash.GetStruct 获取对象的字段及值(reply.ScanStruct)

func (*Hash) Getm

func (k *Hash) Getm(fields ...interface{}) *reply

Hash.Getm 返回多个字段的值(reply.Xxxs)

func (*Hash) IncrBy

func (k *Hash) IncrBy(field interface{}, increment interface{}) *reply

Hash.IncrBy 为指定字段值增加

func (*Hash) IncrByFloat

func (k *Hash) IncrByFloat(field interface{}, increment float64) *reply

Hash.IncrByFloat 为指定字段值增加浮点数

func (*Hash) Len

func (k *Hash) Len() *reply

Hash.Len 返回字段数量

func (*Hash) Set

func (k *Hash) Set(field, value interface{}, noExist ...bool) *reply

Hash.Set 为字段设置其值(noExist为true 表示字段不存则设置其值)

func (*Hash) SetMap

func (k *Hash) SetMap(mp map[interface{}]interface{}) *reply

Hash.SetMap 设置多个字段及值(map[field]value)

func (*Hash) SetStruct

func (k *Hash) SetStruct(obj interface{}) *reply

Hash.SetStruct 设置对象的字段及值(*struct)

func (*Hash) Values

func (k *Hash) Values() *reply

Hash.Values 返回所有字段的值

type IContrler

type IContrler interface {
	ctl.IControler

	// 获取某个连接池对象
	Use(name string) IPooler
}

控制器接口

var (
	Ctl IContrler // 默认Cache控制器

)

func Controller

func Controller(confs []*Config) IContrler

type IKeyer

type IKeyer interface {
	// contains filtered or unexported methods
}

所有Key接口

type IMutexer

type IMutexer interface {
	Lock() error
	Unlock() (bool, error)
	Extend() (bool, error)
}

> 互斥锁接口

func Mutex

func Mutex(dbName, key string) IMutexer

type IPooler

type IPooler interface {
	Mode() EMode
	Get() redis.Conn
	Close() error
}

连接池接口

func Pool

func Pool(dbName string) IPooler

Pool 获取指定pool

type Key

type Key struct {
	DB     string        // 哪个db中(表示哪个db索引)
	K      string        // Key的名称(建议格式:'basexxx:param1=%d,param2=%s,....')
	Coding ECoding       // 编码模式
	Ttl    time.Duration // 存活时间ms(仅作存储,需要自行调用k.Overdue())
	// contains filtered or unexported fields
}

func (*Key) Clusted

func (k *Key) Clusted() bool

Clusted 是否是集群模式

func (*Key) Delete

func (k *Key) Delete(keys ...string) *reply

key.Delete 删除key(默认删自己,否则删入参指定的keys)

func (*Key) Dump

func (k *Key) Dump() *reply

key.RenameNX 序列化key

func (*Key) Existed

func (k *Key) Existed() *reply

key.Existed 判断key是否存在

func (*Key) Expire

func (k *Key) Expire(seconds int64) *reply

key.Expire 设置Key过期时间段(秒)

func (*Key) ExpireAt

func (k *Key) ExpireAt(timestamp int64) *reply

key.ExpireAt 设置Key过期时间点(秒)

func (*Key) Keys

func (k *Key) Keys() *reply

key.Keys 查找键 [*模糊查找]

func (*Key) Move

func (k *Key) Move(db int64) *reply

key.Move 同实例不同库间的键移动

func (*Key) PExpire

func (k *Key) PExpire(millisecond int64) *reply

key.PExpire 设置Key过期时间段(毫秒)

func (*Key) PExpireAt

func (k *Key) PExpireAt(timestamp_ms int64) *reply

key.PExpireAt 设置Key过期时间点(毫秒)

func (*Key) PTTL

func (k *Key) PTTL() *reply

key.PTTL 剩余过期时间(毫秒)

func (*Key) Persist

func (k *Key) Persist() *reply

key.Persist 移除Key的过期设置

func (*Key) RandomKey

func (k *Key) RandomKey() *reply

key.RandomKey 随机返回一个key

func (*Key) Refresh

func (k *Key) Refresh(ttl ...time.Duration) *reply

key.Refresh 刷新Key过期时间 by Ttl

func (*Key) Rename

func (k *Key) Rename(newKey string) *reply

key.Del 重命名

func (*Key) RenameNX

func (k *Key) RenameNX(newKey string) *reply

key.RenameNX 仅当newkey不存在时重命名

func (*Key) Restore

func (k *Key) Restore(ttlMS, serializedValue interface{}) *reply

key.Restore 反序列化

func (*Key) Select

func (k *Key) Select(db int64) *reply

key.Select 选择库

func (*Key) TTL

func (k *Key) TTL() *reply

key.TTL 剩余过期时间(秒)

func (*Key) Type

func (k *Key) Type() *reply

key.Type 返回值的数据类型

func (*Key) Valid

func (k *Key) Valid() bool

key.Valid 键是否有效

type List

type List struct {
	Key
	FixSize int // 定长队列(>0时有效)
}

func (*List) BLpop

func (k *List) BLpop(timeout interface{}) *reply

List.BLpop 阻塞并弹出头元素

func (*List) BRpop

func (k *List) BRpop(timeout interface{}) *reply

List.BRpop 阻塞并弹出末尾元素

func (*List) BRpoplpush

func (k *List) BRpoplpush(key, source, destination string, timeout interface{}) *reply

List.BRpoplpush 阻塞并弹出尾元素,将弹出元素插入另一列表的开头

func (*List) LIndex

func (k *List) LIndex(index int) *reply

List.LIndex 返回指定位置的元素

func (*List) LInsert

func (k *List) LInsert(position, pivot, value string) *reply

List.LInsert 将元素插入指定位置 position:BEFORE|AFTER,当 pivot 不存在于列表 key 时,不执行任何操作。当 key 不存在时, key 被视为空列表,不执行任何操作。

func (*List) LPop

func (k *List) LPop() *reply

List.LPop 移除并返回列表头元素

func (*List) LPush

func (k *List) LPush(value interface{}, whenListExist ...bool) *reply

List.LPush 向列表头插入元素 (whenListExist 当列表存在时才添加)

func (*List) LRange

func (k *List) LRange(start, stop interface{}) *reply

List.LRange 获取指定区间的元素(全部:(0,-1))

func (*List) LRem

func (k *List) LRem(count, value interface{}) *reply

List.LRem 移除元素,count = 0 : 移除表中所有与 value 相等的值,count!=0,移除与 value 相等的元素,数量为 count的绝对值

func (*List) LSet

func (k *List) LSet(index, value interface{}) *reply

List.LSet 设置指定位元素

func (*List) LTrim

func (k *List) LTrim(start, stop interface{}) *reply

List.LTrim 列表裁剪,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。-1 表示尾部

func (*List) RPop

func (k *List) RPop() *reply

List.RPop 移除并返回列表尾元素

func (*List) RPoplpush

func (k *List) RPoplpush(key, source, destination string) *reply

List.RPoplpush 弹出source尾元素并返回,将弹出元素插入destination列表的开头

func (*List) RPush

func (k *List) RPush(value interface{}, whenListExist ...bool) *reply

List.RPush 将指定元素插入列表末尾 (whenListExist 当列表存在时才添加)

type NormalPool

type NormalPool struct{ redis.Pool } //

两种连接模式

func (*NormalPool) Mode

func (*NormalPool) Mode() EMode

type Resp

type Resp = reply

type Set

type Set struct{ Key }

func (*Set) Add

func (k *Set) Add(members ...interface{}) *reply

Set.Add 添加元素

func (*Set) Count

func (k *Set) Count() *reply

Set.Count 集合元素个数

func (*Set) Diff

func (k *Set) Diff(keys []string) *reply

Set.Diff 返回一或多个集合的差集

func (*Set) DiffStore

func (k *Set) DiffStore(destinationKey string, keys []string) *reply

Set.DiffStore 将一或多个集合的差集保存至另一集合(destinationKey)

func (*Set) Inter

func (k *Set) Inter(keys []string) *reply

Set.Inter 一个或多个集合的交集

func (*Set) InterStore

func (k *Set) InterStore(destinationKey string, keys []string) *reply

Set.InterStore 将keys的集合的并集 写入到 destinationKey中

func (*Set) IsMember

func (k *Set) IsMember(member interface{}) *reply

Set.IsMember 判断元素是否是集合成员

func (*Set) Members

func (k *Set) Members() *reply

Set.Members 返回集合中成员

func (*Set) Move

func (k *Set) Move(sourceKey, destinationKey string, member interface{}) *reply

Set.Move 将元素从集合移至另一个集合

func (*Set) Pop

func (k *Set) Pop() *reply

Set.Pop 返回并移除一个元素

func (*Set) RandMember

func (k *Set) RandMember(count ...int64) *reply

Set.RandMember 随机返回一个或多个元素

func (*Set) Rem

func (k *Set) Rem(members ...interface{}) *reply

Set.Rem 移除指定的元素

func (*Set) Union

func (k *Set) Union(keys []string) *reply

Set.Union 返回集合的并集

func (*Set) UnionStore

func (k *Set) UnionStore(destinationKey string, keys []string) *reply

Set.UnionStore 将 keys 的集合的并集 写入到 destinationKey 中

type String

type String struct{ Key }

func (*String) Append

func (k *String) Append(value interface{}) *reply

String.Append 旧值的尾部追加值。

func (*String) Decr

func (k *String) Decr() *reply

String.Decr 自减(并返回减之后的值)

func (*String) DecrBy

func (k *String) DecrBy(increment int64) *reply

String.DecrBy 自减指定值(并返回减之后的值)

func (*String) Get

func (k *String) Get() *reply

String.Get 获取值

func (*String) GetRange

func (k *String) GetRange(start, end int64) *reply

String.GetRange 获取子字符串

func (*String) GetSet

func (k *String) GetSet(value interface{}) *reply

String.GetSet 设置并返回旧值

func (*String) Getm

func (k *String) Getm(keys []string) *reply

String.Getm 返回多个key的值

func (*String) Getms

func (k *String) Getms(args [][]interface{}) *reply

String.Getms 返回多个key的值(以k.K作为fmt模块)

func (*String) Incr

func (k *String) Incr() *reply

String.Incr 自增(并返回加之后的值)

func (*String) IncrBy

func (k *String) IncrBy(increment int64) *reply

String.IncrBy 增加指定值(并返回增加之后的值)

func (*String) IncrByFloat

func (k *String) IncrByFloat(increment float64) *reply

String.IncrByFloat 增加一个浮点值(并返回增加之后的值)

func (*String) LuaIncrBy

func (k *String) LuaIncrBy(amount int) *reply

String.LuaIncrBy 安全增减键值(不会出现负值), Result.Reply==nil表示失败, Result.Int()是最新值

func (*String) Set

func (k *String) Set(mode ESetMode, value interface{}, expire ...time.Duration) *reply

String.Set 设置值(会更新ttl 优先使用 expire)

func (*String) SetRange

func (k *String) SetRange(value interface{}, offset int64) *reply

String.SetRange 设置子字符串

func (*String) Setm

func (k *String) Setm(kv map[string]interface{}) *reply

String.Setm 设置多个值(不更新ttl)

func (*String) SetmNx

func (k *String) SetmNx(kv map[string]interface{}) *reply

String.SetmNx key不存在时设置多个值(当且仅当所有给定键都不存在时,为所有给定键设置值)

func (*String) Setms

func (k *String) Setms(args [][]interface{}, vals []interface{}) *reply

String.Setms 设置多个值(不更新ttl)(以k.K作为fmt模块)

func (*String) Upd

func (k *String) Upd(mode ESetMode, value interface{}) *reply

String.Upd 更新值(不会更新expire时间)

type Zset

type Zset struct{ Key }

func (*Zset) Add

func (k *Zset) Add(mode ESetMode, member, score interface{}) *reply

Zset.Add 添加|更新元素

func (*Zset) AddArr

func (k *Zset) AddArr(ar ...interface{}) *reply

Zset.AddArr 添加|更新多元素 member1 score1, ... memberN,scoreN

func (*Zset) AddMap

func (k *Zset) AddMap(mp map[interface{}]interface{}) *reply

Zset.AddMap 添加|更新多元素 map[member]score

func (*Zset) Card

func (k *Zset) Card() *reply

Zset.Card 返回元素数量

func (*Zset) Count

func (k *Zset) Count(min, max interface{}) *reply

Zset.Count 返回集合两个分数间的元素数(min<=score<=max)

func (*Zset) Del

func (k *Zset) Del(members ...interface{}) *reply

Zset.Del 移除特定元素

func (*Zset) DelRange

func (k *Zset) DelRange(min_start, max_stop interface{}, byScore ...bool) *reply

Zset.DelRange 移除指定区间内的元素([min_start, max_stop])

func (*Zset) Incr

func (k *Zset) Incr(member, increment interface{}) *reply

Zset.Incr 增加元素分数

func (*Zset) Range

func (k *Zset) Range(start, stop interface{}, withScore ...bool) *reply

Zset.Range 返回指定区间内的元素([start, stop])

func (*Zset) Rangebyscore

func (k *Zset) Rangebyscore(min, max interface{}, withScore ...bool) *reply

Zset.Rangebyscore 通过分数返回指定区间内的元素([min, max])

func (*Zset) Rank

func (k *Zset) Rank(e ESort, member interface{}) *reply

Zset.Rank 返回指定元素的排名

func (*Zset) Revrange

func (k *Zset) Revrange(start, stop interface{}, withScore ...bool) *reply

Zset.Revrange 倒序返回指定区间内的元素([start, stop])

func (*Zset) Revrangebyscore

func (k *Zset) Revrangebyscore(min, max interface{}, withScore ...bool) *reply

Zset.Revrangebyscore 倒序通过分数返回指定区间内的元素([min, max])

func (*Zset) Score

func (k *Zset) Score(member interface{}) *reply

Zset.Score 返回指定元素的分数

func (*Zset) Topn

func (k *Zset) Topn(e ESort, start, stopN int, withScore ...bool) *reply

Zset.Topn 返回Top排行元素

func (*Zset) TopnByScore

func (k *Zset) TopnByScore(e ESort, min, max interface{}, withScore ...bool) *reply

Zset.Topn 返回Top排行元素

Jump to

Keyboard shortcuts

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