diff --git a/resource/redisCli/util.go b/resource/redisCli/util.go index b6b4f8092bf14194d556af63dcb375ab70ca7ae8..5c6fd4c2c135569d89e84dbde632c7bbd0293150 100644 --- a/resource/redisCli/util.go +++ b/resource/redisCli/util.go @@ -74,3 +74,26 @@ func DelCache(key string) error { } return nil } + +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 +}