redis

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CtxKey_caller = utils.CtxKey("caller") // 值:CallerDesc对象 一般情况内部使用
View Source
const CtxKey_cmddesc = utils.CtxKey("cmddesc") // 值:字符串 命令描述 一般情况内部使用
View Source
const CtxKey_nolog = utils.CtxKey_nolog // 不打印日志,错误日志还会打印 值:不受限制 一般写1

Variables

View Source
var RegExprieTime = 8

多长时间没更新就认为是取消注册了,单位秒, 允许外部修改 Register对象的注册频率:RegExprieTime/2

View Source
var RegSep string = "&"

Reids中存储注册服务器各字段的分割符号,所以也要要求服务器字符串字段不能包含下面这个分隔符, 允许外部修改

Functions

func IsNilError

func IsNilError(err error) bool

具体命令接口,每个工程自己定制

Types

type Config

type Config struct {
	Addr          string `json:"addr,omitempty"` //地址,host:port
	Passwd        string `json:"passwd,omitempty"`
	DB            int    `json:"db,omitempty"`
	SSl           int    `json:"ssl,omitempty"`
	PoolSize      int    `json:"poolsize,omitempty"`      //连接池大小
	MaxActiveConn int    `json:"maxactiveconn,omitempty"` //最大活跃连接数量
}

type Redis

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

func DefaultRedis

func DefaultRedis() *Redis

func InitDefaultRedis

func InitDefaultRedis(cfg *Config) (*Redis, error)

func NewRedis

func NewRedis(cfg *Config) (*Redis, error)

func (*Redis) CreateRegister

func (r *Redis) CreateRegister(key string, cfg *RegistryInfo) *Register

key是按照zset写入

func (*Redis) CreateRegisterEx

func (r *Redis) CreateRegisterEx(key string, cfgs []*RegistryInfo) *Register

func (*Redis) Do

func (r *Redis) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error)

func (*Redis) Do2

func (r *Redis) Do2(ctx context.Context, cmd string, args ...interface{}) RedisResultBind

func (*Redis) DoCmdBool

func (r *Redis) DoCmdBool(ctx context.Context, cmd string, args ...interface{}) (bool, error)

func (*Redis) DoCmdByteSlices

func (r *Redis) DoCmdByteSlices(ctx context.Context, cmd string, args ...interface{}) ([][]byte, error)

func (*Redis) DoCmdBytes

func (r *Redis) DoCmdBytes(ctx context.Context, cmd string, args ...interface{}) ([]byte, error)

func (*Redis) DoCmdFloat64

func (r *Redis) DoCmdFloat64(ctx context.Context, cmd string, args ...interface{}) (float64, error)

func (*Redis) DoCmdFloat64s

func (r *Redis) DoCmdFloat64s(ctx context.Context, cmd string, args ...interface{}) ([]float64, error)

func (*Redis) DoCmdInt

func (r *Redis) DoCmdInt(ctx context.Context, cmd string, args ...interface{}) (int, error)

func (*Redis) DoCmdInt64

func (r *Redis) DoCmdInt64(ctx context.Context, cmd string, args ...interface{}) (int64, error)

func (*Redis) DoCmdInt64Map

func (r *Redis) DoCmdInt64Map(ctx context.Context, cmd string, args ...interface{}) (map[string]int64, error)

func (*Redis) DoCmdInt64s

func (r *Redis) DoCmdInt64s(ctx context.Context, cmd string, args ...interface{}) ([]int64, error)

func (*Redis) DoCmdIntMap

func (r *Redis) DoCmdIntMap(ctx context.Context, cmd string, args ...interface{}) (map[string]int, error)

func (*Redis) DoCmdInts

func (r *Redis) DoCmdInts(ctx context.Context, cmd string, args ...interface{}) ([]int, error)

func (*Redis) DoCmdString

func (r *Redis) DoCmdString(ctx context.Context, cmd string, args ...interface{}) (string, error)

func (*Redis) DoCmdStringMap

func (r *Redis) DoCmdStringMap(ctx context.Context, cmd string, args ...interface{}) (map[string]string, error)

func (*Redis) DoCmdStrings

func (r *Redis) DoCmdStrings(ctx context.Context, cmd string, args ...interface{}) ([]string, error)

func (*Redis) DoCmdUint64

