rlock

package module
v0.0.0-...-b046085 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 7 Imported by: 0

README

Rlock

Build Status

Implementation of redis lock

⚙️ Installation

go get -u github.com/eininst/rlock

⚡ Quickstart

func init(){
    rlock.SetDefault(getRedis())
}

func main() {
    ok, cancel := rlock.Acquire("lock_name_test", time.Second*10)
    defer cancel()
    if ok {
        fmt.Println("my is safe")
    }
}

👀 New a Instance

func main() {
    lk := rlock.New(getRedis())
    
    ok, cancel := lk.Acquire("lock_name_test", time.Second*10)
    defer cancel()
    if ok {
        fmt.Println("my is safe")
    }
}

See examples

License

MIT

Documentation

Index

Constants

View Source
const LOCK_DEL = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
    return redis.call("DEL", KEYS[1])
else
    return 0
end`

Variables

This section is empty.

Functions

func SetDefault

func SetDefault(rcli *redis.Client, cfgs ...Config)

Types

type CancelFunc

type CancelFunc func() bool

func Acquire

func Acquire(lockName string, timeout time.Duration) (bool, CancelFunc)

func TryAcquire

func TryAcquire(lockName string, expire time.Duration) (bool, CancelFunc)

type Config

type Config struct {
	Prefix string
}

type Rlock

type Rlock struct {
	Config
	// contains filtered or unexported fields
}
var (
	DefaultInstance *Rlock

	LockInterval = time.Millisecond * 4
)

func New

func New(rcli *redis.Client, cfgs ...Config) *Rlock

func (*Rlock) Acquire

func (rlock *Rlock) Acquire(lockName string, timeout time.Duration) (bool, CancelFunc)

func (*Rlock) TryAcquire

func (rlock *Rlock) TryAcquire(lockName string, expire time.Duration) (bool, CancelFunc)

type RlockContext

type RlockContext struct {
	Ctx context.Context
	Key string
	Val string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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