redis

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: BSD-3-Clause Imports: 14 Imported by: 1

README

ginny-redis

redis provider for ginny.

Documentation

Index

Constants

This section is empty.

Variables

Provider

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	//集群节点地址
	ClusterAddrs []string `json:"cluster_addrs" mapstructure:"cluster_addrs"`
	//最大的重定向重试次数,网络错误或找错数据分片时 client会得到重定向错误和集群的最新情况 进行重定向。-1表示不限制,driver默认3次
	MaxRedirects     int               `json:"max_redirects" mapstructure:"max_redirects"`
	ClusterSlotAddrs []ClusterSlotAddr `json:"cluster_slot_addrs" mapstructure:"cluster_slot_addrs"`
}

Cluster redisdb cluster部署特性配置

type ClusterSlotAddr added in v0.0.4

type ClusterSlotAddr struct {
	Master string   `json:"master" mapstructure:"master"`
	Slaves []string `json:"slaves" mapstructure:"slaves"`
}

type Config

type Config struct {
	// 6.0及以上版本的redis采用ACL系统,使用Username鉴定连接
	Username string `json:"username" mapstructure:"username"`
	Password string `json:"password" mapstructure:"password"`
	//业务库DB,使用ClusterClient时该配置失效(包括redis cluster部署、读写分离配置下的sentinel、读写分离或分片多主standalone)
	DB int `json:"db" mapstructure:"db"`

	//连接池可持有分配的最大连接数。driver默认 Client:10*NumCPU ClusterClient:5*NumCPU
	PoolSize int `json:"pool_size" mapstructure:"pool_size"`
	//最小空闲连接数
	MinIdleConns int `json:"min_idle_conns" mapstructure:"min_idle_conns"`
	//空闲连接超时时间(单位s),保持空闲超过该持续时长的连接会由client主动关闭。driver默认5min,配置-1 则不检查空闲超时
	//该设置需小于redis服务端的timeout设置(建议redis服务端timeout设置为0,即服务端不主动断开连接)
	IdleTimeout int `json:"idle_timeout" mapstructure:"idle_timeout"`
	//idle connection reaper对空闲连接的检查频率(单位s)。driver默认1min,配置-1 则reaper无效 但若配置了IdleTimeout 空闲连接仍会被连接池丢弃
	IdleCheckFrequency int `json:"idle_check_frequency" mapstructure:"idle_check_frequency"`
	//连接龄期(单位s),过旧连接会被client关闭。driver默认不关闭过旧连接
	MaxConnAge int `json:"max_conn_age" mapstructure:"max_conn_age"`
	//连接池超时时间(单位s),连接池所有连接都忙时 client等待PoolTimeout后返回错误。driver默认ReadTimeout+1s
	PoolTimeout int `json:"pool_timeout" mapstructure:"pool_timeout"`

	//连接超时(单位s),driver默认5s
	DialTimeout int `json:"dial_timeout" mapstructure:"dial_timeout"`
	//读超时(单位s),driver默认3s,配置-1 无超时
	ReadTimeout int `json:"read_timeout" mapstructure:"read_timeout"`
	//写超时(单位s),driver默认同ReadTimeout
	WriteTimeout int `json:"write_timeout" mapstructure:"write_timeout"`

	//读写分离配置,均为false时 不读从库 所有读写都连主库,二者均为true时driver优先选择RouteByLatency策略
	//允许路由只读命令到最近的主或从节点
	RouteByLatency bool `json:"route_by_latency" mapstructure:"route_by_latency"`
	//允许路由只读命令到任意主或从节点
	RouteRandomly bool `json:"route_randomly" mapstructure:"route_randomly"`

	//通过3种部署的必要配置有无 来区分部署模式:MasterName与SentinelAddrs、ClusterAddrs、StandaloneAddrs
	Sentinel   `json:"sentinel,inline" mapstructure:"sentinel,inline"`
	Cluster    `json:"cluster,inline" mapstructure:"cluster,inline"`
	Standalone `json:"standalone,inline" mapstructure:"standalone,inline"`
}

Config redis基础配置

func NewConfig

func NewConfig(v *viper.Viper) (*Config, error)

NewConfig

func NewConfigByDSN added in v0.0.4

func NewConfigByDSN(ctx context.Context, dsn string) (*Config, error)

NewConfigByDSN redis://127.0.0.1:6379?db=0&username=&password=&slaves=&sentinel_addrs=&master_name= &sentinel_password&cluster_addrs=&max_redirects

func (*Config) String

func (c *Config) String() string

String 打印可输出的配置

type Redis

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

Redis redis管理器:client连接池(cluster、sentinel、standalone部署模式,可配置读写分离),lua脚本管理

func NewRedis

func NewRedis(ctx context.Context, config *Config, logger *zap.Logger) (*Redis, error)

NewRedis 根据基础配置 初始化redis管理器

func (*Redis) Client

func (m *Redis) Client() redis.UniversalClient

Client 返回连接池客户端

func (*Redis) Close

func (m *Redis) Close()

Close 释放连接池使用的资源。该函数应当很少用到

func (*Redis) RegisterScript

func (m *Redis) RegisterScript(ctx context.Context, kvs ...interface{}) error

RegisterScript 注册redis lua script:若服务用到某个脚本,需在服务初始化时注册脚本,每增加一个脚本均需注册 若redis服务实例的脚本缓存未缓存该脚本则Load,以便后续调用只需传脚本的SHA1校验和:节省带宽、且redis无需再次编译脚本,

主要为方便script与pipeline等结合使用时可直接调用EvalSha(单独使用script时driver已做处理)

kvs必须偶数个: key1 value1 key2 value2 ...。key为各自服务包内定义,可参考type scriptKeyXXX struct{} 空结构体。

value是相应脚本*redisdb.Script=redisdb.NewScript()

func (*Redis) Script

func (m *Redis) Script(key interface{}) *redis.Script

Script 根据脚本key取脚本结构 调用方保证:需在服务初始化时 调用RegisterScript()注册相应的键值对,对未注册的key 本函数将返回nil

type Sentinel

type Sentinel struct {
	//主节点别名
	MasterName string `json:"master_name" mapstructure:"master_name"`
	//哨兵节点的地址、requirepass
	SentinelAddrs    []string `json:"sentinel_addrs" mapstructure:"sentinel_addrs"`
	SentinelPassword string   `json:"sentinel_password" mapstructure:"sentinel_password"`
}

Sentinel 哨兵部署特性配置

type Standalone

type Standalone struct {
	//需要读写分离时 需配出部署中的所有主从节点地址,1套主从为1个ClusterSlot,仅单机模式 只有1主没有从时 不写slaves配置
	Master string   `json:"master" mapstructure:"master"`
	Slaves []string `json:"slaves" mapstructure:"slaves"`
}

Standalone 单机、一主多从、或非redis哨兵/集群模式的分片集群部署(多主多从 此种情况应当少见) 特性配置

Jump to

Keyboard shortcuts

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