func (r *Redis) DoCmdUint64(ctx context.Context, cmd string, args ...interface{}) (uint64, error)

func (*Redis) DoScript

func (r *Redis) DoScript(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (interface{}, error)

func (*Redis) DoScript2

func (r *Redis) DoScript2(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) RedisResultBind

func (*Redis) DoScriptBool

func (r *Redis) DoScriptBool(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (bool, error)

func (*Redis) DoScriptByteSlices

func (r *Redis) DoScriptByteSlices(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([][]byte, error)

func (*Redis) DoScriptBytes

func (r *Redis) DoScriptBytes(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([]byte, error)

func (*Redis) DoScriptFloat64

func (r *Redis) DoScriptFloat64(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (float64, error)

func (*Redis) DoScriptFloat64s

func (r *Redis) DoScriptFloat64s(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([]float64, error)

func (*Redis) DoScriptInt

func (r *Redis) DoScriptInt(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (int, error)

func (*Redis) DoScriptInt64

func (r *Redis) DoScriptInt64(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (int64, error)

func (*Redis) DoScriptInt64Map

func (r *Redis) DoScriptInt64Map(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (map[string]int64, error)

func (*Redis) DoScriptInt64s

func (r *Redis) DoScriptInt64s(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([]int64, error)

func (*Redis) DoScriptIntMap

func (r *Redis) DoScriptIntMap(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (map[string]int, error)

func (*Redis) DoScriptInts

func (r *Redis) DoScriptInts(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([]int, error)

func (*Redis) DoScriptString

func (r *Redis) DoScriptString(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (string, error)

func (*Redis) DoScriptStringMap

func (r *Redis) DoScriptStringMap(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (map[string]string, error)

func (*Redis) DoScriptStrings

func (r *Redis) DoScriptStrings(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) ([]string, error)

func (*Redis) DoScriptUint64

func (r *Redis) DoScriptUint64(ctx context.Context, script *RedisScript, keysAndArgs ...interface{}) (uint64, error)

func (*Redis) HMGetObj

func (r *Redis) HMGetObj(ctx context.Context, key string, v interface{}) error

针对HMGET命令 调用Cmd时,参数不需要包括field 结构成员首字母需要大写,tag中必须是包含 `redis:"hello"` 其中hello就表示在redis中存储的field名称 结构成员类型 : Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Float32, Float64, String, []byte 结构成员其他类型 : 通过Json转化 传入的参数为结构的地址 参数组织调用 hmsetObjArgs hmgetObjArgs

func (*Redis) HMSetObj

func (r *Redis) HMSetObj(ctx context.Context, key string, v interface{}) error

参数v 参考Redis.HMGetObj

func (*Redis) HasKey

func (r *Redis) HasKey(ctx context.Context, key string) bool

func (*Redis) Lock

func (r *Redis) Lock(ctx context.Context, key string, timeout time.Duration) (func(), error)

只尝试多次加锁,超时后,返回失败

func (*Redis) NewPipeline

func (r *Redis) NewPipeline() *RedisPipeline

func (*Redis) Pool

func (r *Redis) Pool() *redis.Pool

Pool 暴露原始对象

func (*Redis) ReadServices

func (r *Redis) ReadServices(ctx context.Context, key string, serverNames []string) ([]*RegistryInfo, error)

读取一次服务器列表

func (*Redis) RegHook

func (r *Redis) RegHook(f func(ctx context.Context, cmd *RedisCommond))

func (*Redis) TryLock

func (r *Redis) TryLock(ctx context.Context, key string, timeout time.Duration) (func(), error)

只尝试一次加锁,失败直接返回

func (*Redis) WatchServices

func (r *Redis) WatchServices(key string, serverNames []string, fun func(infos []*RegistryInfo))

WatchService 监控服务器变化 RegistryConfig值填充Registry前缀的变量 回调外部不要修改infos参数 key 表示服务器发现的key serverName 表示监听哪些服务器 为空表示监听全部的服务器

func (*Redis) WatchServices2

func (r *Redis) WatchServices2(key string, serverNames []string, fun func(addInfos, delInfos []*RegistryInfo))

WatchService 监控服务器变化,通知变化,增加或者删除 key 表示服务器发现的key serverName 表示监听哪些服务器 为空表示监听全部的服务器

type RedisCommond

type RedisCommond struct {
	// 命令名和参数
	Cmd     string
	Args    []interface{}
	CmdDesc string // 命令的描述

	Caller *utils.CallerDesc

	// 执行结果 Reids直接调用命令使用,如果Reply和Err说明命令没有执行
	Reply   interface{}
	Err     error
	Elapsed time.Duration
	// contains filtered or unexported fields
}

Redis命令封装 并实现RedisResultBind接口 注意Redis执行结果为空时(ErrNil),并不认为是错误

func (*RedisCommond) Bind

func (c *RedisCommond) Bind(v interface{}) error

func (*RedisCommond) BindJsonObj

func (c *RedisCommond) BindJsonObj(v interface{}) error

func (*RedisCommond) BindJsonObjSlice

func (c *RedisCommond) BindJsonObjSlice(v interface{}) error

func (*RedisCommond) BindMap

func (c *RedisCommond) BindMap(v interface{}) error

func (*RedisCommond) BindSlice

func (c *RedisCommond) BindSlice(v interface{}) error

func (*RedisCommond) CmdString

func (c *RedisCommond) CmdString() string

func (*RedisCommond) ReplyString

func (c *RedisCommond) ReplyString() string

type RedisPipeline

type RedisPipeline struct {

	// 支持特殊情况下指定超时,如 BLPOP 命令
	Timeout time.Duration
	// contains filtered or unexported fields
}

func (*RedisPipeline) Cmd

func (p *RedisPipeline) Cmd(commandName string, args ...interface{}) RedisResultBind

统一的命令

func (*RedisPipeline) Do

func (p *RedisPipeline) Do(ctx context.Context) error

func (*RedisPipeline) DoEx

func (p *RedisPipeline) DoEx(ctx context.Context) ([]*RedisCommond, error)

任何一条命令错误,都会返回错误

func (*RedisPipeline) HMGetObj

func (p *RedisPipeline) HMGetObj(key string, v interface{}) error

参数v 参考Redis.HMGetObj的说明

func (*RedisPipeline) HMSetObj

func (p *RedisPipeline) HMSetObj(key string, v interface{}) bool

参数v 参考Redis.HMGetObj的说明

type RedisResultBind

type RedisResultBind interface {
	// BindKind : Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Float32, Float64, String, []byte
	// 其他类型 : 通过Json转化
	// 传入的参数为对象的地址
	Bind(v interface{}) error

	// BindKind : Slice
	// SliceElemKind : Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Float32, Float64, String
	// 其他类型 : 通过Json转化
	// 传入的参数为Slice的地址 nil的Slice也可以
	BindSlice(v interface{}) error

	// BindType : Map
	// MapElemType : Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Float32, Float64, String
	// 其他类型 : 通过Json转化
	// 传入的参数为Map的地址 nil的Map也可以
	BindMap(v interface{}) error

	// BindType : Struct
	// 结构支持json格式化
	// 传入的参数为结构的地址
	BindJsonObj(v interface{}) error

	// BindType : []Struct 或者 []*Struct
	// SliceElemType 支持json格式化
	// 传入的参数为Slice的地址 nil的Slice也可以
	BindJsonObjSlice(v interface{}) error
}

Redis结果绑定,用这种方式认为不需要太关心Redis执行结果真的正确与否

type RedisScript

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

func NewScript

func NewScript(keyCount int, src string) *RedisScript

type Register

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

func (*Register) DeReg

func (r *Register) DeReg() error

func (*Register) Reg

func (r *Register) Reg() error

type RegistryInfo

type RegistryInfo struct {
	RegistryName   string `json:"registryname,omitempty"`   // 注册的名字 组名
	RegistryID     string `json:"registryid,omitempty"`     // 注册的ID 服务器唯一ID
	RegistryAddr   string `json:"registryaddr,omitempty"`   // 服务器对外暴露的地址
	RegistryPort   int    `json:"registryport,omitempty"`   // 服务器对外暴露的端口
	RegistryScheme string `json:"registryscheme,omitempty"` // 服务器使用的协议
}

RegistryInfo 服务注册信息

func (*RegistryInfo) MarshalZerologObject

func (r *RegistryInfo) MarshalZerologObject(e *zerolog.Event)

Jump to

Keyboard shortcuts

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