redislock

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 4 Imported by: 0

README

go-redislock

Go Release Report Doc License

介绍

使用go编写的基于Redis实现的分布式锁

快速开始

安装
go get -u github.com/fuliang10000/go-redislock
测试
go test github.com/fuliang10000/go-redislock  -v -cover
Use Demo
package main

import (
	"context"
	"fmt"
	redisLock "github.com/fuliang10000/go-redislock"
	"github.com/go-redis/redis/v8"
	"time"
)

func main() {
	var opt = &redis.Options{
		Addr:     "localhost:6379",
		Password: "a123456",
		DB:       0,
	}
	// 获取客户端
	client := redisLock.NewClient(context.Background(), redis.NewClient(opt))

	lockKey := "my_lock_key"
	// 获取锁
	locked := client.Lock(lockKey, 10*time.Second)
	if locked {
		// 释放锁
		defer client.UnLock(lockKey)
		// 执行业务逻辑
		fmt.Println("my work...")
	} else {
		panic("system is busy")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v0.0.5

type Client struct {
	Ctx context.Context
	// contains filtered or unexported fields
}

func NewClient added in v0.0.5

func NewClient(ctx context.Context, redis *redis.Client) *Client

NewClient 获取客户端

func (*Client) Lock added in v0.0.5

func (s *Client) Lock(lockKey string, expiration time.Duration) bool

Lock 获取锁

func (*Client) UnLock added in v0.0.5

func (s *Client) UnLock(lockKey string)

UnLock 释放锁

Jump to

Keyboard shortcuts

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