Skip to content

Commit 2408d41

Browse files
committed
API vhange
1 parent 3785924 commit 2408d41

File tree

3 files changed

+42
-46
lines changed

3 files changed

+42
-46
lines changed

commands_test.go

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ var _ = Describe("Commands", func() {
28142814

28152815
It("should HGETDEL", Label("hash", "HGETDEL"), func() {
28162816
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2817-
// Setup: create a hash with three fields.
2817+
28182818
err := client.HSet(ctx, "myhash", "f1", "val1", "f2", "val2", "f3", "val3").Err()
28192819
Expect(err).NotTo(HaveOccurred())
28202820

@@ -2837,8 +2837,6 @@ var _ = Describe("Commands", func() {
28372837
// HGETDEL on a key that does not exist.
28382838
res, err := client.HGetDel(ctx, "nonexistent", "f1", "f2").Result()
28392839
Expect(err).To(BeNil())
2840-
// Depending on your implementation, missing fields may return nil or empty strings.
2841-
// Adjust the expectation accordingly.
28422840
Expect(res).To(Equal([]string{"", ""}))
28432841
})
28442842

@@ -2847,38 +2845,46 @@ var _ = Describe("Commands", func() {
28472845
// -----------------------------
28482846
It("should HGETEX with EX option", Label("hash", "HGETEX"), func() {
28492847
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2850-
// Setup: create a hash.
2848+
28512849
err := client.HSet(ctx, "myhash", "f1", "val1", "f2", "val2").Err()
28522850
Expect(err).NotTo(HaveOccurred())
28532851

28542852
// Call HGETEX with EX option and 60 seconds TTL.
2855-
res, err := client.HGetEXWithArgs(ctx, "myhash", redis.HGetEXExpirationEX, 60, "f1", "f2").Result()
2853+
opt := redis.HGetEXOptions{
2854+
ExpirationType: redis.HGetEXExpirationEX,
2855+
ExpirationVal: 60,
2856+
}
2857+
res, err := client.HGetEXWithArgs(ctx, "myhash", &opt, "f1", "f2").Result()
28562858
Expect(err).NotTo(HaveOccurred())
28572859
Expect(res).To(Equal([]string{"val1", "val2"}))
2858-
// Optionally, verify TTL if your implementation exposes it.
28592860
})
28602861

28612862
It("should HGETEX with PERSIST option", Label("hash", "HGETEX"), func() {
2862-
SkipBeforeRedisVersion(8, "requires Redis 8.x")
2863-
// Setup: create a hash.
2863+
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2864+
28642865
err := client.HSet(ctx, "myhash", "f1", "val1", "f2", "val2").Err()
28652866
Expect(err).NotTo(HaveOccurred())
28662867

28672868
// Call HGETEX with PERSIST (no TTL value needed).
2868-
res, err := client.HGetEXWithArgs(ctx, "myhash", redis.HGetEXExpirationPERSIST, 0, "f1", "f2").Result()
2869+
opt := redis.HGetEXOptions{ExpirationType: redis.HGetEXExpirationPERSIST}
2870+
res, err := client.HGetEXWithArgs(ctx, "myhash", &opt, "f1", "f2").Result()
28692871
Expect(err).NotTo(HaveOccurred())
28702872
Expect(res).To(Equal([]string{"val1", "val2"}))
28712873
})
28722874

28732875
It("should HGETEX with EXAT option", Label("hash", "HGETEX"), func() {
2874-
SkipBeforeRedisVersion(8, "requires Redis 8.x")
2875-
// Setup: create a hash.
2876+
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2877+
28762878
err := client.HSet(ctx, "myhash", "f1", "val1", "f2", "val2").Err()
28772879
Expect(err).NotTo(HaveOccurred())
28782880

28792881
// Set expiration at a specific Unix timestamp (60 seconds from now).
28802882
expireAt := time.Now().Add(60 * time.Second).Unix()
2881-
res, err := client.HGetEXWithArgs(ctx, "myhash", redis.HGetEXExpirationEXAT, expireAt, "f1", "f2").Result()
2883+
opt := redis.HGetEXOptions{
2884+
ExpirationType: redis.HGetEXExpirationEXAT,
2885+
ExpirationVal: expireAt,
2886+
}
2887+
res, err := client.HGetEXWithArgs(ctx, "myhash", &opt, "f1", "f2").Result()
28822888
Expect(err).NotTo(HaveOccurred())
28832889
Expect(res).To(Equal([]string{"val1", "val2"}))
28842890
})
@@ -2887,75 +2893,62 @@ var _ = Describe("Commands", func() {
28872893
// HSETEX with FNX/FXX options
28882894
// -----------------------------
28892895
It("should HSETEX with FNX condition", Label("hash", "HSETEX"), func() {
2890-
SkipBeforeRedisVersion(8, "requires Redis 8.x")
2891-
// Ensure the key is removed.
2892-
client.Del(ctx, "myhash")
2896+
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
28932897

2894-
// FNX: set field only if it does not exist.
2895-
opt := redis.HSetXOptions{
2898+
opt := redis.HSetEXOptions{
28962899
Condition: redis.HSetEXFNX,
28972900
ExpirationType: redis.HSetEXExpirationEX,
28982901
ExpirationVal: 60,
28992902
}
29002903
res, err := client.HSetEXWithArgs(ctx, "myhash", &opt, "f1", "val1").Result()
29012904
Expect(err).NotTo(HaveOccurred())
2902-
// Expect the field to be set.
29032905
Expect(res).To(Equal(int64(1)))
29042906

2905-
opt = redis.HSetXOptions{
2907+
opt = redis.HSetEXOptions{
29062908
Condition: redis.HSetEXFNX,
29072909
ExpirationType: redis.HSetEXExpirationEX,
29082910
ExpirationVal: 60,
29092911
}
2910-
// Attempt to set the same field again with FNX.
29112912
res, err = client.HSetEXWithArgs(ctx, "myhash", &opt, "f1", "val2").Result()
29122913
Expect(err).NotTo(HaveOccurred())
2913-
// Since the field already exists, no update occurs.
29142914
Expect(res).To(Equal(int64(0)))
29152915
})
29162916

29172917
It("should HSETEX with FXX condition", Label("hash", "HSETEX"), func() {
29182918
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2919-
// Setup: ensure field f2 exists.
2920-
client.Del(ctx, "myhash")
2919+
29212920
err := client.HSet(ctx, "myhash", "f2", "val1").Err()
29222921
Expect(err).NotTo(HaveOccurred())
29232922

2924-
opt := redis.HSetXOptions{
2923+
opt := redis.HSetEXOptions{
29252924
Condition: redis.HSetEXFXX,
29262925
ExpirationType: redis.HSetEXExpirationEX,
29272926
ExpirationVal: 60,
29282927
}
2929-
// FXX: update field only if it exists.
29302928
res, err := client.HSetEXWithArgs(ctx, "myhash", &opt, "f2", "val2").Result()
29312929
Expect(err).NotTo(HaveOccurred())
29322930
Expect(res).To(Equal(int64(1)))
2933-
opt = redis.HSetXOptions{
2931+
opt = redis.HSetEXOptions{
29342932
Condition: redis.HSetEXFXX,
29352933
ExpirationType: redis.HSetEXExpirationEX,
29362934
ExpirationVal: 60,
29372935
}
2938-
// FXX on a non-existing field (f3) should not set the field.
29392936
res, err = client.HSetEXWithArgs(ctx, "myhash", &opt, "f3", "val3").Result()
29402937
Expect(err).NotTo(HaveOccurred())
29412938
Expect(res).To(Equal(int64(0)))
29422939
})
29432940

29442941
It("should HSETEX with multiple field operations", Label("hash", "HSETEX"), func() {
2945-
SkipBeforeRedisVersion(8, "requires Redis 8.x")
2946-
// Remove key if it exists.
2947-
client.Del(ctx, "myhash")
2948-
opt := redis.HSetXOptions{
2942+
SkipBeforeRedisVersion(7.9, "requires Redis 8.x")
2943+
2944+
opt := redis.HSetEXOptions{
29492945
ExpirationType: redis.HSetEXExpirationEX,
29502946
ExpirationVal: 60,
29512947
}
2952-
// Set multiple fields at once (no condition).
29532948
res, err := client.HSetEXWithArgs(ctx, "myhash", &opt, "f1", "val1", "f2", "val2").Result()
29542949
Expect(err).NotTo(HaveOccurred())
2955-
// Assume 1 indicates all fields were set.
29562950
Expect(res).To(Equal(int64(1)))
29572951

2958-
// Verify that both fields are set.
29592952
values, err := client.HMGet(ctx, "myhash", "f1", "f2").Result()
29602953
Expect(err).NotTo(HaveOccurred())
29612954
Expect(values).To(Equal([]interface{}{"val1", "val2"}))

example/hset-struct/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
22
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
3-
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
4-
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
53
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
64
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

hash_commands.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ type HashCmdable interface {
1212
HGetAll(ctx context.Context, key string) *MapStringStringCmd
1313
HGetDel(ctx context.Context, key string, fields ...string) *StringSliceCmd
1414
HGetEX(ctx context.Context, key string, fields ...string) *StringSliceCmd
15-
HGetEXWithArgs(ctx context.Context, key string, expirationType HGetEXExpirationType, expirationVal int64, fields ...string) *StringSliceCmd
16-
HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd
15+
HGetEXWithArgs(ctx context.Context, key string, options *HGetEXOptions, fields ...string) *StringSliceCmd
1716
HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd
1817
HKeys(ctx context.Context, key string) *StringSliceCmd
1918
HLen(ctx context.Context, key string) *IntCmd
2019
HMGet(ctx context.Context, key string, fields ...string) *SliceCmd
2120
HSet(ctx context.Context, key string, values ...interface{}) *IntCmd
2221
HMSet(ctx context.Context, key string, values ...interface{}) *BoolCmd
2322
HSetEX(ctx context.Context, key string, fieldsAndValues ...string) *IntCmd
24-
HSetEXWithArgs(ctx context.Context, key string, options *HSetXOptions, fieldsAndValues ...string) *IntCmd
23+
HSetEXWithArgs(ctx context.Context, key string, options *HSetEXOptions, fieldsAndValues ...string) *IntCmd
2524
HSetNX(ctx context.Context, key, field string, value interface{}) *BoolCmd
2625
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
2726
HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
@@ -491,12 +490,18 @@ const (
491490
HGetEXExpirationPERSIST HGetEXExpirationType = "PERSIST"
492491
)
493492

494-
func (c cmdable) HGetEXWithArgs(ctx context.Context, key string, expirationType HGetEXExpirationType, expirationVal int64, fields ...string) *StringSliceCmd {
495-
args := []interface{}{"HGETEX", key}
493+
type HGetEXOptions struct {
494+
ExpirationType HGetEXExpirationType
495+
ExpirationVal int64
496+
}
496497

497-
args = append(args, string(expirationType))
498-
if expirationType != HGetEXExpirationPERSIST {
499-
args = append(args, expirationVal)
498+
func (c cmdable) HGetEXWithArgs(ctx context.Context, key string, options *HGetEXOptions, fields ...string) *StringSliceCmd {
499+
args := []interface{}{"HGETEX", key}
500+
if options.ExpirationType != "" {
501+
args = append(args, string(options.ExpirationType))
502+
if options.ExpirationType != HGetEXExpirationPERSIST {
503+
args = append(args, options.ExpirationVal)
504+
}
500505
}
501506

502507
args = append(args, "FIELDS", len(fields))
@@ -526,7 +531,7 @@ const (
526531
HSetEXExpirationKEEPTTL HSetEXExpirationType = "KEEPTTL"
527532
)
528533

529-
type HSetXOptions struct {
534+
type HSetEXOptions struct {
530535
Condition HSetEXCondition
531536
ExpirationType HSetEXExpirationType
532537
ExpirationVal int64
@@ -543,7 +548,7 @@ func (c cmdable) HSetEX(ctx context.Context, key string, fieldsAndValues ...stri
543548
return cmd
544549
}
545550

546-
func (c cmdable) HSetEXWithArgs(ctx context.Context, key string, options *HSetXOptions, fieldsAndValues ...string) *IntCmd {
551+
func (c cmdable) HSetEXWithArgs(ctx context.Context, key string, options *HSetEXOptions, fieldsAndValues ...string) *IntCmd {
547552
args := []interface{}{"HSETEX", key}
548553
if options.Condition != "" {
549554
args = append(args, string(options.Condition))

0 commit comments

Comments
 (0)