redislock

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

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

Go to latest
Published: May 26, 2020 License: MIT Imports: 5 Imported by: 0

README

Distribute Lock In Redis

RedisLock is a distribute lock tool written in Go (Golang). If you need performance and good productivity, you will love RedisLock.

Installation

To install RedisLock package, you need to install Go and set your Go workspace first.

  1. The first need Go installed , then you can use the below Go command to install Gin.
$ go get -u github.com/stone-stones/redislock 
  1. Import it in your code:
import "github.com/stone-stones/redislock "
  1. (Optional) Import github.com/go-redis/redis. This is required to connect redis.
import "github.com/go-redis/redis"
package main

import (
	"log"
	"time"

	"github.com/go-redis/redis"
	"github.com/stone-stones/redislock"
)

func main() {
	cli = redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "",
		DB:       0,
	})
	rlock := redislock.NewRedisLock(cli)(cli)
	//lock take three params,lock key,default expiration time,auto refresh key expiration time before unlock the key
	err := rlock.Lock("test_key2", 10*time.Second, true)
	if err != nil {
		log.Error(err)
	}
	err = rlock.Unlock()
	if err != nil {
		log.Error(err)
	}
}

Documentation

Index

Constants

View Source
const (
	//SUC relaese lock success
	SUC = iota
	//ERR relaese lock failed
	ERR
)

Variables

View Source
var (
	//NILClient redis client init error or close
	NILClient = errors.New("nil client")
	//LOCKFailed get lock failed
	LOCKFailed = errors.New("set key failed")
	//UNLOCKVALUEERROR unlock value not match set value
	UNLOCKVALUEERROR = errors.New("unlock value error")
	//UNLOCKKEYNOTFOUND unlock key not found
	UNLOCKKEYNOTFOUND = errors.New("unlock key not found error")
)

Functions

This section is empty.

Types

type RedisLock

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

RedisLock base struct

func NewRedisLock

func NewRedisLock(cli *redis.Client) *RedisLock

NewRedisLock init a new RedisLock struct

func (*RedisLock) Lock

func (r *RedisLock) Lock(key string, timeOut time.Duration, renew bool) error

Lock set redis lock

func (*RedisLock) SetContext

func (r *RedisLock) SetContext(ctx context.Context)

SetContext set context,so can use cancle function to canle lock

func (*RedisLock) TTL

func (r *RedisLock) TTL() time.Duration

TTL get lock remain time -1: no expiration time -2: key not exists

func (*RedisLock) UnLock

func (r *RedisLock) UnLock() error

UnLock releaselock

Jump to

Keyboard shortcuts

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