redis

package
v0.0.0-...-94c11f6 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 12 Imported by: 0

README

单元测试可以使用 https://github.com/alicebob/miniredis, 可以开启一个本地的模拟redis

案例

Redis 优化方向

参数优化

maxIdle设置高点,可以保证突发流量情况下,能够有足够的连接去获取redis,不用在高流量情况下建立连接

go-redis参数优化

  min_idle_conn: 30               
  dial_timeout: "1s"
  read_timeout: "500ms"
  write_timeout: "500ms"
  pool_size: 500
  pool_timeout: "60s"

redisgo参数优化

maxIdle = 30
maxActive = 500
dialTimeout = "1s"
readTimeout = "500ms"
writeTimeout = "500ms"
idleTimeout = "60s"

使用优化

  • 增加redis从库
  • 对批量数据,根据redis从库数量,并发goroutine拉取数据
  • 对批量数据大量使用pipeline指令
  • 精简key字段
  • redis的value存储编解码改为msgpack

Pipeline

Documentation

Index

Constants

View Source
const (
	// PrefixCheckRepeat check repeat key
	PrefixCheckRepeat = "CHECK_REPEAT"
	// RepeatDefaultTimeout define default timeout
	RepeatDefaultTimeout = 60
)
View Source
const (
	// ErrRedisNotFound not exist in redis
	ErrRedisNotFound = redis.Nil
	// DefaultRedisName default redis name
	DefaultRedisName = "default"
)

Variables

View Source
var RedisClient *redis.Client

RedisClient redis 客户端

Functions

func Init

func Init() (*redis.Client, func(), error)

Init init a default redis instance

func InitTestRedis

func InitTestRedis()

InitTestRedis 实例化一个可以用于单元测试的redis

Types

type CheckRepeat

type CheckRepeat interface {
	Set(key string, value interface{}, expiration time.Duration) error
	Get(key string) (string, error)
	SetNX(key string, value interface{}, expiration time.Duration) (bool, error)
	Del(keys string) int64
}

CheckRepeat define interface

func NewCheckRepeat

func NewCheckRepeat(client *redis.Client) CheckRepeat

NewCheckRepeat create a check repeat

type Config

type Config struct {
	Addr         string
	Password     string
	DB           int
	MinIdleConn  int
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	PoolSize     int
	PoolTimeout  time.Duration
	// tracing switch
	EnableTrace bool
}

Config redis config

func LoadConf

func LoadConf(name string) (ret *Config, err error)

LoadConf load redis config

type IDAlloc

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

IDAlloc id生成器 key 为业务key, 由业务前缀+功能前缀+具体场景id组成 比如生成用户id, 可以传入user_id, 完整示例: eagle:idalloc:user_id

func NewIDAlloc

func NewIDAlloc(conn *redis.Client) *IDAlloc

NewIDAlloc create a id alloc instance

func (*IDAlloc) GetCurrentID

func (ia *IDAlloc) GetCurrentID(key string) (int64, error)

GetCurrentID 获取当前id

func (*IDAlloc) GetKey

func (ia *IDAlloc) GetKey(key string) string

GetKey 获取key

func (*IDAlloc) GetNewID

func (ia *IDAlloc) GetNewID(key string, step int64) (int64, error)

GetNewID 生成id

type RedisManager

type RedisManager struct {
	*sync.RWMutex
	// contains filtered or unexported fields
}

RedisManager define a redis manager nolint

func NewRedisManager

func NewRedisManager() *RedisManager

NewRedisManager create a redis manager

func (*RedisManager) GetClient

func (r *RedisManager) GetClient(name string) (*redis.Client, error)

GetClient get a redis instance

Jump to

Keyboard shortcuts

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