Code Monkey home page Code Monkey logo

redis-tools's Introduction

redis-tools

Go Report Card GitHub top language GitHub CodeFactor go_version

redis-tools is a collection of redis tools, including distributed lock, cas, casEx, cad .

Quick Start

Fisrt, create a demo and import the redis-tools and redis client :

> go mod init demo

> go get github.com/zehuamama/redis-tools
> go get github.com/go-redis/redis/v8

Distributed lock

The trylock case :

package main

import (
	"context"
	"log"

	"github.com/go-redis/redis/v8"
	tools "github.com/zehuamama/redis-tools"
)

func main() {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	disLock, err := tools.NewRedisLock(client, "lock resource")
	if err != nil {
		log.Fatal(err)
	}

	succ, err := disLock.TryLock(context.Background())
	if err != nil {
		log.Println(err)
        return
	}

	if succ {
		defer disLock.Unlock(context.Background())
	}
}

and spinlock case :

    succ, err := disLock.SpinLock(context.Background(), 5)  // retry 5 times
	if err != nil {
		log.Println(err)
        return
	}

	if succ {
		defer disLock.Unlock(context.Background())
	}

Redis Tools

compare and swap case :

func main() {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	succ, err := tools.NewTools(client).Cas(context.Background(), "cas_key", "old value", "new value")
	if err != nil {
		log.Println(err)
		return
	}

    ...
}

and compare and delete case :

    succ, err := tools.NewTools(client).Cad(context.Background(), "cas_key", "old value")
	if err != nil {
		log.Println(err)
		return
	}

Contributing

If you are intersted in contributing to redis-tools, please see here: CONTRIBUTING

License

redis-tools is licensed under the term of the BSD 2-Clause License

redis-tools's People

Contributors

zehuamama avatar qianyuyu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.