remove PERSIST, add unit tests to ensure the stability of ZRandMember and HRandField.

Signed-off-by: monkey <golang@88.com>
This commit is contained in:
monkey 2021-03-27 17:49:55 +08:00
parent e7dbdda439
commit e3ce4ea661
2 changed files with 20 additions and 17 deletions

View File

@ -9,18 +9,11 @@ import (
"github.com/go-redis/redis/v8/internal"
)
const (
// KeepTTL is an option for Set command to keep key's existing TTL.
// For example:
//
// rdb.Set(ctx, key, value, redis.KeepTTL)
KeepTTL = -1
// Persist is remove the time to live associated with the key.
// For example:
// rdb.GetEX(ctx, key, redis.Persist)
Persist = -2
)
// KeepTTL is an option for Set command to keep key's existing TTL.
// For example:
//
// rdb.Set(ctx, key, value, redis.KeepTTL)
const KeepTTL = -1
func usePrecise(dur time.Duration) bool {
return dur < time.Second || dur%time.Second != 0
@ -722,9 +715,7 @@ func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *Str
}
// redis-server version >= 6.2.0.
//
// A value of zero means that the expiration time will not be changed.
// Persist(-2) Remove the time to live associated with the key.
// A expiration of zero remove the time to live associated with the key(GetEX key persist).
func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration) *StringCmd {
args := make([]interface{}, 0, 4)
args = append(args, "getex", key)
@ -734,7 +725,7 @@ func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration
} else {
args = append(args, "ex", formatSec(ctx, expiration))
}
} else if expiration == Persist {
} else if expiration == 0 {
args = append(args, "persist")
}
@ -1222,6 +1213,7 @@ func (c cmdable) HVals(ctx context.Context, key string) *StringSliceCmd {
return cmd
}
// redis-server version >= 6.2.0.
func (c cmdable) HRandField(ctx context.Context, key string, count int, withValues bool) *StringSliceCmd {
args := make([]interface{}, 0, 4)
@ -2310,8 +2302,11 @@ func (c cmdable) ZUnionStore(ctx context.Context, dest string, store *ZStore) *I
return cmd
}
// redis-server version >= 6.2.0.
func (c cmdable) ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd {
args := make([]interface{}, 0, 4)
// Although count=0 is meaningless, redis accepts count=0.
args = append(args, "zrandmember", key, count)
if withScores {
args = append(args, "withscores")

View File

@ -1841,7 +1841,11 @@ var _ = Describe("Commands", func() {
v := client.HRandField(ctx, "hash", 1, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Result()).To(Or(Equal([]string{"key1"}), Equal([]string{"key2"})))
Expect(v.Val()).To(Or(Equal([]string{"key1"}), Equal([]string{"key2"})))
v = client.HRandField(ctx, "hash", 0, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(HaveLen(0))
var slice []string
err = client.HRandField(ctx, "hash", 1, true).ScanSlice(&slice)
@ -3897,6 +3901,10 @@ var _ = Describe("Commands", func() {
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(Or(Equal([]string{"one"}), Equal([]string{"two"})))
v = client.ZRandMember(ctx, "zset", 0, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(HaveLen(0))
var slice []string
err = client.ZRandMember(ctx, "zset", 1, true).ScanSlice(&slice)
Expect(err).NotTo(HaveOccurred())