Skip to content

Latest commit

History

149 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

goRedisson

Redisson go implementation

GocodecovReleaseGo Report CardGo ReferenceLicense: MIT

Description

redis mutex rwmutex go implementation with watchdog

English | 简体中文

Example use:

lock

package main
import (
"context""log""sync""time""github.com/me-cs/goRedisson""github.com/redis/go-redis/v9"
)
funcmain() {
// create redis clientredisDB:=redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password setDB: 0, // use default DB
})
deferredisDB.Close()
g:=goRedisson.NewGoRedisson(redisDB)
lock:=g.GetLock("example")
err:=lock.Lock()
iferr!=nil {
log.Print(err)
return
}
//Your business codeerr=lock.Unlock()
iferr!=nil {
log.Print(err)
return
}
return
}

rwlock

package main
import (
"context""sync""time""github.com/me-cs/goRedisson""github.com/redis/go-redis/v9"
)
funcmain() {
redisDB:=redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password setDB: 0, // use default DB
})
deferredisDB.Close()
g:=goRedisson.NewGoRedisson(redisDB)
l:=g.GetReadWriteLock("testRwMutest")
a:=0wg:= sync.WaitGroup{}
wg.Add(2)
gofunc() {
deferwg.Done()
innerWg:= sync.WaitGroup{}
fori:=0; i<100; i++ {
innerWg.Add(1)
gofunc() {
deferinnerWg.Done()
ctx, cancel:=context.WithTimeout(context.Background(), 5*time.Second)
defercancel()
err:=l.WriteLock().LockContext(ctx)
iferr!=nil {
panic(err)
}
a++err=l.WriteLock().Unlock()
iferr!=nil {
panic(err)
}
}()
}
innerWg.Wait()
}()
gofunc() {
deferwg.Done()
innerWg:= sync.WaitGroup{}
fori:=0; i<100; i++ {
innerWg.Add(1)
gofunc() {
deferinnerWg.Done()
ctx, cancel:=context.WithTimeout(context.Background(), 5*time.Second)
defercancel()
err:=l.ReadLock().LockContext(ctx)
iferr!=nil {
panic(err)
}
err=l.ReadLock().Unlock()
iferr!=nil {
panic(err)
}
}()
}
innerWg.Wait()
}()
wg.Wait()
ifa!=100 {
panic(a)
}
return
}

mutex

package main
import (
"context""log""sync""time""github.com/me-cs/goRedisson""github.com/redis/go-redis/v9"
)
funcmain() {
// create redis clientredisDB:=redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password setDB: 0, // use default DB
})
deferredisDB.Close()
g:=goRedisson.NewGoRedisson(redisDB)
mutex:=g.GetMutex("example")
err:=mutex.Lock()
iferr!=nil {
log.Print(err)
return
}
//Your business codeerr=mutex.Unlock()
iferr!=nil {
log.Print(err)
return
}
}

Contributing

Contributing is done with commit code. There is no help that is too small! :)

If you wish to contribute to this project, please branch and issue a pull request against master ("GitHub Flow")

Give a Star! ⭐

If you like or are using this project to learn or solve real problems through this project. please give it a star. Thanks!

About

Redisson golang implementation

Topics

Resources

Code of conduct

Contributing

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages