redis

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2021 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

Overview

trace redis link: https://github.com/smacker/opentracing-go-redis

Index

Constants

View Source
const (
	// PrefixCheckRepeat check repeat key
	PrefixCheckRepeat = "CHECK_REPEAT"
	// RepeatDefaultTimeout define default timeout
	RepeatDefaultTimeout = 60
)
View Source
const (
	// LockKey redis lock key
	LockKey = "snake:redis:lock:%s"
	// DefaultTimeout default expire time
	DefaultTimeout = 2 * time.Second
)
View Source
const ErrRedisNotFound = redis.Nil

ErrRedisNotFound not exist in redis

Variables

View Source
var RedisClient *redis.Client

RedisClient redis 客户端

Functions

func Init

func Init(c *Config) *redis.Client

Init 实例化一个redis client

func InitTestRedis added in v1.0.3

func InitTestRedis()

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

func WrapRedisClient added in v1.3.1

func WrapRedisClient(ctx context.Context, client *redis.Client) *redis.Client

WrapRedisClient adds opentracing measurements for commands and returns cloned client

Types

type CheckRepeat added in v1.3.0

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 added in v1.3.0

func NewCheckRepeat(client *redis.Client) CheckRepeat

NewCheckRepeat create a check repeat

type Config added in v1.3.1

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
}

Config redis config

type IDAlloc added in v1.3.0

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

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

func NewIDAlloc added in v1.3.0

func NewIDAlloc(conn *redis.Client) *IDAlloc

NewIDAlloc create a id alloc instance

func (*IDAlloc) GetCurrentID added in v1.3.0

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

GetCurrentID 获取当前id

func (*IDAlloc) GetKey added in v1.3.0

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

GetKey 获取key

func (*IDAlloc) GetNewID added in v1.3.0

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

GetNewID 生成id

type Lock added in v1.1.1

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

Lock 定义lock结构体

func NewLock added in v1.1.1

func NewLock(conn *redis.Client, key string, opts ...Option) *Lock

NewLock new a lock instance

func (*Lock) GetKey added in v1.1.1

func (l *Lock) GetKey() string

GetKey 获取key

func (*Lock) Lock added in v1.1.1

func (l *Lock) Lock(ctx context.Context) (bool, error)

Lock 加锁

func (*Lock) Unlock added in v1.1.1

func (l *Lock) Unlock(ctx context.Context) error

Unlock 解锁 token 一致才会执行删除,避免误删,这里用了lua脚本进行事务处理

type Option added in v1.3.0

type Option func(*Lock)

func WithTimeout added in v1.3.1

func WithTimeout(expiration time.Duration) Option

Jump to

Keyboard shortcuts

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