redis

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 11 Imported by: 1

README

redis

example

    addrs := []string{":6379"}
    auth := ""
    redis := NewManager(addrs, auth, Prefix("test"), SetReadTimeout(time.Second*2),
         SetWriteTimeout(time.Second*2), SetPoolSize(256), SetNamespace("test"))
    redis.Get("aaa")

多集群使用方法

参考 _example 内的 multi_cluster_example.go

Documentation

Overview

Package redis defined redis_client

Package redis defined redis_client

Package redis defined redis_client

Package redis defined redis_client

Package redis defined redis_client

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyConnPool empty connection pool
	ErrEmptyConnPool = errors.New("empty connection pool")
	// ErrAcquiredConnTimeout acquire connection timed out
	ErrAcquiredConnTimeout = errors.New("acquire connection timed out")
	// ErrNilValue redis nil value
	ErrNilValue = redis.ErrNil
	// ErrNoManagerAvailable 表示在全局的 manager map 中找不到该 Manager 的注册记录
	ErrNoManagerAvailable = errors.New("no available manager in manager map, check your cluster name?")
	// ErrSLATimeout 已经超时直接熔断redis操作
	ErrSLATimeout = errors.New("sla timeout, interrupted redis action")
)

Functions

func ByteSlices

func ByteSlices(reply interface{}, err error) ([][]byte, error)

ByteSlices wrapper

func Bytes

func Bytes(reply interface{}, err error) ([]byte, error)

Bytes wrapper

func Int

func Int(reply interface{}, err error) (int, error)

Int wrapper

func Int64

func Int64(reply interface{}, err error) (int64, error)

Int64 wrapper

func Ints

func Ints(reply interface{}, err error) ([]int, error)

Ints wrapper

func String

func String(reply interface{}, err error) (string, error)

String wrapper

func Strings

func Strings(reply interface{}, err error) ([]string, error)

Strings wrapper

func Values

func Values(reply interface{}, err error) ([]interface{}, error)

Values wrapper

Types

type AcquireConnMode

type AcquireConnMode uint8

AcquireConnMode ...

const (
	// AcquireConnModeUnblock ...
	AcquireConnModeUnblock AcquireConnMode = iota
	// AcquireConnModeTimeout ...
	AcquireConnModeTimeout
	// AcquireConnModeBlock ...
	AcquireConnModeBlock
)

type Conn

type Conn struct {
	redis.Conn
	// contains filtered or unexported fields
}

Conn wrapper

type Error

type Error interface {
	error
	MissedKey() bool
}

Error 内部封装,为了区分出 Get 操作时 redis 返回值是否为 nil

type Manager

type Manager struct {
	Connected int // 初始化新建的可用连接数
	// contains filtered or unexported fields
}

Manager redis client

func NewManager

func NewManager(addrs []string, auth string, opts ...Option) (*Manager, error)

NewManager ...

func NewManagerFromConfig

func NewManagerFromConfig(cfg config.Configer, sec string) (*Manager, error)

NewManagerFromConfig ...

func (*Manager) BLPop

func (m *Manager) BLPop(ctx context.Context, key string, secTimeout int64) (interface{}, error)

BLPop command

func (*Manager) BRPop

func (m *Manager) BRPop(ctx context.Context, key string, secTimeout int64) (interface{}, error)

BRPop command

func (*Manager) Decr

func (m *Manager) Decr(ctx context.Context, key string) (reply interface{}, err error)

Decr command

func (*Manager) DecrBy

func (m *Manager) DecrBy(ctx context.Context, key string, delt int) (reply interface{}, err error)

DecrBy command

func (*Manager) Del

func (m *Manager) Del(ctx context.Context, key string) (interface{}, error)

Del command

func (*Manager) Exists

func (m *Manager) Exists(ctx context.Context, key string) (bool, error)

Exists command

func (*Manager) Expire

func (m *Manager) Expire(ctx context.Context, key string, ttl int64) (interface{}, error)

Expire command

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string) (reply interface{}, err error)

Get command

func (*Manager) GetString

func (m *Manager) GetString(ctx context.Context, key string) (reply string, err error)

GetString wrapper get and string

