From b4dc60d25676afb893f0f6b47d79e6dc37cfc093 Mon Sep 17 00:00:00 2001
From: "mingye.fan"
Date: Wed, 24 Nov 2021 11:18:22 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=9C=89=E6=95=88=E8=BF=87?=
=?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=95=BF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
xredis_set.go | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/xredis_set.go b/xredis_set.go
index 2d83a86..3729918 100644
--- a/xredis_set.go
+++ b/xredis_set.go
@@ -145,8 +145,9 @@ func (c *Client) setListValue(ctx context.Context, key string, valValue reflect.
if err != nil {
return err
}
- if options.Expiration != -1 {
- return c.Expire(ctx, key, options.Expiration).Err()
+ err = c.expireKeyTTl(ctx, key, options.Expiration)
+ if err != nil {
+ return err
}
return nil
}
@@ -165,8 +166,9 @@ func (c *Client) setSetValue(ctx context.Context, key string, valValue reflect.V
if err != nil {
return err
}
- if options.Expiration != -1 {
- return c.Expire(ctx, key, options.Expiration).Err()
+ err = c.expireKeyTTl(ctx, key, options.Expiration)
+ if err != nil {
+ return err
}
return nil
}
@@ -186,8 +188,9 @@ func (c *Client) setStructValue(ctx context.Context, key string, valValue reflec
if err != nil {
return err
}
- if options.Expiration != -1 {
- return c.Expire(ctx, key, options.Expiration).Err()
+ err = c.expireKeyTTl(ctx, key, options.Expiration)
+ if err != nil {
+ return err
}
return nil
}
@@ -207,8 +210,17 @@ func (c *Client) setMapValue(ctx context.Context, key string, valValue reflect.V
if err != nil {
return err
}
- if options.Expiration != -1 {
- return c.Expire(ctx, key, options.Expiration).Err()
+ err = c.expireKeyTTl(ctx, key, options.Expiration)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// 设置有效过期时长
+func (c *Client) expireKeyTTl(ctx context.Context, key string, expiration time.Duration) error {
+ if expiration > 0 {
+ return c.Expire(ctx, key, expiration).Err()
}
return nil
}