cache

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

简单 string 分布式锁操作

分布式锁资源管理 Redis str

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrHookProgress4RedisLock

type ErrHookProgress4RedisLock func(item *RedisDistributedRenewalAndUnlock, err error)

type LocalCache

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

本地缓存结构体

func NewInstance

func NewInstance(cap uint) *LocalCache

func (*LocalCache) Clear

func (b *LocalCache) Clear(ctx context.Context, errHonk types.ErrHookProgress)

func (*LocalCache) Delete

func (b *LocalCache) Delete(ctx context.Context, key string) error

func (*LocalCache) Destroy

func (b *LocalCache) Destroy(ctx context.Context)

func (*LocalCache) Get

func (b *LocalCache) Get(ctx context.Context, key string) (any, error)

func (*LocalCache) Set

func (b *LocalCache) Set(ctx context.Context, key string, value any, expiration time.Duration) error

type RedisCache

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

RedisCache类

func NewRedisCache

func NewRedisCache(client redis.Cmdable, dbIndex uint8) *RedisCache

构造函数

func (*RedisCache) Clear

func (r *RedisCache) Clear(ctx context.Context, errHonk types.ErrHookProgress)

慎用,清除掉此库的所有缓存资源

func (*RedisCache) Delete

func (r *RedisCache) Delete(ctx context.Context, key string) error

func (*RedisCache) Destroy

func (r *RedisCache) Destroy(ctx context.Context)

关闭Redis连接

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) (any, error)

func (*RedisCache) GetRedis

func (r *RedisCache) GetRedis() (*redis.Cmdable, uint8)

获得Redis客户端句柄

func (*RedisCache) HDelete

func (r *RedisCache) HDelete(ctx context.Context, hashName string, key string) error

func (*RedisCache) HExpire

func (r *RedisCache) HExpire(ctx context.Context, hashName string, expiration time.Duration) error

func (*RedisCache) HGet

func (r *RedisCache) HGet(ctx context.Context, hashName string, key string) (any, error)

func (*RedisCache) HGetAll

func (r *RedisCache) HGetAll(ctx context.Context, hashName string) (map[string]string, error)

func (*RedisCache) HKeys

func (r *RedisCache) HKeys(ctx context.Context, hashName string) ([]string, error)

func (*RedisCache) HLength

func (r *RedisCache) HLength(ctx context.Context, hashName string) (int64, error)

func (*RedisCache) HSet

func (r *RedisCache) HSet(ctx context.Context, hashName string, key string, value any) error

func (*RedisCache) LLength

func (r *RedisCache) LLength(ctx context.Context, listName string) (int64, error)

func (*RedisCache) ListPop

func (r *RedisCache) ListPop(ctx context.Context, listName string) (string, error)

func (*RedisCache) ListPush

func (r *RedisCache) ListPush(ctx context.Context, listName string, value ...any) error

func (*RedisCache) ListShift

func (r *RedisCache) ListShift(ctx context.Context, listName string) (string, error)

func (*RedisCache) ListUnshift

func (r *RedisCache) ListUnshift(ctx context.Context, listName string, value ...any) error

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, value any, expiration time.Duration) error

type RedisDistributeLockResManager

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

加锁资源 --------------------------------------------------------------------------------------------------------------

func GetInstallRedisDistributeLockResManager

func GetInstallRedisDistributeLockResManager() *RedisDistributeLockResManager

Redis 分布式锁资源管理

func (*RedisDistributeLockResManager) Get

func (*RedisDistributeLockResManager) Put

type RedisDistributeRenewalAndUnlockResManager

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

续约/解锁资源 ----------------------------------------------------------------------------------------------------------

func GetInstallRedisDistributeRenewalAndUnlockResManager

func GetInstallRedisDistributeRenewalAndUnlockResManager() *RedisDistributeRenewalAndUnlockResManager

func (*RedisDistributeRenewalAndUnlockResManager) Get

func (*RedisDistributeRenewalAndUnlockResManager) Put

type RedisDistributedLock

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

分布式锁 str 处理

func (*RedisDistributedLock) AutoLock

阻塞式加锁重试方法

func (*RedisDistributedLock) AutoLockWithHook

func (rl *RedisDistributedLock) AutoLockWithHook(
	ctx context.Context,
	key string,
	expiration time.Duration,
	timeout time.Duration,
	retry types.FixIntervalRetry,
	callback ErrHookProgress4RedisLock,
)

可以尝试多次加锁,避免一次锁定失败 API ***************************************************************************************************

func (*RedisDistributedLock) AutoSingleFightLock

func (rl *RedisDistributedLock) AutoSingleFightLock(
	ctx context.Context,
	key string,
	expiration time.Duration,
	timeout time.Duration,
	retry *types.FixIntervalRetry,
) (*RedisDistributedRenewalAndUnlock, error)

自动singleflight加锁(非常高并发并且热点集中的极端情况下使用,慎用)

func (*RedisDistributedLock) Clear

func (rl *RedisDistributedLock) Clear()

func (*RedisDistributedLock) Reset

func (rl *RedisDistributedLock) Reset(client redis.Cmdable)

重置

type RedisDistributedRenewalAndUnlock

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

续约和释放锁处理(Redis分布式锁)----------------------------------------------------------------------------------------------

func (*RedisDistributedRenewalAndUnlock) AutoRenewal

func (rl *RedisDistributedRenewalAndUnlock) AutoRenewal(
	interval time.Duration,
	timeout time.Duration,
) error

自动续约阻塞版本, 一直续约中知道主动的释放锁

func (*RedisDistributedRenewalAndUnlock) AutoRenewalWithHook

func (l *RedisDistributedRenewalAndUnlock) AutoRenewalWithHook(
	maxRenewingCnt uint,
	expiration time.Duration,
	timeout time.Duration,
	everyRetry types.FixIntervalRetry,
	callback types.ErrHookProgress,
)

自动续约要进行原子操作: 考虑lua, hook版 maxRenewingCnt 续约的此时 expiration 间隔多长时间开始续约 timeout 超时 everyRetry 每次续约的重试策略 callback 钩子函数

func (*RedisDistributedRenewalAndUnlock) Clear

func (*RedisDistributedRenewalAndUnlock) Renewal

续约要进行原子操作: 考虑lua, 只能续约1次

func (*RedisDistributedRenewalAndUnlock) Reset

func (rl *RedisDistributedRenewalAndUnlock) Reset(client redis.Cmdable, key string, value string, expiration time.Duration)

func (*RedisDistributedRenewalAndUnlock) UnLock

解锁要进行原子操作: 考虑lua Api ***********************************************************************

Jump to

Keyboard shortcuts

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