redislock

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2025 License: MIT Imports: 8 Imported by: 0

README

redislock

Redis 分布式锁

Examples

package main

import (
	"time"

	"github.com/gomooth/locker/lockhelper"
	"github.com/gomooth/locker/redislock"
	"github.com/redis/go-redis/v9"
)

var client *redis.Client

func init() {
	client = redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "",
		DB:       0,
	})
}

func main() {
	lock := redislock.New(client, redislock.WithTimeout(15*time.Minute))

	key := lockhelper.Key("app", "package", "business")
	if err := lock.Lock(key); err != nil {
		panic(err)
	}
	defer func() {
		_ = lock.UnLock(key)
	}()

	// do something ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(client *redis.Client, opts ...func(*distributedRedisLock)) locker.ILocker

New 创建分布式 redis 锁

func WithTimeout

func WithTimeout(expire time.Duration) func(*distributedRedisLock)

Types

This section is empty.

Jump to

Keyboard shortcuts

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