rdidgen

package
v0.0.0-...-93b1e75 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package rdidgen provides implementation of id generation using redis server.

Typical usage:

idgen := rdidgen.MustNew(
    rdidgen.WithRedisClient(client),
    rdidgen.WithKeyPrefix("object"),
)

id, err := idgen.NextID()
if err != nil {
    ...
}

Index

Constants

View Source
const DefKeyPrefix = "entity"

DefKeyPrefix is the default prefix of the redis key that stores next id.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config structure with generator's configuration. Shouldn't be created manually.

type ConfigService

type ConfigService interface {
	GetByKey(string, interface{}) error
	IsNoSuchKeyError(error) bool
}

ConfigService interface used to obtain configuration from somewhere into some specific structure.

type IDGen

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

IDGen structure provides id generation functionality using redis client. Don't create manually, use the functions down below instead.

func MustNew

func MustNew(opts ...Option) *IDGen

MustNew function creates new redis id generator with provided options and panics on any error.

func New

func New(opts ...Option) (*IDGen, error)

New function creates new redis id generator with provided options.

func (*IDGen) Key

func (idGen *IDGen) Key() string

Key method retrieves redis key that stores next id.

func (*IDGen) NextID

func (idGen *IDGen) NextID() (int64, error)

NextID method increments and retrieves the next id.

type Option

type Option func(*Config) error

Option function that is fed to New and MustNew. Obtain them using 'With' functions down below.

func WithConfig

func WithConfig(service ConfigService, key string) Option

WithConfig option retrieves configuration from provided configuration service.

Example JSON configuration with all possible fields (if some are not present, defaults will be used):

{
    "keyPrefix": "user"
}

func WithKeyPrefix

func WithKeyPrefix(keyPrefix string) Option

WithKeyPrefix option applies provided redis key prefix that stores next id. Default: "entity".

func WithRedisClient

func WithRedisClient(redisClient *redis.Client) Option

WithRedisClient option applies provided instance of redis client. If not provided, the default client will be used.

Jump to

Keyboard shortcuts

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