https://github.com/xdpcs/redis-lock
Distributed lock based on redis.
https://github.com/xdpcs/redis-lock
Last synced: about 1 year ago
JSON representation
Distributed lock based on redis.
- Host: GitHub
- URL: https://github.com/xdpcs/redis-lock
- Owner: XdpCs
- License: mit
- Created: 2023-07-13T06:42:05.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-29T12:47:58.000Z (almost 3 years ago)
- Last Synced: 2025-05-04T22:45:44.510Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-lock






Distributed lock based on [redis](https://redis.io/docs/manual/patterns/distributed-locks/).
redis-lock supports watchdog mechanism in [redisson](https://github.com/redisson/redisson).
## install
`go get`
```shell
go get -u github.com/XdpCs/redis-lock
```
`go mod`
```shell
require github.com/XdpCs/redis-lock latest
```
## example
Error handling is simplified to panic for shorter example.
You can run this program in this [directory](./example/main.go).
```go
package main
import (
"context"
"time"
redislock "github.com/XdpCs/redis-lock"
"github.com/redis/go-redis/v9"
)
func main() {
// init context
ctx := context.Background()
// init redis client
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
// close redis client
defer rdb.Close()
// flush redis
_ = rdb.FlushDB(ctx).Err()
// init redislock client
client, err := redislock.NewDefaultClient(rdb)
if err != nil {
panic(err)
}
// try lock with default parameter
mutex, err := client.TryLock(ctx, "XdpCs", -1)
if err != nil {
panic(err)
}
defer func(mutex *redislock.Mutex, ctx context.Context) {
// unlock mutex
err := mutex.Unlock(ctx)
if err != nil {
panic(err)
}
}(mutex, ctx)
time.Sleep(time.Second * 30)
}
```
## License
redis-lock is under the [MIT](LICENSE). Please refer to LICENSE for more information.