tools

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2023 License: MIT Imports: 5 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisClient

type RedisClient interface {
	Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
}

RedisClient .

type RedisLock

type RedisLock struct {
	Client RedisClient
	Key    string // resources that need to be locked
	// contains filtered or unexported fields
}

RedisLock .

func NewRedisLock

func NewRedisLock(client RedisClient, key string) (*RedisLock, error)

NewRedisLock new a redis distribute lock

func (*RedisLock) SpinLock

func (rl *RedisLock) SpinLock(ctx context.Context, retryTimes int) (bool, error)

SpinLock Loop `retryTimes` times to call TryLock

func (*RedisLock) TryLock

func (rl *RedisLock) TryLock(ctx context.Context) (bool, error)

TryLock attempt to lock, return true if the lock is successful, otherwise false

func (*RedisLock) Unlock

func (rl *RedisLock) Unlock(ctx context.Context) (bool, error)

Unlock attempt to unlock, return true if the lock is successful, otherwise false

type RedisTools

type RedisTools struct {
	Client RedisClient
}

RedisTools .

func NewTools

func NewTools(client RedisClient) *RedisTools

NewTools create a new redis tools

func (*RedisTools) Cad

func (r *RedisTools) Cad(ctx context.Context, key string, value interface{}) (bool, error)

Cad compare and delete

func (*RedisTools) Cas

func (r *RedisTools) Cas(ctx context.Context, key string, oldValue interface{},
	newValue interface{}) (bool, error)

Cas compare and swap

func (*RedisTools) CasEx

func (r *RedisTools) CasEx(ctx context.Context, key string, oldValue interface{},
	newValue interface{}, expire time.Duration) (bool, error)

CasEx compare and swap with timeout, If the timeout is 0, the timeout is not set, If the timeout is -1, keep timeout (redis >= 6.0).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL