redistool

package
v0.0.0-...-5a65518 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rand

func Rand() string

Rand returns a random string.

func RandId

func RandId() string

RandId returns a random id string.

func Randn

func Randn(n int) string

Randn returns a random string with length n.

func Seed

func Seed(seed int64)

Seed sets the seed to seed.

Types

type Client

type Client struct {
	redis.UniversalClient
}

func New

func New(c Config) *Client

func (*Client) Get

func (r *Client) Get(ctx context.Context, key string) (res string, err error)

func (*Client) HSetFromStruct

func (r *Client) HSetFromStruct(ctx context.Context, key string, data interface{}) *redis.IntCmd

HSetFromStruct 把struct按hash结构存入redis

func (*Client) HSetFromStructByPip

func (r *Client) HSetFromStructByPip(ctx context.Context, pip *redis.Pipeliner, key string, data interface{}) *redis.IntCmd

HSetFromStructByPip 使用pipeline把struct按hash结构存入redis

func (*Client) WrapDoWithTracing

func (r *Client) WrapDoWithTracing(ctx context.Context, spanName string, fn func(ctx context.Context, span trace.Span) error)

WrapDoWithTracing 使用链路追踪

@spanName: 链路名称

@fn: func(ctx context.Context, span trace.Span)

example:

cli.WrapDoWithTracing(context.Background(), "redis.Get", func(ctx context.Context, span trace.Span) {
		r, err := cli.Get(ctx, "KEY")
		if err != nil {
			logger.GetLogger().Error(ctx, "redis.Get error", err)
			span.RecordError(err)
			return
		}
		t.Log("result:", r)
	})

type Config

type Config struct {
	Addr             []string
	DB               int
	UserName         string
	Password         string
	MasterName       string //when redis sentinel
	SentinelPassword string //when redis sentinel
}

type Locker

type Locker struct {
	IsDebug bool
	// contains filtered or unexported fields
}

A Locker is a redis lock.

func NewRedisLock

func NewRedisLock(store redis.UniversalClient, key string) *Locker

NewRedisLock returns a Locker.

func NewRedisLockWithContext

func NewRedisLockWithContext(ctx context2.Context, store redis.UniversalClient, key string) *Locker

NewRedisLockWithContext returns a Locker.

func (*Locker) Acquire

func (rl *Locker) Acquire(repeat int, wait time.Duration) (bool, error)

Acquire acquires the lock.

在获取锁之前可调用SetExpire()设置锁时间,默认锁10s

@repeat 重试次数,默认不重试

@wait 重试(repeat>1)时,重试的间隔时间 默认10ms

func (*Locker) AcquireBackoff

func (rl *Locker) AcquireBackoff(repeat int, baseDelay, maxDelay time.Duration) (bool, error)

AcquireBackoff acquires the lock using 指数退避策略.

在获取锁之前可调用SetExpire()设置锁时间,默认锁10s

@repeat 重试次数,默认不重试

@baseDelay 重试(repeat>1)时,基础延迟时间

@maxDelay 重试(repeat>0)时,最大延迟时间,默认10ms

func (*Locker) Release

func (rl *Locker) Release() (bool, error)

Release releases the lock.

func (*Locker) SetExpire

func (rl *Locker) SetExpire(seconds int)

SetExpire sets the expiration.

Jump to

Keyboard shortcuts

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