Popularity
7.6
Growing
Activity
4.6
-
1,293
15
149

Programming language: Go
License: GNU General Public License v3.0 or later
Latest version: v0.7.1

redis-lock alternatives and similar packages

Based on the "Distributed Systems" category.
Alternatively, view redis-lock alternatives based on common mentions on social networks and blogs.

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

Add another 'Distributed Systems' Package

README

redislock

Test GoDoc 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/v9"
)

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)

    ctx := context.Background()

    // Try to obtain lock.
    lock, err := locker.Obtain(ctx, "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(ctx)
    fmt.Println("I have a lock!")

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

    // Extend my lock.
    if err := lock.Refresh(ctx, 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(ctx); 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.