mirror of https://github.com/go-redis/redis.git
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:
parent
e7dbdda439
commit
e3ce4ea661
27
commands.go
27
commands.go
|
@ -9,18 +9,11 @@ import (
|
||||||
"github.com/go-redis/redis/v8/internal"
|
"github.com/go-redis/redis/v8/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// KeepTTL is an option for Set command to keep key's existing TTL.
|
||||||
// KeepTTL is an option for Set command to keep key's existing TTL.
|
// For example:
|
||||||
// For example:
|
//
|
||||||
//
|
// rdb.Set(ctx, key, value, redis.KeepTTL)
|
||||||
// rdb.Set(ctx, key, value, redis.KeepTTL)
|
const KeepTTL = -1
|
||||||
KeepTTL = -1
|
|
||||||
|
|
||||||
// Persist is remove the time to live associated with the key.
|
|
||||||
// For example:
|
|
||||||
// rdb.GetEX(ctx, key, redis.Persist)
|
|
||||||
Persist = -2
|
|
||||||
)
|
|
||||||
|
|
||||||
func usePrecise(dur time.Duration) bool {
|
func usePrecise(dur time.Duration) bool {
|
||||||
return dur < time.Second || dur%time.Second != 0
|
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.
|
// redis-server version >= 6.2.0.
|
||||||
//
|
// A expiration of zero remove the time to live associated with the key(GetEX key persist).
|
||||||
// A value of zero means that the expiration time will not be changed.
|
|
||||||
// Persist(-2) Remove the time to live associated with the key.
|
|
||||||
func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration) *StringCmd {
|
func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration) *StringCmd {
|
||||||
args := make([]interface{}, 0, 4)
|
args := make([]interface{}, 0, 4)
|
||||||
args = append(args, "getex", key)
|
args = append(args, "getex", key)
|
||||||
|
@ -734,7 +725,7 @@ func (c cmdable) GetEX(ctx context.Context, key string, expiration time.Duration
|
||||||
} else {
|
} else {
|
||||||
args = append(args, "ex", formatSec(ctx, expiration))
|
args = append(args, "ex", formatSec(ctx, expiration))
|
||||||
}
|
}
|
||||||
} else if expiration == Persist {
|
} else if expiration == 0 {
|
||||||
args = append(args, "persist")
|
args = append(args, "persist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1222,6 +1213,7 @@ func (c cmdable) HVals(ctx context.Context, key string) *StringSliceCmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// redis-server version >= 6.2.0.
|
||||||
func (c cmdable) HRandField(ctx context.Context, key string, count int, withValues bool) *StringSliceCmd {
|
func (c cmdable) HRandField(ctx context.Context, key string, count int, withValues bool) *StringSliceCmd {
|
||||||
args := make([]interface{}, 0, 4)
|
args := make([]interface{}, 0, 4)
|
||||||
|
|
||||||
|
@ -2310,8 +2302,11 @@ func (c cmdable) ZUnionStore(ctx context.Context, dest string, store *ZStore) *I
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// redis-server version >= 6.2.0.
|
||||||
func (c cmdable) ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd {
|
func (c cmdable) ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd {
|
||||||
args := make([]interface{}, 0, 4)
|
args := make([]interface{}, 0, 4)
|
||||||
|
|
||||||
|
// Although count=0 is meaningless, redis accepts count=0.
|
||||||
args = append(args, "zrandmember", key, count)
|
args = append(args, "zrandmember", key, count)
|
||||||
if withScores {
|
if withScores {
|
||||||
args = append(args, "withscores")
|
args = append(args, "withscores")
|
||||||
|
|
|
@ -1841,7 +1841,11 @@ var _ = Describe("Commands", func() {
|
||||||
|
|
||||||
v := client.HRandField(ctx, "hash", 1, false)
|
v := client.HRandField(ctx, "hash", 1, false)
|
||||||
Expect(v.Err()).NotTo(HaveOccurred())
|
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
|
var slice []string
|
||||||
err = client.HRandField(ctx, "hash", 1, true).ScanSlice(&slice)
|
err = client.HRandField(ctx, "hash", 1, true).ScanSlice(&slice)
|
||||||
|
@ -3897,6 +3901,10 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(v.Err()).NotTo(HaveOccurred())
|
Expect(v.Err()).NotTo(HaveOccurred())
|
||||||
Expect(v.Val()).To(Or(Equal([]string{"one"}), Equal([]string{"two"})))
|
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
|
var slice []string
|
||||||
err = client.ZRandMember(ctx, "zset", 1, true).ScanSlice(&slice)
|
err = client.ZRandMember(ctx, "zset", 1, true).ScanSlice(&slice)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
Loading…
Reference in New Issue