psnowflake

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDeviceID

func GetDeviceID(sid int64) (datacenterid, workerid int64)

获取数据中心ID和机器ID

func GetGenTime

func GetGenTime(sid int64) (t string)

获取创建ID时的时间字符串(精度:秒)

func GetGenTimestamp

func GetGenTimestamp(sid int64) (timestamp int64)

获取创建ID时的时间戳

func GetTimestamp

func GetTimestamp(sid int64) (timestamp int64)

获取时间戳

func GetTimestampStatus

func GetTimestampStatus() (state float64)

获取时间戳已使用的占比:范围(0.0 - 1.0)

Types

type Snowflake

type Snowflake struct {
	sync.Mutex //加锁,防止并发碰撞
	// contains filtered or unexported fields
}

func NewSnowflake

func NewSnowflake(redisObj *redis.Client, datacenterid, workerid int64) (*Snowflake, error)

NewSnowflake redisObj: *redis.Client 已经实例化的redis链接对象 datacenterid: int64 workerid: int64

func (*Snowflake) Generate

func (s *Snowflake) Generate(prefix ...string) (string, error)

Generate 生成一个唯一ID prefix: string key前缀,用于区分不同场景用途的发号 return: string

func (*Snowflake) GenerateBatch

func (s *Snowflake) GenerateBatch(nums int, prefix ...string) (arr []string, err error)

GenerateBatch 批量生成指定数量的ID nums: int 数量 prefix: string key前缀,用于区分不同场景用途的发号 return: []string 返回ID数组

func (*Snowflake) Produce

func (s *Snowflake) Produce(nums int, prefix ...string) (int, error)

Produce 批量预生成ID,并将结果存储到redis中 nums: int 生成号的数量 prefix: key前缀,用于区分不同场景用途的发号 return:

count: 成功写入Redis集合的数量
err: 错误信息

Jump to

Keyboard shortcuts

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