util.go 2.95 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package redisCli

import (
	"context"
chenweijian's avatar
chenweijian committed
5
	"git.hilo.cn/hilo-common/myerr"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/mylogrus"
chenweijian's avatar
chenweijian committed
7
	"github.com/go-redis/redis/v8"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	"time"
)

//这个用户避免多个服务器并发问题。
func SetNX(key string, value interface{}, expiration time.Duration, callBack func()) {
	flag, err := RedisClient.SetNX(context.Background(), key, value, expiration).Result()
	if err != nil {
		mylogrus.MyLog.Errorf("key:%v lock start setNx err: %v", key, err)
	}
	if !flag {
		mylogrus.MyLog.Infof("key:%v lock setNx has lock", key)
		return
	}
	mylogrus.MyLog.Infof("key:%v lock setNx begin", key)
	callBack()
	//执行结束之后,移除key
	//RedisClient.Del(context.Background(), key)
	mylogrus.MyLog.Infof("key:%v lock setNx end", key)
}

func Lock(key string, expiration time.Duration) bool {
	flag, err := RedisClient.SetNX(context.Background(), key, 1, expiration).Result()
	if err != nil {
		return false
	}
	if !flag {
		return false
	}
	return true
}
chenweijian's avatar
chenweijian committed
38

hujiebin's avatar
hujiebin committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// 尝试获取锁
func TryLock(key string, expiration time.Duration) bool {
	deadline := time.Now().Add(expiration)
	for {
		if time.Now().After(deadline) {
			return false // 超时无法获取锁
		}
		if Lock(key, expiration) {
			return true
		}
		time.Sleep(time.Millisecond)
	}
}

// 删除锁
func DelLock(key string) {
	RedisClient.Del(context.Background(), key)
}

chenweijian's avatar
chenweijian committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
func GetCacheInt64(key string) (int64, error) {
	data, err := RedisClient.Get(context.Background(), key).Int64()
	if err != nil && err != redis.Nil {
		return 0, err
	}
	return data, nil
}

func IncrBy(key string, num int64) (int64, error) {
	resNum, err := RedisClient.IncrBy(context.Background(), key, num).Result()
	if err != nil {
		return 0, err
	}
	return resNum, nil
}

func IncrNumExpire(key string, num int64, expiration time.Duration) (int64, error) {
	times, err := IncrBy(key, num)
	if err != nil {
		return 0, err
	}
	ttl, err := RedisClient.TTL(context.Background(), key).Result()
	if err != nil {
		return 0, err
	}
	if ttl == -1 {
		RedisClient.Expire(context.Background(), key, expiration)
	}
	return times, nil
}
chenweijian's avatar
chenweijian committed
88 89 90 91 92 93 94 95 96

func DelCache(key string) error {
	err := RedisClient.Del(context.Background(), key).Err()
	if err != nil {
		mylogrus.MyLog.Errorf("DelCache key:%s, err:%s", key, err)
		return err
	}
	return nil
}
chenweijian's avatar
chenweijian committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

func DecrBy(key string, num int64) (int64, error) {
	resNum, err := RedisClient.DecrBy(context.Background(), key, num).Result()
	if err != nil {
		return 0, err
	}
	return resNum, nil
}

func DecrNumExpire(key string, num int64, expiration time.Duration) (int64, error) {
	times, err := DecrBy(key, num)
	if err != nil {
		return 0, err
	}
	ttl, err := RedisClient.TTL(context.Background(), key).Result()
	if err != nil {
		return 0, err
	}
	if ttl == -1 {
		RedisClient.Expire(context.Background(), key, expiration)
	}
	return times, nil
}
chenweijian's avatar
chenweijian committed
120 121 122 123 124 125 126 127

func DecrNum(key string, num int64) (int64, error) {
	resNum, err := RedisClient.DecrBy(context.Background(), key, num).Result()
	if err != nil {
		return 0, myerr.WrapErr(err)
	}
	return resNum, nil
}