func (*Manager) HDel

func (m *Manager) HDel(ctx context.Context, key, sub string) (interface{}, error)

HDel command

func (*Manager) HExists

func (m *Manager) HExists(ctx context.Context, key, sub string) (bool, error)

HExists command

func (*Manager) HGet

func (m *Manager) HGet(ctx context.Context, key, sub string) (reply interface{}, err error)

HGet command

func (*Manager) HGetAll

func (m *Manager) HGetAll(ctx context.Context, key string) (map[string]string, error)

HGetAll 线上应该谨慎(禁止)使用,subKey数目过多时会阻塞请求,影响性能

func (*Manager) HIncrBy

func (m *Manager) HIncrBy(ctx context.Context, key, sub string, delt int) (reply interface{}, err error)

HIncrBy command

func (*Manager) HKeys

func (m *Manager) HKeys(ctx context.Context, key string) ([]string, error)

HKeys command

func (*Manager) HLen

func (m *Manager) HLen(ctx context.Context, key string) (int, error)

HLen command

func (*Manager) HMGet

func (m *Manager) HMGet(ctx context.Context, key string, subKeys []string) (map[string]string, error)

HMGet command

func (*Manager) HMSet

func (m *Manager) HMSet(ctx context.Context, key string, subKV map[string]interface{}) (interface{}, error)

HMSet command

func (*Manager) HSet

func (m *Manager) HSet(ctx context.Context, key, sub string, val interface{}) (reply interface{}, err error)

HSet command

func (*Manager) Incr

func (m *Manager) Incr(ctx context.Context, key string) (reply interface{}, err error)

Incr command

func (*Manager) IncrBy

func (m *Manager) IncrBy(ctx context.Context, key string, delt int) (reply interface{}, err error)

IncrBy command

func (*Manager) LLen

func (m *Manager) LLen(ctx context.Context, key string) (int, error)

LLen command

func (*Manager) LPop

func (m *Manager) LPop(ctx context.Context, key string) (interface{}, error)

LPop command

func (*Manager) LPush

func (m *Manager) LPush(ctx context.Context, key string, val interface{}) (interface{}, error)

LPush command

func (*Manager) LRange

func (m *Manager) LRange(ctx context.Context, key string, start, end int) ([]interface{}, error)

LRange command

func (*Manager) LRem

func (m *Manager) LRem(ctx context.Context, key string, count int, val interface{}) (int64, error)

LRem command

func (*Manager) MGet

func (m *Manager) MGet(ctx context.Context, keys []string) (map[string]string, error)

MGet command

func (*Manager) MSet

func (m *Manager) MSet(ctx context.Context, kv map[string]interface{}) (reply interface{}, err error)

MSet command

func (*Manager) MockBlock

func (m *Manager) MockBlock(dur time.Duration)

MockBlock ...

func (*Manager) RPop

func (m *Manager) RPop(ctx context.Context, key string) (interface{}, error)

RPop command

func (*Manager) RPush

func (m *Manager) RPush(ctx context.Context, key string, val interface{}) (interface{}, error)

RPush command

func (*Manager) SAdd

func (m *Manager) SAdd(ctx context.Context, key string, member interface{}) (interface{}, error)

SAdd command

func (*Manager) SCard

func (m *Manager) SCard(ctx context.Context, key string) (int, error)

SCard command

func (*Manager) SIsMember

func (m *Manager) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)

SIsMember command

func (*Manager) SMembers

func (m *Manager) SMembers(ctx context.Context, key string) ([]interface{}, error)

SMembers command

func (*Manager) SRem

func (m *Manager) SRem(ctx context.Context, key string, member interface{}) (interface{}, error)

SRem command

func (*Manager) SUnion

func (m *Manager) SUnion(ctx context.Context, sets []string) ([]interface{}, error)

SUnion command

func (*Manager) Set

func (m *Manager) Set(ctx context.Context, key string, val interface{}) (reply interface{}, err error)

Set command

func (*Manager) SetEx

func (m *Manager) SetEx(ctx context.Context, key string, expireTime int, val interface{}) (reply interface{}, err error)

SetEx command

func (*Manager) SetNEx

