Coder Social home page Coder Social logo

distributed-lock's Introduction

distributed-lock

100行代码 基于redis 实现的分布式锁

功能接口:

方法名称 说明
1、NewLock 新建锁对象。
2、Lock 加锁;如果为获取到锁,会阻塞。
3、Unlock 解锁。
4、Close 关闭,关闭后不能再次调用加锁解锁功能,只能再次重新申请。

使用方法:

1、创建redis对象

rdb := redis.NewClient(&redis.Options{
   Addr:     "192.168.56.105:6379",
   Password: "", // no password set
   DB:       0,  // use default DB

})

2、创建锁对象

rr, err := NewLock(rdb, "test-redis-lock")
if err != nil {
   t.Fatal(err)
}
func NewLock(rdb *redis.Client, key string) (r *redisLock, err error) 
参数说明:
rdb redis 客户端指针
key 一个锁的标识,标识不是最终的key值,实现时在后面有拼接字符标识。

3、获取锁

r.Lock()

4、释放锁

r.Unlock()

distributed-lock's People

Contributors

bzyy1024 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.