redisx

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSpinMutexConfig = SpinMutexConfig{
	Retry:   3,
	Sleep:   100 * time.Millisecond,
	Timeout: 500 * time.Millisecond,
}

DefaultSpinMutexConfig 默认一份自旋锁配置

Functions

func NewClient

func NewClient(conf map[string]interface{}) redis.UniversalClient

NewClient 从配置信息中创建client

Types

type Cache

type Cache interface {
	// Get 读取缓存, i 是读出的数据,必须是一个指针
	Get(ctx context.Context, i interface{}) error

	// Delete 删除缓存
	Delete(ctx context.Context) error

	// SetCodec 设置序列化协议
	SetCodec(c Codec)

	// SetLoadHandler 设置加载函数
	SetLoadHandler(h LoadHandler)
}

Cache 定义一个缓存的操作

func NewStringCache

func NewStringCache(client redis.UniversalClient, key string, ttl time.Duration) Cache

NewStringCache new一个StringCache,codec 默认使用

type Codec

type Codec interface {
	// Unmarshal 反序列化
	Unmarshal(data []byte, v interface{}) error

	// Marshal 序列化
	Marshal(v interface{}) ([]byte, error)
}

Codec 是缓存结构序列化的抽象定义

type LoadHandler

type LoadHandler func(context.Context) (interface{}, error)

LoadHandler 是 Get获取不到缓存时,应该调用的加载的函数

type SpinMutex

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

SpinLock 自旋锁

func NewSpinMutex

func NewSpinMutex(client redis.UniversalClient, key string, config SpinMutexConfig) *SpinMutex

NewSpinLock 创建一个重试锁

func (*SpinMutex) Lock

func (s *SpinMutex) Lock() error

Lock 锁定

func (*SpinMutex) TryLock

func (s *SpinMutex) TryLock() bool

TryLock 尝试锁

func (*SpinMutex) Unlock

func (s *SpinMutex) Unlock() error

Unlock 解锁

type SpinMutexConfig

type SpinMutexConfig struct {
	Retry   int           // 重试次数
	Sleep   time.Duration // 每次等待时间
	Timeout time.Duration // 持有锁最长时间
}

SpinMutexConfig 自旋锁配置 * Retry 定义锁重试次数,到达次数还无法获得锁,则抛出retry to out * Sleep 每次重试后,阻塞时间,建议不要太长 * Timout 持有锁的最长时间,即每次事务最长执行时间

type StringCache

type StringCache struct {
	Codec       Codec       // 序列化协议
	LoadHandler LoadHandler // 加载函数
	// contains filtered or unexported fields
}

StringCache 使用redis的string结构作缓存的结构体

func (*StringCache) Delete

func (s *StringCache) Delete(ctx context.Context) error

Delete 删除缓存

func (*StringCache) Get

func (s *StringCache) Get(ctx context.Context, i interface{}) error

Get 读取缓存

func (*StringCache) SetCodec

func (s *StringCache) SetCodec(c Codec)

SetCodec 设置Codec

func (*StringCache) SetLoadHandler

func (s *StringCache) SetLoadHandler(h LoadHandler)

SetLoadHandler 设置LoadHandler

Jump to

Keyboard shortcuts

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