Skip to content

me-cs/goRedisson

Repository files navigation

goRedisson

Redisson go implementation

Go codecov Release Go Report Card Go Reference License: 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"
)

func main() {
	// create redis client
	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
	}

	//Your business code

	err = lock.Unlock()
	if err != nil {
		log.Print(err)
		return
	}

	return
}

rwlock

package main

import (
	"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

package main

import (
	"context"
	"log"
	"sync"
	"time"

	"github.com/me-cs/goRedisson"
	"github.com/redis/go-redis/v9"
)

func main() {
	// create redis client
	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
	}

	//Your business code

	err = mutex.Unlock()
	if err != 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!