Skip to content

Commit e3ce4ea

Browse files
committed
remove PERSIST, add unit tests to ensure the stability of ZRandMember and HRandField.
Signed-off-by: monkey <[email protected]>
1 parent e7dbdda commit e3ce4ea

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

commands.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ import (
99
"github.com/go-redis/redis/v8/internal"
1010
)
1111

12-
const (
13-
// KeepTTL is an option for Set command to keep key's existing TTL.
14-
// For example:
15-
//
16-
// rdb.Set(ctx, key, value, redis.KeepTTL)
17-
KeepTTL = -1
18-
19-
// Persist is remove the time to live associated with the key.
20-
// For example:
21-
// rdb.GetEX(ctx, key, redis.Persist)
22-
Persist = -2
23-
)
12+
// KeepTTL is an option for Set command to keep key's existing TTL.
13+
// For example:
14+
//
15+
// rdb.Set(ctx, key, value, redis.KeepTTL)
16+
const KeepTTL = -1
2417

2518
func usePrecise(dur time.Duration) bool {
2619
return dur < time.Second || dur%time.Second != 0
@@ -722,9 +715,7 @@ func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *Str
722715
}
723716

724717
// redis-server version >= 6.2.0.
725-
//
726-
// A value of zero means that the expiration time will not be changed.
727-
// Persist(-2) Remove the time to live associated with the key.
718+
// A expiration of zero remove the time to live associated with the key(GetEX key persist).
728719
func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration) *StringCmd {
729720
args := make([]interface{}, 0, 4)
730721
args = append(args, "getex", key)
@@ -734,7 +725,7 @@ func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration
734725
} else {
735726
args = append(args, "ex", formatSec(ctx, expiration))
736727
}
737-
} else if expiration == Persist {
728+
} else if expiration == 0 {
738729
args = append(args, "persist")
739730
}
740731

@@ -1222,6 +1213,7 @@ func (c cmdable) HVals(ctx context.Context, key string) *StringSliceCmd {
12221213
return cmd
12231214
}
12241215

1216+
// redis-server version >= 6.2.0.
12251217
func (c cmdable) HRandField(ctx context.Context, key string, count int, withValues bool) *StringSliceCmd {
12261218
args := make([]interface{}, 0, 4)
12271219

@@ -2310,8 +2302,11 @@ func (c cmdable) ZUnionStore(ctx context.Context, dest string, store *ZStore) *I
23102302
return cmd
23112303
}
23122304

2305+
// redis-server version >= 6.2.0.
23132306
func (c cmdable) ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd {
23142307
args := make([]interface{}, 0, 4)
2308+
2309+
// Although count=0 is meaningless, redis accepts count=0.
23152310
args = append(args, "zrandmember", key, count)
23162311
if withScores {
23172312
args = append(args, "withscores")

commands_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,11 @@ var _ = Describe("Commands", func() {
18411841

18421842
v := client.HRandField(ctx, "hash", 1, false)
18431843
Expect(v.Err()).NotTo(HaveOccurred())
1844-
Expect(v.Result()).To(Or(Equal([]string{"key1"}), Equal([]string{"key2"})))
1844+
Expect(v.Val()).To(Or(Equal([]string{"key1"}), Equal([]string{"key2"})))
1845+
1846+
v = client.HRandField(ctx, "hash", 0, false)
1847+
Expect(v.Err()).NotTo(HaveOccurred())
1848+
Expect(v.Val()).To(HaveLen(0))
18451849

18461850
var slice []string
18471851
err = client.HRandField(ctx, "hash", 1, true).ScanSlice(&slice)
@@ -3897,6 +3901,10 @@ var _ = Describe("Commands", func() {
38973901
Expect(v.Err()).NotTo(HaveOccurred())
38983902
Expect(v.Val()).To(Or(Equal([]string{"one"}), Equal([]string{"two"})))
38993903

3904+
v = client.ZRandMember(ctx, "zset", 0, false)
3905+
Expect(v.Err()).NotTo(HaveOccurred())
3906+
Expect(v.Val()).To(HaveLen(0))
3907+
39003908
var slice []string
39013909
err = client.ZRandMember(ctx, "zset", 1, true).ScanSlice(&slice)
39023910
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)