module_id

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultIdStorageSpan int64 = 500
	DefaultStartId       int64 = 10000
)

Functions

This section is empty.

Types

type IdFace

type IdFace interface {
	GetId(ctx context.Context, idType IdType) (int64, error)
	GetIds(ctx context.Context, idType IdType, size int64) ([]int64, error)
}
var (
	// IdWorker 全局的ID生成器 请在 程序初始化 时候赋值
	IdWorker IdFace
)

type IdGenerator

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

func NewIdGenerator

func NewIdGenerator(handler IdHandler) *IdGenerator

func (*IdGenerator) GetId

func (this_ *IdGenerator) GetId(ctx context.Context, idType IdType) (int64, error)

func (*IdGenerator) GetIds

func (this_ *IdGenerator) GetIds(ctx context.Context, idType IdType, size int64) ([]int64, error)

func (*IdGenerator) SetStartIdForType

func (this_ *IdGenerator) SetStartIdForType(idType IdType, startId int64)

SetStartIdForType 为特定类型设置起始ID

func (*IdGenerator) WithIdStorageSpan

func (this_ *IdGenerator) WithIdStorageSpan(span int64) *IdGenerator

WithIdStorageSpan 设置ID存储跨度

func (*IdGenerator) WithStartId

func (this_ *IdGenerator) WithStartId(startId int64) *IdGenerator

WithStartId 设置默认起始ID

type IdHandler

type IdHandler interface {
	ShouldCheckAndPersist() bool
	// Increment 增长,使用 redis 的 incr 指令实现原子操作
	// 如果缓存不存在则返回的 id 为 0
	Increment(ctx context.Context, idType IdType, size int64) (int64, error)
	// CacheId 缓存 id,缓存中不存在则通过 QueryId 查询恢复
	CacheId(ctx context.Context, idType IdType, id int64) error
	// StorageQueryId 存储 查询 id,缓存中不存在时进行查询
	StorageQueryId(ctx context.Context, idType IdType) (int64, error)
	// StorageInsert 存储 新增 id
	StorageInsert(ctx context.Context, idType IdType, id int64) error
	// StorageUpdate 存储 更新 id
	StorageUpdate(ctx context.Context, idType IdType, id int64) error
	// RecoverLock id 恢复锁,通常在存储时使用
	RecoverLock(ctx context.Context, idType IdType) (Unlocker, error)
	// PersistLock id 入库落地锁,通常在存储时使用
	PersistLock(ctx context.Context, idType IdType) (Unlocker, error)
}

type IdType

type IdType string

type Unlocker

type Unlocker interface {
	Unlock()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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