https://github.com/me-cs/goredisson
Redisson golang implementation
https://github.com/me-cs/goredisson
cache distributed distributed-locks go lock redis watchdog
Last synced: 5 months ago
JSON representation
Redisson golang implementation
- Host: GitHub
- URL: https://github.com/me-cs/goredisson
- Owner: me-cs
- License: mit
- Created: 2022-09-23T09:50:57.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-21T08:40:36.000Z (7 months ago)
- Last Synced: 2025-04-06T17:06:39.356Z (6 months ago)
- Topics: cache, distributed, distributed-locks, go, lock, redis, watchdog
- Language: Go
- Homepage:
- Size: 131 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README-CN.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# goRedisson
*Redisson go语言实现*
[](https://github.com/me-cs/goRedisson/actions)
[](https://codecov.io/gh/me-cs/goRedisson)
[](https://github.com/me-cs/goRedisson)
[](https://goreportcard.com/report/github.com/me-cs/goRedisson)
[](https://pkg.go.dev/github.com/me-cs/goRedisson)
[](https://opensource.org/licenses/MIT)## 项目描述
带有看门狗的 redis 互斥锁/读写锁 的go语言实现[English](README.md) | 简体中文
### 示例:
lock
```go
package mainimport (
"context"
"log"
"sync"
"time""github.com/me-cs/goRedisson"
"github.com/redis/go-redis/v9"
)func main() {
// 创建redis客户端
redisDB := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
defer redisDB.Close()g := goRedisson.NewGoRedisson(redisDB)
lock := g.GetLock("example")
err := lock.Lock()
if err != nil {
log.Print(err)
return
}// 你的业务代码
err = lock.Unlock()
if err != nil {
log.Print(err)
return
}return
}```
rwlock
```go
package mainimport (
"context"
"sync"
"time""github.com/me-cs/goRedisson"
"github.com/redis/go-redis/v9"
)func main() {
redisDB := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
defer redisDB.Close()g := goRedisson.NewGoRedisson(redisDB)
l := g.GetReadWriteLock("testRwMutest")
a := 0
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
innerWg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
innerWg.Add(1)
go func() {
defer innerWg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := l.WriteLock().LockContext(ctx)
if err != nil {
panic(err)
}
a++
err = l.WriteLock().Unlock()
if err != nil {
panic(err)
}
}()
}
innerWg.Wait()
}()go func() {
defer wg.Done()
innerWg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
innerWg.Add(1)
go func() {
defer innerWg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := l.ReadLock().LockContext(ctx)
if err != nil {
panic(err)
}
err = l.ReadLock().Unlock()
if err != nil {
panic(err)
}
}()
}
innerWg.Wait()
}()wg.Wait()
if a != 100 {
panic(a)
}return
}```
mutex
```go
package mainimport (
"context"
"log"
"sync"
"time""github.com/me-cs/goRedisson"
"github.com/redis/go-redis/v9"
)func main() {
// 创建redis 客户端
redisDB := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
defer redisDB.Close()g := goRedisson.NewGoRedisson(redisDB)
mutex := g.GetMutex("example")
err := mutex.Lock()
if err != nil {
log.Print(err)
return
}// 你的业务代码
err = mutex.Unlock()
if err != nil {
log.Print(err)
return
}}
```## 贡献
贡献是通过提交代码完成的。没有什么帮助是太小的!:)如果你想为这个项目做贡献,请在主干线上分支并发出拉动请求 ("[GitHub Flow](https://guides.github.com/introduction/flow/)")