func (m *Manager) SetNEx(ctx context.Context, key string, expireTime int, val interface{}) (reply interface{}, err error)

SetNEx setNX与Expire的合并 需要redis版本大于2.6.12

func (*Manager) ZAdd

func (m *Manager) ZAdd(ctx context.Context, key string, score float64, member interface{}) (interface{}, error)

ZAdd command

func (*Manager) ZRangeByScore

func (m *Manager) ZRangeByScore(ctx context.Context, key string, min, max float64) (interface{}, error)

ZRangeByScore command

func (*Manager) ZRemRangeByScore

func (m *Manager) ZRemRangeByScore(ctx context.Context, key string, min, max float64) (interface{}, error)

ZRemRangeByScore command

type ManagerMap

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

ManagerMap 存储一组 Manager,按照

func NewManagerMap

func NewManagerMap(clusterConfigArr []ManagerMapConfig) (*ManagerMap, error)

NewManagerMap 返回一组 codis cluster 按照名字获取对应的 Manager

func (*ManagerMap) GetManager

func (mm *ManagerMap) GetManager(clusterName string) (*Manager, error)

GetManager 根据 codis 的 cluster name 获取对应的 Manager

type ManagerMapConfig

type ManagerMapConfig struct {
	Addrs            []string
	Auth             string
	CodisClusterName string
	Opts             []Option
}

ManagerMapConfig 对应一个 codis 集群(几个对等的ip)

type Option

type Option func(o *option)

Option 动态参数配置,使 NewManger 支持变参,不设定的参数使用默认值

func DisfServiceName

func DisfServiceName(sn string) Option

DisfServiceName 设置disf对应的service name

func EnableDisf

func EnableDisf() Option

EnableDisf 启用disf

func EnableNodemgr

func EnableNodemgr() Option

EnableNodemgr 启用nodemgr

func Prefix

func Prefix(prefix string) Option

Prefix 是 redis key 默认的 Prefix

func SetAcquireConnMode

func SetAcquireConnMode(mode AcquireConnMode) Option

SetAcquireConnMode 设置获取连接模式:阻塞、超时等待、直接返回

func SetClusterName

func SetClusterName(cn string) Option

SetClusterName 设置 codis 集群的名字

func SetConnectTimeout

func SetConnectTimeout(cTimeout time.Duration) Option

SetConnectTimeout 连接超时

func SetDB

func SetDB(db int) Option

func SetHealthyThreshold

func SetHealthyThreshold(threshold int64) Option

SetHealthyThreshold 设置nodemgr 健康节点投票阈值

func SetKeepSilent

func SetKeepSilent(silent bool) Option

SetKeepSilent 设置静默模式,初始化如果有host连不上则忽略

func SetMaxConn

func SetMaxConn(max int64) Option

SetMaxConn 设置连接池内的连接上限

func SetMaxCooldownTime

func SetMaxCooldownTime(time int64) Option

SetMaxCooldownTime 设置nodemgr 故障恢复时间

func SetMinHealthyRatio

func SetMinHealthyRatio(ratio float64) Option

SetMinHealthyRatio 设置nodemgr 最小可用度保护

func SetPoolSize

func SetPoolSize(poolSize int64) Option

SetPoolSize 设置初始连接池大小 叫 initial pool size 更合适

func SetReadTimeout

func SetReadTimeout(rTimeout time.Duration) Option

SetReadTimeout 读取数据超时

func SetSLAFuse

func SetSLAFuse(enable bool) Option

SetSLAFuse 设置是否启用SLA熔断

func SetStatFunc

func SetStatFunc(sFunc func(ctx context.Context, cmd string, cost time.Duration, err error) error) Option

SetStatFunc 是统计命令字符串、时延、redis操作错误的闭包

func SetWaitTimeout

func SetWaitTimeout(wtTimeout time.Duration) Option

SetWaitTimeout 等待连接池中的连接的超时

func SetWorkerCycle

func SetWorkerCycle(cycle int) Option

SetWorkerCycle 设置nodemgr 节点状态更新周期

func SetWriteTimeout

func SetWriteTimeout(wTimeout time.Duration) Option

SetWriteTimeout 写数据超时

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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