redis

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotObtained is returned when a lock cannot be obtained.
	ErrNotObtained = errors.New("redislock: not obtained")

	// ErrLockNotHeld is returned when trying to release an inactive lock.
	ErrLockNotHeld = errors.New("redislock: lock not held")
)
View Source
var (
	Nil = redis.Nil
)

Functions

func NewClient

func NewClient(r *redis.Options) (err error)

NewClient 推荐链接格式

 	&redis.Options{
	Addr:         "127.0.0.1:6379",
	Password:     "hello", // no password set
	db:           0,       // use default db
	PoolSize:     100,
	MinIdleConns: 50,
}

Types

type Client added in v0.0.6

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

Client wraps a redis client.

func New added in v0.0.6

func New(client RedisClient) *Client

New creates a new Client instance with a custom namespace.

func (*Client) Obtain added in v0.0.6

func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (*Lock, error)

Obtain tries to obtain a new lock using a key with the given TTL. May return ErrNotObtained if not successful.

type Lock added in v0.0.6

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

Lock represents an obtained, distributed lock.

func Obtain added in v0.0.6

func Obtain(ctx context.Context, client RedisClient, key string, ttl time.Duration, opt *Options) (*Lock, error)

Obtain is a short-cut for New(...).Obtain(...).

func TryLock added in v0.0.6

func TryLock(ctx context.Context, key string, client *redis.Client, exp time.Duration) (*Lock, error)

TryLock exp(ttl time)

func (*Lock) Key added in v0.0.6

func (l *Lock) Key() string

Key returns the redis key used by the lock.

func (*Lock) Metadata added in v0.0.6

func (l *Lock) Metadata() string

Metadata returns the metadata of the lock.

func (*Lock) Refresh added in v0.0.6

func (l *Lock) Refresh(ctx context.Context, ttl time.Duration, opt *Options) error

Refresh extends the lock with a new TTL. May return ErrNotObtained if refresh is unsuccessful.

func (*Lock) Release added in v0.0.6

func (l *Lock) Release(ctx context.Context) error

Release manually releases the lock. May return ErrLockNotHeld.

func (*Lock) TTL added in v0.0.6

func (l *Lock) TTL(ctx context.Context) (time.Duration, error)

TTL returns the remaining time-to-live. Returns 0 if the lock has expired.

func (*Lock) Token added in v0.0.6

func (l *Lock) Token() string

Token returns the token value set by the lock.

type Option

type Option func(r *redis.Options)

type Options added in v0.0.6

type Options struct {
	// RetryStrategy allows to customise the lock retry strategy.
	// Default: do not retry
	RetryStrategy RetryStrategy

	// Metadata string is appended to the lock token.
	Metadata string
}

Options describe the options for the lock

type Redis

type Redis struct {
	Addr string
	Type string
	Pass string
	// contains filtered or unexported fields
}

type RedisClient added in v0.0.6

type RedisClient interface {
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
	EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
	ScriptExists(ctx context.Context, scripts ...string) *redis.BoolSliceCmd
	ScriptLoad(ctx context.Context, script string) *redis.StringCmd
}

RedisClient is a minimal client interface.

type RetryStrategy added in v0.0.6

type RetryStrategy interface {
	// NextBackoff returns the next backoff duration.
	NextBackoff() time.Duration
}

RetryStrategy allows to customise the lock retry strategy.

func ExponentialBackoff added in v0.0.6

func ExponentialBackoff(min, max time.Duration) RetryStrategy

ExponentialBackoff strategy is an optimization strategy with a retry time of 2**n milliseconds (n means number of times). You can set a minimum and maximum value, the recommended minimum value is not less than 16ms.

func LimitRetry added in v0.0.6

func LimitRetry(s RetryStrategy, max int) RetryStrategy

LimitRetry limits the number of retries to max attempts.

func LinearBackoff added in v0.0.6

func LinearBackoff(backoff time.Duration) RetryStrategy

LinearBackoff allows retries regularly with customized intervals

func NoRetry added in v0.0.6

func NoRetry() RetryStrategy

NoRetry acquire the lock only once.

Jump to

Keyboard shortcuts

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