wuid

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Example
newClient := func() (redis.UniversalClient, bool, error) {
	var client redis.UniversalClient
	// ...
	return client, true, nil
}

// Setup
w := NewWUID("alpha", nil)
err := w.LoadH28FromRedis(newClient, "wuid")
if err != nil {
	panic(err)
}

// Generate
for i := 0; i < 10; i++ {
	fmt.Printf("%#016x\n", w.Next())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NewClient

type NewClient func() (client redis.UniversalClient, autoClose bool, err error)

type Option

type Option = internal.Option

func WithH28Verifier

func WithH28Verifier(cb func(h28 int64) error) Option

WithH28Verifier adds an extra verifier for the high 28 bits.

func WithObfuscation

func WithObfuscation(seed int) Option

WithObfuscation enables number obfuscation.

func WithSection

func WithSection(section int8) Option

WithSection brands a section ID on each generated number. A section ID must be in between [0, 7].

func WithStep

func WithStep(step int64, floor int64) Option

WithStep sets the step and the floor for each generated number.

type WUID

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

WUID is an extremely fast universal unique identifier generator.

func NewWUID

func NewWUID(name string, logger slog.Logger, opts ...Option) *WUID

NewWUID creates a new WUID instance.

func (*WUID) LoadH28FromRedis

func (w *WUID) LoadH28FromRedis(newClient NewClient, key string) error

LoadH28FromRedis adds 1 to a specific number in Redis and fetches its new value. The new value is used as the high 28 bits of all generated numbers. In addition, all the arguments passed in are saved for future renewal.

func (*WUID) Next

func (w *WUID) Next() int64

Next returns a unique identifier.

func (*WUID) RenewNow

func (w *WUID) RenewNow() error

RenewNow reacquires the high 28 bits immediately.

Jump to

Keyboard shortcuts

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