Popularity
5.7
Growing
Activity
4.0
Declining
222
4
40

Programming language: Go
Latest version: v0.6.0

redis-lock alternatives and similar packages

Based on the "Distributed Systems" category

Do you think we are missing an alternative of redis-lock or a related project?

Add another 'Distributed Systems' Package

README

redislock

Build Status GoDoc Go Report Card License

Simplified distributed locking implementation using Redis. For more information, please see examples.

Examples

import (
  "fmt"
  "time"

  "github.com/bsm/redislock"
  "github.com/go-redis/redis/v8"
)

func main() {
    // Connect to redis.
    client := redis.NewClient(&redis.Options{
        Network:    "tcp",
        Addr:       "127.0.0.1:6379",
    })
    defer client.Close()

    // Create a new lock client.
    locker := redislock.New(client)

    // Try to obtain lock.
    lock, err := locker.Obtain("my-key", 100*time.Millisecond, nil)
    if err == redislock.ErrNotObtained {
        fmt.Println("Could not obtain lock!")
    } else if err != nil {
        log.Fatalln(err)
    }

    // Don't forget to defer Release.
    defer lock.Release()
    fmt.Println("I have a lock!")

    // Sleep and check the remaining TTL.
    time.Sleep(50 * time.Millisecond)
    if ttl, err := lock.TTL(); err != nil {
        log.Fatalln(err)
    } else if ttl > 0 {
        fmt.Println("Yay, I still have my lock!")
    }

    // Extend my lock.
    if err := lock.Refresh(100*time.Millisecond, nil); err != nil {
        log.Fatalln(err)
    }

    // Sleep a little longer, then check.
    time.Sleep(100 * time.Millisecond)
    if ttl, err := lock.TTL(); err != nil {
        log.Fatalln(err)
    } else if ttl == 0 {
        fmt.Println("Now, my lock has expired!")
    }

}

Documentation

Full documentation is available on GoDoc


*Note that all licence references and agreements mentioned in the redis-lock README section above are relevant to that project's source code only.