From c9dceefb0f2ce53a758ed4beef5aee2e3ebb204d Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Wed, 12 Jul 2023 17:48:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=84=E9=92=BB=E4=B9=B0=E7=B2=89=E9=92=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/redisCli/util.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/resource/redisCli/util.go b/resource/redisCli/util.go index b6b4f80..5c6fd4c 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 +} -- 2.22.0