redsid

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: MIT Imports: 4 Imported by: 0

README

redsId

为每一个worker实例获取一个递增的连续的唯一的ID

  1. 启动worker实例
  2. worker连接redis, 同时从0开始检查, 本实例想要获取的ID存不存在
  3. 不存在, 设置ID为key, 同时设置过期时间
  4. 定时1s 去检查key是否过期
  5. 实例退出后, 那么redis的key过期, 实例重新启动之后, 那么通过检查重新获取key

example

client := redis.NewClient(&redis.Options{
	Network: "tcp",
	Addr:    "127.0.0.1:6379",
})
defer client.Close()

rn := redsid.New()
rn.SetRedisClient(client)
rn.Start(func(err error) {
    fmt.Println("error", err)
})

for i := 0; i < 100; i++ {
    fmt.Println(rn.GetID())
}

select {}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cfg

type Cfg struct {
	// 名字前缀
	// 默认: redsID
	NamePrefix string

	// 过期时间
	// 默认: 5s
	ExpiredTime time.Duration

	// 重试时间
	// 默认: 2s
	RetryTime time.Duration
	// contains filtered or unexported fields
}

func New

func New() *Cfg

func (*Cfg) GetID

func (t *Cfg) GetID() int

func (*Cfg) SetRedisClient

func (t *Cfg) SetRedisClient(client *redis.Client)

func (*Cfg) Start

func (t *Cfg) Start()

func (*Cfg) Stop

func (t *Cfg) Stop()

Jump to

Keyboard shortcuts

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