idempotency

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(cfg Config, rdb redis.UniversalClient)

Must 初始化幂等性服务工厂(必须调用)

Types

type BusinessConfig

type BusinessConfig struct {
	KeyPrefix  string        `json:"key_prefix"`
	Expiration time.Duration `json:"expiration"`
}

BusinessConfig 业务配置

type Config

type Config struct {
	// 本地缓存大小(字节)
	LocalCacheSize int `json:"local_cache_size"`
	// 各业务配置
	BusinessConfigs map[string]BusinessConfig `json:"business_configs"`
}

Config 配置

type Factory

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

Factory 幂等性服务工厂

func NewFactory

func NewFactory(cfg Config, rdb redis.UniversalClient) *Factory

NewFactory 创建幂等性服务工厂

func (*Factory) GetService

func (f *Factory) GetService(businessType string) *IdempotencyService

GetService 获取指定业务的幂等性服务

type IdempotencyService

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

IdempotencyService 提供幂等性检查服务

func NewIdempotencyService

func NewIdempotencyService(redisClient redis.UniversalClient, keyPrefix string, size int, expiration time.Duration) *IdempotencyService

NewIdempotencyService 创建幂等性服务实例

func Service

func Service(businessType string) *IdempotencyService

Service 获取指定业务的幂等性服务(全局方法)

func (*IdempotencyService) CheckIdempotency

func (s *IdempotencyService) CheckIdempotency(ctx context.Context, requestID string, data interface{}) (bool, error)

CheckIdempotency 检查操作是否重复 requestID: 请求唯一标识符(如订单号) data: 请求数据(用于生成唯一键) 返回值: - bool: true 表示请求是新的(未重复),false 表示请求重复 - error: 操作过程中的错误

func (*IdempotencyService) DeleteIdempotencyKey

func (s *IdempotencyService) DeleteIdempotencyKey(ctx context.Context, requestID string, data interface{})

DeleteIdempotencyKey 删除幂等性键 用于处理失败时清理已设置的键

func (*IdempotencyService) GetCacheStats

func (s *IdempotencyService) GetCacheStats() string

GetCacheStats 获取缓存使用情况

Jump to

Keyboard shortcuts

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