idempotent

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

Idempotent

api idempotent tool based on redis lua script.

Usage

go get -u github.com/snail-plus/gopkg/idempotent
import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
  "github.com/snail-plus/gopkg/idempotent"
)

func main() {
	client := redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
		DB:   0,
	})
	i := idempotent.New(
		idempotent.WithRedis(client),
	)

	token := i.Token(context.Background())
	fmt.Println(i.Check(context.Background(), token))
	// true

	// second check will fail
	fmt.Println(i.Check(context.Background(), token))
	// false
}

Options

  • WithRedis - redis client, default 127.0.0.1:6379
  • WithPrefix - cache key prefix, default idempotent
  • WithExpire - key expire time, default 60 minute

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithExpire

func WithExpire(min int) func(*Options)

func WithPrefix

func WithPrefix(prefix string) func(*Options)

func WithRedis

func WithRedis(rd redis.UniversalClient) func(*Options)

Types

type Idempotent

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

func New

func New(options ...func(*Options)) *Idempotent

func (*Idempotent) Check

func (i *Idempotent) Check(ctx context.Context, token string) bool

func (*Idempotent) Token

func (i *Idempotent) Token(ctx context.Context) string

type Options

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

Jump to

Keyboard shortcuts

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