From 547c6d79847414de3b9b6361438833e5df0eb7b2 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 12 May 2024 13:45:05 +0300 Subject: [PATCH 1/8] Add HExpire command --- commands_test.go | 14 ++++++++++++++ hash_commands.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/commands_test.go b/commands_test.go index d30a9d8bb..c3f3b5567 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1104,6 +1104,20 @@ var _ = Describe("Commands", func() { Expect(cursor).NotTo(BeZero()) }) + It("should HExpire", func() { + res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(BeNil()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err = client.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int{1, 1, -2})) + }) + It("should ZScan", func() { for i := 0; i < 1000; i++ { err := client.ZAdd(ctx, "myset", redis.Z{ diff --git a/hash_commands.go b/hash_commands.go index 2c62a75ad..fa37fe5cb 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -172,3 +172,13 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str _ = c(ctx, cmd) return cmd } + +func (c cmdable) HExpire(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIRE", key, count, "FIELDS", len(fields)} + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} From 5a8ad13353c5b123358c61c5d8dc0d8d5fb55acc Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 12 May 2024 14:41:42 +0300 Subject: [PATCH 2/8] Add HPExpire, HexpireAt, HPExpireAt, HTTL, HPTTL, HPersist,HExpireTime, HPExpireTIme, HGetF, HSetF commands --- hash_commands.go | 270 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 267 insertions(+), 3 deletions(-) diff --git a/hash_commands.go b/hash_commands.go index fa37fe5cb..a8ed4d740 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -1,6 +1,9 @@ package redis -import "context" +import ( + "context" + "time" +) type HashCmdable interface { HDel(ctx context.Context, key string, fields ...string) *IntCmd @@ -173,8 +176,149 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str return cmd } -func (c cmdable) HExpire(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIRE", key, count, "FIELDS", len(fields)} +type HExpireArgs struct { + NX bool + XX bool + GT bool + LT bool +} + +func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIRE", key, expiration} + + // only if one argument is true, we can add it to the args + // if more than one argument is true, it will cause an error + if expirationArgs.NX { + args = append(args, "NX") + } else if expirationArgs.XX { + args = append(args, "XX") + } else if expirationArgs.GT { + args = append(args, "GT") + } else if expirationArgs.LT { + args = append(args, "LT") + } + + args = append(args, "FIELDS", len(fields)) + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration)} + + // only if one argument is true, we can add it to the args + // if more than one argument is true, it will cause an error + if expirationArgs.NX { + args = append(args, "NX") + } else if expirationArgs.XX { + args = append(args, "XX") + } else if expirationArgs.GT { + args = append(args, "GT") + } else if expirationArgs.LT { + args = append(args, "LT") + } + + args = append(args, "FIELDS", len(fields)) + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIREAT", key, tm.Unix()} + + // only if one argument is true, we can add it to the args + // if more than one argument is true, it will cause an error + if expirationArgs.NX { + args = append(args, "NX") + } else if expirationArgs.XX { + args = append(args, "XX") + } else if expirationArgs.GT { + args = append(args, "GT") + } else if expirationArgs.LT { + args = append(args, "LT") + } + + args = append(args, "FIELDS", len(fields)) + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond)} + + // only if one argument is true, we can add it to the args + // if more than one argument is true, it will cause an error + if expirationArgs.NX { + args = append(args, "NX") + } else if expirationArgs.XX { + args = append(args, "XX") + } else if expirationArgs.GT { + args = append(args, "GT") + } else if expirationArgs.LT { + args = append(args, "LT") + } + + args = append(args, "FIELDS", len(fields)) + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HPERSIST", key, count, "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIRETIME", key, count, "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIRETIME", key, count, "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HTTL", key, count, "FIELDS", len(fields)} + for _, field := range fields { args = append(args, field) } @@ -182,3 +326,123 @@ func (c cmdable) HExpire(ctx context.Context, key string, count int, fields ...s _ = c(ctx, cmd) return cmd } + +func (c cmdable) HPTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { + args := []interface{}{"HPTTL", key, count, "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +type HGetFArgs struct { + Persist bool + EX time.Duration + PX time.Duration + EXAT time.Time + PXAT time.Time +} + +func (c cmdable) HGetF(ctx context.Context, key, field string, expirationArgs HExpireArgs, args HGetFArgs) *StringSliceCmd { + argsSlice := []interface{}{"HGETF", key, field} + + // only if one argument is true, we can add it to the args + // if more than one argument is true, it will cause an error + if expirationArgs.NX { + argsSlice = append(argsSlice, "NX") + } else if expirationArgs.XX { + argsSlice = append(argsSlice, "XX") + } else if expirationArgs.GT { + argsSlice = append(argsSlice, "GT") + } else if expirationArgs.LT { + argsSlice = append(argsSlice, "LT") + } + + if args.Persist { + argsSlice = append(argsSlice, "PERSIST") + } else if args.EX > 0 { + argsSlice = append(argsSlice, "EX", args.EX) + } else if args.PX > 0 { + argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) + } else if !args.EXAT.IsZero() { + argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) + } else if !args.PXAT.IsZero() { + argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) + } + + cmd := NewStringSliceCmd(ctx, argsSlice...) + _ = c(ctx, cmd) + return cmd +} + +type HSetFArgs struct { + DC bool + DCF bool + DOF bool + NX bool + XX bool + GT bool + LT bool + GETNEW bool + GETOLD bool + EX time.Duration + PX time.Duration + EXAT time.Time + PXAT time.Time + KEEPTTL bool +} + +func (c cmdable) HSetF(ctx context.Context, key string, args HSetFArgs, fieldValues ...string) *StringSliceCmd { + argsSlice := []interface{}{"HSETF", key} + + if args.DC { + argsSlice = append(argsSlice, "DC") + } + + if args.DCF { + argsSlice = append(argsSlice, "DCF") + } else if args.DOF { + argsSlice = append(argsSlice, "DOF") + } + + if args.NX { + argsSlice = append(argsSlice, "NX") + } else if args.XX { + argsSlice = append(argsSlice, "XX") + } else if args.GT { + argsSlice = append(argsSlice, "GT") + } else if args.LT { + argsSlice = append(argsSlice, "LT") + } + + if args.GETNEW { + argsSlice = append(argsSlice, "GETNEW") + } else if args.GETOLD { + argsSlice = append(argsSlice, "GETOLD") + } + + if args.KEEPTTL { + argsSlice = append(argsSlice, "KEEPTTL") + } else if args.EX > 0 { + argsSlice = append(argsSlice, "EX", args.EX) + } else if args.PX > 0 { + argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) + } else if !args.EXAT.IsZero() { + argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) + } else if !args.PXAT.IsZero() { + argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) + } + + argsSlice = append(argsSlice, "FVS", len(fieldValues)) + + for _, fieldValue := range fieldValues { + argsSlice = append(argsSlice, fieldValue) + } + + cmd := NewStringSliceCmd(ctx, argsSlice...) + _ = c(ctx, cmd) + return cmd +} From 034386344c5e3f3c916ab468632a00e3a71f8244 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 12 May 2024 15:44:20 +0300 Subject: [PATCH 3/8] add docstring --- commands_test.go | 6 +++ hash_commands.go | 126 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/commands_test.go b/commands_test.go index c3f3b5567..d75112e5d 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1105,6 +1105,12 @@ var _ = Describe("Commands", func() { }) It("should HExpire", func() { + //For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "ADDR", + // }) + // defer client1.Close() + res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(BeNil()) diff --git a/hash_commands.go b/hash_commands.go index a8ed4d740..c215b12ce 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -183,7 +183,25 @@ type HExpireArgs struct { LT bool } -func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { +// HExpire - Sets the expiration time for specified fields in a hash in seconds. +// The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. +// For more information - https://redis.io/commands/hexpire/ +func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIRE", key, expiration, "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +// HExpire - Sets the expiration time for specified fields in a hash in seconds. +// It requires a key, an expiration duration, a struct with boolean flags for conditional expiration settings (NX, XX, GT, LT), and a list of fields. +// The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. +// For more information - https://redis.io/commands/hexpire/ +func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { args := []interface{}{"HEXPIRE", key, expiration} // only if one argument is true, we can add it to the args @@ -208,7 +226,22 @@ func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Durati return cmd } +// HPExpire - Sets the expiration time for specified fields in a hash in milliseconds. +// Similar to HExpire, it accepts a key, an expiration duration in milliseconds, a struct with expiration condition flags, and a list of fields. +// The command modifies the standard time.Duration to milliseconds for the Redis command. +// For more information - https://redis.io/commands/hpexpire/ func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration)} // only if one argument is true, we can add it to the args @@ -233,7 +266,22 @@ func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Durat return cmd } -func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { +// HExpireAt - Sets the expiration time for specified fields in a hash to a UNIX timestamp in seconds. +// Takes a key, a UNIX timestamp, a struct of conditional flags, and a list of fields. +// The command sets absolute expiration times based on the UNIX timestamp provided. +// For more information - https://redis.io/commands/hexpireat/ +func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIREAT", key, tm.Unix(), "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { args := []interface{}{"HEXPIREAT", key, tm.Unix()} // only if one argument is true, we can add it to the args @@ -258,7 +306,21 @@ func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, expira return cmd } -func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { +// HPExpireAt - Sets the expiration time for specified fields in a hash to a UNIX timestamp in milliseconds. +// Similar to HExpireAt but for timestamps in milliseconds. It accepts the same parameters and adjusts the UNIX time to milliseconds. +// For more information - https://redis.io/commands/hpexpireat/ +func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), "FIELDS", len(fields)} + + for _, field := range fields { + args = append(args, field) + } + cmd := NewIntSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Time, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond)} // only if one argument is true, we can add it to the args @@ -283,6 +345,10 @@ func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, expir return cmd } +// HPersist - Removes the expiration time from specified fields in a hash. +// Accepts a key, the count of fields to affect, and the fields themselves. +// This command ensures that each field specified will have its expiration removed if present. +// For more information - https://redis.io/commands/hpersist/ func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { args := []interface{}{"HPERSIST", key, count, "FIELDS", len(fields)} @@ -294,6 +360,10 @@ func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ... return cmd } +// HExpireTime - Retrieves the expiration time for specified fields in a hash as a UNIX timestamp in seconds. +// Requires a key, a count of fields, and the fields themselves to fetch their expiration timestamps. +// This command returns the expiration times for each field or error/status codes for each field as specified. +// For more information - https://redis.io/commands/hexpiretime/ func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { args := []interface{}{"HEXPIRETIME", key, count, "FIELDS", len(fields)} @@ -305,6 +375,10 @@ func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields return cmd } +// HPExpireTime - Retrieves the expiration time for specified fields in a hash as a UNIX timestamp in milliseconds. +// Similar to HExpireTime, adjusted for timestamps in milliseconds. It requires the same parameters. +// Provides the expiration timestamp for each field in milliseconds. +// For more information - https://redis.io/commands/hexpiretime/ func (c cmdable) HPExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { args := []interface{}{"HPEXPIRETIME", key, count, "FIELDS", len(fields)} @@ -316,6 +390,10 @@ func (c cmdable) HPExpireTime(ctx context.Context, key string, count int, fields return cmd } +// HTTL - Retrieves the remaining time to live for specified fields in a hash in seconds. +// Requires a key, the count of fields, and the fields themselves. It returns the TTL for each specified field. +// This command fetches the TTL in seconds for each field or returns error/status codes as appropriate. +// For more information - https://redis.io/commands/httl/ func (c cmdable) HTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { args := []interface{}{"HTTL", key, count, "FIELDS", len(fields)} @@ -327,6 +405,10 @@ func (c cmdable) HTTL(ctx context.Context, key string, count int, fields ...stri return cmd } +// HPTTL - Retrieves the remaining time to live for specified fields in a hash in milliseconds. +// Similar to HTTL, but returns the TTL in milliseconds. It requires a key, a count of fields, and the specified fields. +// This command provides the TTL in milliseconds for each field or returns error/status codes as needed. +// For more information - https://redis.io/commands/hpttl/ func (c cmdable) HPTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { args := []interface{}{"HPTTL", key, count, "FIELDS", len(fields)} @@ -346,7 +428,37 @@ type HGetFArgs struct { PXAT time.Time } -func (c cmdable) HGetF(ctx context.Context, key, field string, expirationArgs HExpireArgs, args HGetFArgs) *StringSliceCmd { +// HGetF - Retrieves the value of a specified field in a hash and optionally updates its expiration time. +// Accepts a key, a single field, and a struct containing various expiration parameters such as: +// EX (seconds), PX (milliseconds), EXAT (UNIX time in seconds), and PXAT (UNIX time in milliseconds). +// It also allows for the removal of the expiration time with the 'PERSIST' option. +// The command constructs an argument list starting with "HGETF", followed by the key, the field, and any of the specified expiration settings. +// For more information - https://redis.io/commands/hgetf/ +func (c cmdable) HGetF(ctx context.Context, key, field string, args HGetFArgs) *StringSliceCmd { + argsSlice := []interface{}{"HGETF", key, field} + + if args.Persist { + argsSlice = append(argsSlice, "PERSIST") + } else if args.EX > 0 { + argsSlice = append(argsSlice, "EX", args.EX) + } else if args.PX > 0 { + argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) + } else if !args.EXAT.IsZero() { + argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) + } else if !args.PXAT.IsZero() { + argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) + } + + cmd := NewStringSliceCmd(ctx, argsSlice...) + _ = c(ctx, cmd) + return cmd +} + +// HGetFWithArgs - Retrieves the value of a specified field in a hash with additional conditional expiration settings. +// Similar to HGetF but includes conditional flags (NX, XX, GT, LT) to determine when to apply the expiration settings. +// This function builds upon HGetF by adding these conditional flags, which control the setting of the expiration based on the current state of the field. +// For more information - https://redis.io/commands/hgetf/ +func (c cmdable) HGetFWithArgs(ctx context.Context, key, field string, expirationArgs HExpireArgs, args HGetFArgs) *StringSliceCmd { argsSlice := []interface{}{"HGETF", key, field} // only if one argument is true, we can add it to the args @@ -395,6 +507,12 @@ type HSetFArgs struct { KEEPTTL bool } +// HSetF - Sets field-value pairs in a hash with optional expiration and value retrieval settings. +// Accepts a key, a struct of options that includes directives for field creation and modification behavior (DC, DCF, DOF), +// expiration settings (EX, PX, EXAT, PXAT, KEEPTTL), and value retrieval options (GETNEW, GETOLD). +// The command supports conditional settings (NX, XX, GT, LT) to govern how and when fields are updated or expired based on their current state. +// It builds a command that can conditionally set values and expiration times, potentially returning the old or new values of fields. +// For more information - https://redis.io/commands/hsetf/ func (c cmdable) HSetF(ctx context.Context, key string, args HSetFArgs, fieldValues ...string) *StringSliceCmd { argsSlice := []interface{}{"HSETF", key} From e37da15281c4bacd4742eb2732020de2a2f0083e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 13 May 2024 20:38:36 +0300 Subject: [PATCH 4/8] add tests and fix commands --- commands_test.go | 224 +++++++++++++++++++++++++++++++++++++++++++++-- hash_commands.go | 36 +++++--- 2 files changed, 241 insertions(+), 19 deletions(-) diff --git a/commands_test.go b/commands_test.go index d75112e5d..c09f491e0 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1104,24 +1104,230 @@ var _ = Describe("Commands", func() { Expect(cursor).NotTo(BeZero()) }) - It("should HExpire", func() { - //For testing purposes only + // It("should HExpire", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // res, err := client1.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, 1, -2})) + // }) + + // It("should HPExpire", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, 1, -2})) + // }) + + // It("should HExpireAt", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, 1, -2})) + // }) + + // It("should HPExpireAt", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, 1, -2})) + // }) + + // It("should HPersist", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HPersist(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HPersist(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{0, 0, -1})) + + // res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, 1, -2})) + + // res, err = client1.HPersist(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, -1, -2})) + // }) + + // It("should HExpireTime", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HExpireTime(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, -2})) + + // res, err = client1.HExpireTime(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1234567890, -1, -2})) + // }) + + // It("should HPExpireTime", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HPExpireTime(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, -2})) + + // res, err = client1.HPExpireTime(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1234567890, -1, -2})) + // }) + + // It("should HTTL", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HTTL(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, -2})) + + // res, err = client1.HTTL(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{10, -1, -2})) + // }) + + // It("should HPTTL", func() { + // // For testing purposes only + // client1 := redis.NewClient(&redis.Options{ + // Addr: "localhost:6379", + // }) + // defer client1.Close() + + // _, err := client1.HPTTL(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + // Expect(err).To(HaveOccurred()) + // for i := 0; i < 100; i++ { + // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + // Expect(sadd.Err()).NotTo(HaveOccurred()) + // } + + // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{1, -2})) + + // res, err = client1.HPTTL(ctx, "myhash", 10, "key1", "key2", "key200").Result() + // Expect(err).NotTo(HaveOccurred()) + // Expect(res).To(Equal([]int64{10, -1, -2})) + // }) + + It("should HSetF and HGetF", func() { + // For testing purposes only // client1 := redis.NewClient(&redis.Options{ - // Addr: "ADDR", + // Addr: "localhost:6379", // }) // defer client1.Close() - res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(BeNil()) + sadd := client.HSetF(ctx, "myhash", redis.HSetFArgs{}, "field1", "Hello", "field2", "World") + Expect(sadd.Err()).NotTo(HaveOccurred()) + Expect(sadd.Val()).To(Equal([]string{"1", "1"})) + for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSetF(ctx, "myhash", redis.HSetFArgs{}, fmt.Sprintf("key%d", i), "Hello") Expect(sadd.Err()).NotTo(HaveOccurred()) + Expect(sadd.Val()).To(Equal([]string{"1"})) } - res, err = client.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + res, err := client.HSetF(ctx, "myhash", redis.HSetFArgs{GETOLD: true, EX: 10}, "field1", "Hello", "field2", "World", "field3", "foo").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(BeEquivalentTo([]string{"Hello", "World", ""})) + + res, err = client.HSetF(ctx, "myhash", redis.HSetFArgs{DOF: true}, "field1", "Hello", "field2", "World", "field4", "bar").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(BeEquivalentTo([]string{"0", "0", "1"})) + + res, err = client.HGetF(ctx, "myhash", redis.HGetFArgs{Persist: true}, "field1", "field2", "field3").Result() Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int{1, 1, -2})) + Expect(res).To(BeEquivalentTo([]string{"Hello", "World", "foo"})) + }) It("should ZScan", func() { diff --git a/hash_commands.go b/hash_commands.go index c215b12ce..863207383 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -187,7 +187,9 @@ type HExpireArgs struct { // The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. // For more information - https://redis.io/commands/hexpire/ func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIRE", key, expiration, "FIELDS", len(fields)} + //temp until fixed in redis + // args := []interface{}{"HEXPIRE", key, expiration, "FIELDS", len(fields)} + args := []interface{}{"HEXPIRE", key, expiration, len(fields)} for _, field := range fields { args = append(args, field) @@ -230,8 +232,10 @@ func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration tim // Similar to HExpire, it accepts a key, an expiration duration in milliseconds, a struct with expiration condition flags, and a list of fields. // The command modifies the standard time.Duration to milliseconds for the Redis command. // For more information - https://redis.io/commands/hpexpire/ -func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, expirationArgs HExpireArgs, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), "FIELDS", len(fields)} +func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { + //temp until fixed in redis + // args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), "FIELDS", len(fields)} + args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), len(fields)} for _, field := range fields { args = append(args, field) @@ -271,7 +275,9 @@ func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration ti // The command sets absolute expiration times based on the UNIX timestamp provided. // For more information - https://redis.io/commands/hexpireat/ func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIREAT", key, tm.Unix(), "FIELDS", len(fields)} + //temp until fixed in redis + // args := []interface{}{"HEXPIREAT", key, tm.Unix(), "FIELDS", len(fields)} + args := []interface{}{"HEXPIREAT", key, tm.Unix(), len(fields)} for _, field := range fields { args = append(args, field) @@ -310,7 +316,9 @@ func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time // Similar to HExpireAt but for timestamps in milliseconds. It accepts the same parameters and adjusts the UNIX time to milliseconds. // For more information - https://redis.io/commands/hpexpireat/ func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), "FIELDS", len(fields)} + //temp until fixed in redis + // args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), "FIELDS", len(fields)} + args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), len(fields)} for _, field := range fields { args = append(args, field) @@ -350,7 +358,9 @@ func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Tim // This command ensures that each field specified will have its expiration removed if present. // For more information - https://redis.io/commands/hpersist/ func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HPERSIST", key, count, "FIELDS", len(fields)} + //temp until fixed in redis + // args := []interface{}{"HPERSIST", key, count, "FIELDS", len(fields)} + args := []interface{}{"HPERSIST", key, count, len(fields)} for _, field := range fields { args = append(args, field) @@ -365,7 +375,9 @@ func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ... // This command returns the expiration times for each field or error/status codes for each field as specified. // For more information - https://redis.io/commands/hexpiretime/ func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIRETIME", key, count, "FIELDS", len(fields)} + //temp until fixed in redis + // args := []interface{}{"HEXPIRETIME", key, count, "FIELDS", len(fields)} + args := []interface{}{"HEXPIRETIME", key, count, len(fields)} for _, field := range fields { args = append(args, field) @@ -434,8 +446,8 @@ type HGetFArgs struct { // It also allows for the removal of the expiration time with the 'PERSIST' option. // The command constructs an argument list starting with "HGETF", followed by the key, the field, and any of the specified expiration settings. // For more information - https://redis.io/commands/hgetf/ -func (c cmdable) HGetF(ctx context.Context, key, field string, args HGetFArgs) *StringSliceCmd { - argsSlice := []interface{}{"HGETF", key, field} +func (c cmdable) HGetF(ctx context.Context, key string, args HGetFArgs, field ...string) *StringSliceCmd { + argsSlice := []interface{}{"HGETF", key} if args.Persist { argsSlice = append(argsSlice, "PERSIST") @@ -449,6 +461,10 @@ func (c cmdable) HGetF(ctx context.Context, key, field string, args HGetFArgs) * argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) } + argsSlice = append(argsSlice, "FIELDS", len(field)) + for _, f := range field { + argsSlice = append(argsSlice, f) + } cmd := NewStringSliceCmd(ctx, argsSlice...) _ = c(ctx, cmd) return cmd @@ -554,7 +570,7 @@ func (c cmdable) HSetF(ctx context.Context, key string, args HSetFArgs, fieldVal argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) } - argsSlice = append(argsSlice, "FVS", len(fieldValues)) + argsSlice = append(argsSlice, "FVS", len(fieldValues)/2) for _, fieldValue := range fieldValues { argsSlice = append(argsSlice, fieldValue) From de43ef8f8aa448ffbcc3a4fa120cb6c17e896dd6 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Wed, 15 May 2024 14:45:33 +0300 Subject: [PATCH 5/8] modify commands --- commands_test.go | 340 +++++++++++++++++++---------------------------- hash_commands.go | 49 +++---- 2 files changed, 159 insertions(+), 230 deletions(-) diff --git a/commands_test.go b/commands_test.go index c09f491e0..125c77e9a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1104,208 +1104,148 @@ var _ = Describe("Commands", func() { Expect(cursor).NotTo(BeZero()) }) - // It("should HExpire", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // res, err := client1.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, 1, -2})) - // }) - - // It("should HPExpire", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, 1, -2})) - // }) - - // It("should HExpireAt", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, 1, -2})) - // }) - - // It("should HPExpireAt", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, 1, -2})) - // }) - - // It("should HPersist", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HPersist(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HPersist(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{0, 0, -1})) - - // res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, 1, -2})) - - // res, err = client1.HPersist(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, -1, -2})) - // }) - - // It("should HExpireTime", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HExpireTime(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, -2})) - - // res, err = client1.HExpireTime(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1234567890, -1, -2})) - // }) - - // It("should HPExpireTime", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HPExpireTime(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, -2})) - - // res, err = client1.HPExpireTime(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1234567890, -1, -2})) - // }) - - // It("should HTTL", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HTTL(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, -2})) - - // res, err = client1.HTTL(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{10, -1, -2})) - // }) - - // It("should HPTTL", func() { - // // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() - - // _, err := client1.HPTTL(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - // Expect(err).To(HaveOccurred()) - // for i := 0; i < 100; i++ { - // sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - // Expect(sadd.Err()).NotTo(HaveOccurred()) - // } - - // res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{1, -2})) - - // res, err = client1.HPTTL(ctx, "myhash", 10, "key1", "key2", "key200").Result() - // Expect(err).NotTo(HaveOccurred()) - // Expect(res).To(Equal([]int64{10, -1, -2})) - // }) + It("should HExpire", func() { + res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } - It("should HSetF and HGetF", func() { - // For testing purposes only - // client1 := redis.NewClient(&redis.Options{ - // Addr: "localhost:6379", - // }) - // defer client1.Close() + res, err = client.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPExpire", func() { + _, err := client.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HExpireAt", func() { + _, err := client.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + res, err := client.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPExpireAt", func() { + _, err := client.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPersist", func() { + _, err := client.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{-1, -1, -2})) + + res, err = client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -1, -2})) + }) + + It("should HExpireTime", func() { + _, err := client.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client.HExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(BeNumerically("~", time.Now().Add(10*time.Second).Unix(), 1)) + }) + + It("should HPExpireTime", func() { + _, err := client.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client.HPExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(BeEquivalentTo([]int64{time.Now().Add(10 * time.Second).UnixMilli(), -1, -2})) + }) + + It("should HTTL", func() { + _, err := client.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client.HTTL(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{10, -1, -2})) + }) + + It("should HPTTL", func() { + _, err := client.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client.HPTTL(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(BeNumerically("~", 10*time.Second.Milliseconds(), 1)) + }) + + It("should HSetF and HGetF", func() { sadd := client.HSetF(ctx, "myhash", redis.HSetFArgs{}, "field1", "Hello", "field2", "World") Expect(sadd.Err()).NotTo(HaveOccurred()) Expect(sadd.Val()).To(Equal([]string{"1", "1"})) diff --git a/hash_commands.go b/hash_commands.go index 863207383..ab1be9329 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -187,8 +187,6 @@ type HExpireArgs struct { // The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. // For more information - https://redis.io/commands/hexpire/ func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HEXPIRE", key, expiration, "FIELDS", len(fields)} args := []interface{}{"HEXPIRE", key, expiration, len(fields)} for _, field := range fields { @@ -218,7 +216,7 @@ func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration tim args = append(args, "LT") } - args = append(args, "FIELDS", len(fields)) + args = append(args, len(fields)) for _, field := range fields { args = append(args, field) @@ -233,8 +231,6 @@ func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration tim // The command modifies the standard time.Duration to milliseconds for the Redis command. // For more information - https://redis.io/commands/hpexpire/ func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), "FIELDS", len(fields)} args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), len(fields)} for _, field := range fields { @@ -260,7 +256,7 @@ func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration ti args = append(args, "LT") } - args = append(args, "FIELDS", len(fields)) + args = append(args, len(fields)) for _, field := range fields { args = append(args, field) @@ -275,8 +271,7 @@ func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration ti // The command sets absolute expiration times based on the UNIX timestamp provided. // For more information - https://redis.io/commands/hexpireat/ func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HEXPIREAT", key, tm.Unix(), "FIELDS", len(fields)} + args := []interface{}{"HEXPIREAT", key, tm.Unix(), len(fields)} for _, field := range fields { @@ -302,7 +297,7 @@ func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time args = append(args, "LT") } - args = append(args, "FIELDS", len(fields)) + args = append(args, len(fields)) for _, field := range fields { args = append(args, field) @@ -316,8 +311,6 @@ func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time // Similar to HExpireAt but for timestamps in milliseconds. It accepts the same parameters and adjusts the UNIX time to milliseconds. // For more information - https://redis.io/commands/hpexpireat/ func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), "FIELDS", len(fields)} args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), len(fields)} for _, field := range fields { @@ -343,7 +336,7 @@ func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Tim args = append(args, "LT") } - args = append(args, "FIELDS", len(fields)) + args = append(args, len(fields)) for _, field := range fields { args = append(args, field) @@ -354,13 +347,11 @@ func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Tim } // HPersist - Removes the expiration time from specified fields in a hash. -// Accepts a key, the count of fields to affect, and the fields themselves. +// Accepts a key and the fields themselves. // This command ensures that each field specified will have its expiration removed if present. // For more information - https://redis.io/commands/hpersist/ -func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HPERSIST", key, count, "FIELDS", len(fields)} - args := []interface{}{"HPERSIST", key, count, len(fields)} +func (c cmdable) HPersist(ctx context.Context, key string, fields ...string) *IntSliceCmd { + args := []interface{}{"HPERSIST", key, len(fields)} for _, field := range fields { args = append(args, field) @@ -371,13 +362,11 @@ func (c cmdable) HPersist(ctx context.Context, key string, count int, fields ... } // HExpireTime - Retrieves the expiration time for specified fields in a hash as a UNIX timestamp in seconds. -// Requires a key, a count of fields, and the fields themselves to fetch their expiration timestamps. +// Requires a key and the fields themselves to fetch their expiration timestamps. // This command returns the expiration times for each field or error/status codes for each field as specified. // For more information - https://redis.io/commands/hexpiretime/ -func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - //temp until fixed in redis - // args := []interface{}{"HEXPIRETIME", key, count, "FIELDS", len(fields)} - args := []interface{}{"HEXPIRETIME", key, count, len(fields)} +func (c cmdable) HExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd { + args := []interface{}{"HEXPIRETIME", key, len(fields)} for _, field := range fields { args = append(args, field) @@ -391,8 +380,8 @@ func (c cmdable) HExpireTime(ctx context.Context, key string, count int, fields // Similar to HExpireTime, adjusted for timestamps in milliseconds. It requires the same parameters. // Provides the expiration timestamp for each field in milliseconds. // For more information - https://redis.io/commands/hexpiretime/ -func (c cmdable) HPExpireTime(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIRETIME", key, count, "FIELDS", len(fields)} +func (c cmdable) HPExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd { + args := []interface{}{"HPEXPIRETIME", key, len(fields)} for _, field := range fields { args = append(args, field) @@ -403,11 +392,11 @@ func (c cmdable) HPExpireTime(ctx context.Context, key string, count int, fields } // HTTL - Retrieves the remaining time to live for specified fields in a hash in seconds. -// Requires a key, the count of fields, and the fields themselves. It returns the TTL for each specified field. +// Requires a key and the fields themselves. It returns the TTL for each specified field. // This command fetches the TTL in seconds for each field or returns error/status codes as appropriate. // For more information - https://redis.io/commands/httl/ -func (c cmdable) HTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HTTL", key, count, "FIELDS", len(fields)} +func (c cmdable) HTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd { + args := []interface{}{"HTTL", key, len(fields)} for _, field := range fields { args = append(args, field) @@ -418,11 +407,11 @@ func (c cmdable) HTTL(ctx context.Context, key string, count int, fields ...stri } // HPTTL - Retrieves the remaining time to live for specified fields in a hash in milliseconds. -// Similar to HTTL, but returns the TTL in milliseconds. It requires a key, a count of fields, and the specified fields. +// Similar to HTTL, but returns the TTL in milliseconds. It requires a key and the specified fields. // This command provides the TTL in milliseconds for each field or returns error/status codes as needed. // For more information - https://redis.io/commands/hpttl/ -func (c cmdable) HPTTL(ctx context.Context, key string, count int, fields ...string) *IntSliceCmd { - args := []interface{}{"HPTTL", key, count, "FIELDS", len(fields)} +func (c cmdable) HPTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd { + args := []interface{}{"HPTTL", key, len(fields)} for _, field := range fields { args = append(args, field) From d1e68abd31eb866f50397a6d044f5afc1f399288 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 26 May 2024 12:49:58 +0300 Subject: [PATCH 6/8] api changes --- commands_test.go | 317 ++++++++++++++++++++++------------------------- hash_commands.go | 175 ++------------------------ 2 files changed, 164 insertions(+), 328 deletions(-) diff --git a/commands_test.go b/commands_test.go index 125c77e9a..58ead4af8 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1104,172 +1104,6 @@ var _ = Describe("Commands", func() { Expect(cursor).NotTo(BeZero()) }) - It("should HExpire", func() { - res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err = client.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, 1, -2})) - }) - - It("should HPExpire", func() { - _, err := client.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, 1, -2})) - }) - - It("should HExpireAt", func() { - _, err := client.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, 1, -2})) - }) - - It("should HPExpireAt", func() { - _, err := client.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, 1, -2})) - }) - - It("should HPersist", func() { - _, err := client.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{-1, -1, -2})) - - res, err = client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -2})) - - res, err = client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -1, -2})) - }) - - It("should HExpireTime", func() { - _, err := client.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -2})) - - res, err = client.HExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(BeNumerically("~", time.Now().Add(10*time.Second).Unix(), 1)) - }) - - It("should HPExpireTime", func() { - _, err := client.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -2})) - - res, err = client.HPExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(BeEquivalentTo([]int64{time.Now().Add(10 * time.Second).UnixMilli(), -1, -2})) - }) - - It("should HTTL", func() { - _, err := client.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -2})) - - res, err = client.HTTL(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{10, -1, -2})) - }) - - It("should HPTTL", func() { - _, err := client.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) - for i := 0; i < 100; i++ { - sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - } - - res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]int64{1, -2})) - - res, err = client.HPTTL(ctx, "myhash", "key1", "key2", "key200").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(BeNumerically("~", 10*time.Second.Milliseconds(), 1)) - }) - - It("should HSetF and HGetF", func() { - sadd := client.HSetF(ctx, "myhash", redis.HSetFArgs{}, "field1", "Hello", "field2", "World") - Expect(sadd.Err()).NotTo(HaveOccurred()) - Expect(sadd.Val()).To(Equal([]string{"1", "1"})) - - for i := 0; i < 100; i++ { - sadd := client.HSetF(ctx, "myhash", redis.HSetFArgs{}, fmt.Sprintf("key%d", i), "Hello") - Expect(sadd.Err()).NotTo(HaveOccurred()) - Expect(sadd.Val()).To(Equal([]string{"1"})) - } - - res, err := client.HSetF(ctx, "myhash", redis.HSetFArgs{GETOLD: true, EX: 10}, "field1", "Hello", "field2", "World", "field3", "foo").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(BeEquivalentTo([]string{"Hello", "World", ""})) - - res, err = client.HSetF(ctx, "myhash", redis.HSetFArgs{DOF: true}, "field1", "Hello", "field2", "World", "field4", "bar").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(BeEquivalentTo([]string{"0", "0", "1"})) - - res, err = client.HGetF(ctx, "myhash", redis.HGetFArgs{Persist: true}, "field1", "field2", "field3").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(BeEquivalentTo([]string{"Hello", "World", "foo"})) - - }) - It("should ZScan", func() { for i := 0; i < 1000; i++ { err := client.ZAdd(ctx, "myset", redis.Z{ @@ -2596,6 +2430,157 @@ var _ = Describe("Commands", func() { Equal([]redis.KeyValue{{Key: "key2", Value: "hello2"}}), )) }) + + It("should HExpire", Label("hash-expiration"), func() { + //redis client with port 6379 + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + res, err := client1.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPExpire", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HExpireAt", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPExpireAt", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, 1, -2})) + }) + + It("should HPersist", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{-1, -1, -2})) + + res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client1.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -1, -2})) + }) + + It("should HExpireTime", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client1.HExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(BeNumerically("~", time.Now().Add(10*time.Second).Unix(), 1)) + }) + + It("should HPExpireTime", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client1.HPExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(BeEquivalentTo([]int64{time.Now().Add(10 * time.Second).UnixMilli(), -1, -2})) + }) + + It("should HTTL", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client1.HTTL(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{10, -1, -2})) + }) + + It("should HPTTL", Label("hash-expiration"), func() { + client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) + _, err := client1.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(HaveOccurred()) + for i := 0; i < 100; i++ { + sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + Expect(sadd.Err()).NotTo(HaveOccurred()) + } + + res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]int64{1, -2})) + + res, err = client1.HPTTL(ctx, "myhash", "key1", "key2", "key200").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(BeNumerically("~", 10*time.Second.Milliseconds(), 1)) + }) }) Describe("hyperloglog", func() { diff --git a/hash_commands.go b/hash_commands.go index ab1be9329..59fbb8656 100644 --- a/hash_commands.go +++ b/hash_commands.go @@ -187,7 +187,7 @@ type HExpireArgs struct { // The command constructs an argument list starting with "HEXPIRE", followed by the key, duration, any conditional flags, and the specified fields. // For more information - https://redis.io/commands/hexpire/ func (c cmdable) HExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIRE", key, expiration, len(fields)} + args := []interface{}{"HEXPIRE", key, expiration, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -216,7 +216,7 @@ func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration tim args = append(args, "LT") } - args = append(args, len(fields)) + args = append(args, "FIELDS", len(fields)) for _, field := range fields { args = append(args, field) @@ -231,7 +231,7 @@ func (c cmdable) HExpireWithArgs(ctx context.Context, key string, expiration tim // The command modifies the standard time.Duration to milliseconds for the Redis command. // For more information - https://redis.io/commands/hpexpire/ func (c cmdable) HPExpire(ctx context.Context, key string, expiration time.Duration, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), len(fields)} + args := []interface{}{"HPEXPIRE", key, formatMs(ctx, expiration), "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -256,7 +256,7 @@ func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration ti args = append(args, "LT") } - args = append(args, len(fields)) + args = append(args, "FIELDS", len(fields)) for _, field := range fields { args = append(args, field) @@ -272,7 +272,7 @@ func (c cmdable) HPExpireWithArgs(ctx context.Context, key string, expiration ti // For more information - https://redis.io/commands/hexpireat/ func (c cmdable) HExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIREAT", key, tm.Unix(), len(fields)} + args := []interface{}{"HEXPIREAT", key, tm.Unix(), "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -297,7 +297,7 @@ func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time args = append(args, "LT") } - args = append(args, len(fields)) + args = append(args, "FIELDS", len(fields)) for _, field := range fields { args = append(args, field) @@ -311,7 +311,7 @@ func (c cmdable) HExpireAtWithArgs(ctx context.Context, key string, tm time.Time // Similar to HExpireAt but for timestamps in milliseconds. It accepts the same parameters and adjusts the UNIX time to milliseconds. // For more information - https://redis.io/commands/hpexpireat/ func (c cmdable) HPExpireAt(ctx context.Context, key string, tm time.Time, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), len(fields)} + args := []interface{}{"HPEXPIREAT", key, tm.UnixNano() / int64(time.Millisecond), "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -336,7 +336,7 @@ func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Tim args = append(args, "LT") } - args = append(args, len(fields)) + args = append(args, "FIELDS", len(fields)) for _, field := range fields { args = append(args, field) @@ -351,7 +351,7 @@ func (c cmdable) HPExpireAtWithArgs(ctx context.Context, key string, tm time.Tim // This command ensures that each field specified will have its expiration removed if present. // For more information - https://redis.io/commands/hpersist/ func (c cmdable) HPersist(ctx context.Context, key string, fields ...string) *IntSliceCmd { - args := []interface{}{"HPERSIST", key, len(fields)} + args := []interface{}{"HPERSIST", key, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -366,7 +366,7 @@ func (c cmdable) HPersist(ctx context.Context, key string, fields ...string) *In // This command returns the expiration times for each field or error/status codes for each field as specified. // For more information - https://redis.io/commands/hexpiretime/ func (c cmdable) HExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd { - args := []interface{}{"HEXPIRETIME", key, len(fields)} + args := []interface{}{"HEXPIRETIME", key, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -381,7 +381,7 @@ func (c cmdable) HExpireTime(ctx context.Context, key string, fields ...string) // Provides the expiration timestamp for each field in milliseconds. // For more information - https://redis.io/commands/hexpiretime/ func (c cmdable) HPExpireTime(ctx context.Context, key string, fields ...string) *IntSliceCmd { - args := []interface{}{"HPEXPIRETIME", key, len(fields)} + args := []interface{}{"HPEXPIRETIME", key, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -396,7 +396,7 @@ func (c cmdable) HPExpireTime(ctx context.Context, key string, fields ...string) // This command fetches the TTL in seconds for each field or returns error/status codes as appropriate. // For more information - https://redis.io/commands/httl/ func (c cmdable) HTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd { - args := []interface{}{"HTTL", key, len(fields)} + args := []interface{}{"HTTL", key, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -411,7 +411,7 @@ func (c cmdable) HTTL(ctx context.Context, key string, fields ...string) *IntSli // This command provides the TTL in milliseconds for each field or returns error/status codes as needed. // For more information - https://redis.io/commands/hpttl/ func (c cmdable) HPTTL(ctx context.Context, key string, fields ...string) *IntSliceCmd { - args := []interface{}{"HPTTL", key, len(fields)} + args := []interface{}{"HPTTL", key, "FIELDS", len(fields)} for _, field := range fields { args = append(args, field) @@ -420,152 +420,3 @@ func (c cmdable) HPTTL(ctx context.Context, key string, fields ...string) *IntSl _ = c(ctx, cmd) return cmd } - -type HGetFArgs struct { - Persist bool - EX time.Duration - PX time.Duration - EXAT time.Time - PXAT time.Time -} - -// HGetF - Retrieves the value of a specified field in a hash and optionally updates its expiration time. -// Accepts a key, a single field, and a struct containing various expiration parameters such as: -// EX (seconds), PX (milliseconds), EXAT (UNIX time in seconds), and PXAT (UNIX time in milliseconds). -// It also allows for the removal of the expiration time with the 'PERSIST' option. -// The command constructs an argument list starting with "HGETF", followed by the key, the field, and any of the specified expiration settings. -// For more information - https://redis.io/commands/hgetf/ -func (c cmdable) HGetF(ctx context.Context, key string, args HGetFArgs, field ...string) *StringSliceCmd { - argsSlice := []interface{}{"HGETF", key} - - if args.Persist { - argsSlice = append(argsSlice, "PERSIST") - } else if args.EX > 0 { - argsSlice = append(argsSlice, "EX", args.EX) - } else if args.PX > 0 { - argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) - } else if !args.EXAT.IsZero() { - argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) - } else if !args.PXAT.IsZero() { - argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) - } - - argsSlice = append(argsSlice, "FIELDS", len(field)) - for _, f := range field { - argsSlice = append(argsSlice, f) - } - cmd := NewStringSliceCmd(ctx, argsSlice...) - _ = c(ctx, cmd) - return cmd -} - -// HGetFWithArgs - Retrieves the value of a specified field in a hash with additional conditional expiration settings. -// Similar to HGetF but includes conditional flags (NX, XX, GT, LT) to determine when to apply the expiration settings. -// This function builds upon HGetF by adding these conditional flags, which control the setting of the expiration based on the current state of the field. -// For more information - https://redis.io/commands/hgetf/ -func (c cmdable) HGetFWithArgs(ctx context.Context, key, field string, expirationArgs HExpireArgs, args HGetFArgs) *StringSliceCmd { - argsSlice := []interface{}{"HGETF", key, field} - - // only if one argument is true, we can add it to the args - // if more than one argument is true, it will cause an error - if expirationArgs.NX { - argsSlice = append(argsSlice, "NX") - } else if expirationArgs.XX { - argsSlice = append(argsSlice, "XX") - } else if expirationArgs.GT { - argsSlice = append(argsSlice, "GT") - } else if expirationArgs.LT { - argsSlice = append(argsSlice, "LT") - } - - if args.Persist { - argsSlice = append(argsSlice, "PERSIST") - } else if args.EX > 0 { - argsSlice = append(argsSlice, "EX", args.EX) - } else if args.PX > 0 { - argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) - } else if !args.EXAT.IsZero() { - argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) - } else if !args.PXAT.IsZero() { - argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) - } - - cmd := NewStringSliceCmd(ctx, argsSlice...) - _ = c(ctx, cmd) - return cmd -} - -type HSetFArgs struct { - DC bool - DCF bool - DOF bool - NX bool - XX bool - GT bool - LT bool - GETNEW bool - GETOLD bool - EX time.Duration - PX time.Duration - EXAT time.Time - PXAT time.Time - KEEPTTL bool -} - -// HSetF - Sets field-value pairs in a hash with optional expiration and value retrieval settings. -// Accepts a key, a struct of options that includes directives for field creation and modification behavior (DC, DCF, DOF), -// expiration settings (EX, PX, EXAT, PXAT, KEEPTTL), and value retrieval options (GETNEW, GETOLD). -// The command supports conditional settings (NX, XX, GT, LT) to govern how and when fields are updated or expired based on their current state. -// It builds a command that can conditionally set values and expiration times, potentially returning the old or new values of fields. -// For more information - https://redis.io/commands/hsetf/ -func (c cmdable) HSetF(ctx context.Context, key string, args HSetFArgs, fieldValues ...string) *StringSliceCmd { - argsSlice := []interface{}{"HSETF", key} - - if args.DC { - argsSlice = append(argsSlice, "DC") - } - - if args.DCF { - argsSlice = append(argsSlice, "DCF") - } else if args.DOF { - argsSlice = append(argsSlice, "DOF") - } - - if args.NX { - argsSlice = append(argsSlice, "NX") - } else if args.XX { - argsSlice = append(argsSlice, "XX") - } else if args.GT { - argsSlice = append(argsSlice, "GT") - } else if args.LT { - argsSlice = append(argsSlice, "LT") - } - - if args.GETNEW { - argsSlice = append(argsSlice, "GETNEW") - } else if args.GETOLD { - argsSlice = append(argsSlice, "GETOLD") - } - - if args.KEEPTTL { - argsSlice = append(argsSlice, "KEEPTTL") - } else if args.EX > 0 { - argsSlice = append(argsSlice, "EX", args.EX) - } else if args.PX > 0 { - argsSlice = append(argsSlice, "PX", formatMs(ctx, args.PX)) - } else if !args.EXAT.IsZero() { - argsSlice = append(argsSlice, "EXAT", args.EXAT.Unix()) - } else if !args.PXAT.IsZero() { - argsSlice = append(argsSlice, "PXAT", args.PXAT.UnixNano()/int64(time.Millisecond)) - } - - argsSlice = append(argsSlice, "FVS", len(fieldValues)/2) - - for _, fieldValue := range fieldValues { - argsSlice = append(argsSlice, fieldValue) - } - - cmd := NewStringSliceCmd(ctx, argsSlice...) - _ = c(ctx, cmd) - return cmd -} From b6f04d3ca977e2d5357c736cc06b603645bfb2d2 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 20 Jun 2024 00:31:23 +0300 Subject: [PATCH 7/8] fix tests --- commands_test.go | 101 +++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/commands_test.go b/commands_test.go index 58ead4af8..8559e4f8d 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2432,152 +2432,149 @@ var _ = Describe("Commands", func() { }) It("should HExpire", Label("hash-expiration"), func() { - //redis client with port 6379 - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - res, err := client1.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + res, err = client.HExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, 1, -2})) }) It("should HPExpire", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + _, err := client.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() + res, err := client.HPExpire(ctx, "myhash", 10, "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, 1, -2})) }) It("should HExpireAt", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + res, err := client.HExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, 1, -2})) }) It("should HPExpireAt", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() + res, err := client.HPExpireAt(ctx, "myhash", time.Now().Add(10*time.Second), "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, 1, -2})) }) It("should HPersist", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + res, err := client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{-1, -1, -2})) - res, err = client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + res, err = client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -2})) - res, err = client1.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() + res, err = client.HPersist(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -1, -2})) }) It("should HExpireTime", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -2})) - res, err = client1.HExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + res, err = client.HExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res[0]).To(BeNumerically("~", time.Now().Add(10*time.Second).Unix(), 1)) }) It("should HPExpireTime", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -2})) - res, err = client1.HPExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() + res, err = client.HPExpireTime(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(BeEquivalentTo([]int64{time.Now().Add(10 * time.Second).UnixMilli(), -1, -2})) }) It("should HTTL", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -2})) - res, err = client1.HTTL(ctx, "myhash", "key1", "key2", "key200").Result() + res, err = client.HTTL(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{10, -1, -2})) }) It("should HPTTL", Label("hash-expiration"), func() { - client1 := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) - _, err := client1.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() - Expect(err).To(HaveOccurred()) + + _, err := client.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() + Expect(err).To(BeNil()) for i := 0; i < 100; i++ { - sadd := client1.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") + sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello") Expect(sadd.Err()).NotTo(HaveOccurred()) } - res, err := client1.HExpire(ctx, "myhash", 10, "key1", "key200").Result() + res, err := client.HExpire(ctx, "myhash", 10, "key1", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal([]int64{1, -2})) - res, err = client1.HPTTL(ctx, "myhash", "key1", "key2", "key200").Result() + res, err = client.HPTTL(ctx, "myhash", "key1", "key2", "key200").Result() Expect(err).NotTo(HaveOccurred()) Expect(res[0]).To(BeNumerically("~", 10*time.Second.Milliseconds(), 1)) }) From b2f2bcf00012e980d1d838e102641c9f4ab0592e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 20 Jun 2024 00:36:37 +0300 Subject: [PATCH 8/8] remove tests from RE --- commands_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/commands_test.go b/commands_test.go index 8559e4f8d..b0224449b 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2431,7 +2431,7 @@ var _ = Describe("Commands", func() { )) }) - It("should HExpire", Label("hash-expiration"), func() { + It("should HExpire", Label("hash-expiration", "NonRedisEnterprise"), func() { res, err := client.HExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() Expect(err).To(BeNil()) for i := 0; i < 100; i++ { @@ -2444,7 +2444,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{1, 1, -2})) }) - It("should HPExpire", Label("hash-expiration"), func() { + It("should HPExpire", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HPExpire(ctx, "no_such_key", 10, "field1", "field2", "field3").Result() Expect(err).To(BeNil()) for i := 0; i < 100; i++ { @@ -2457,7 +2457,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{1, 1, -2})) }) - It("should HExpireAt", Label("hash-expiration"), func() { + It("should HExpireAt", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2471,7 +2471,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{1, 1, -2})) }) - It("should HPExpireAt", Label("hash-expiration"), func() { + It("should HPExpireAt", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HPExpireAt(ctx, "no_such_key", time.Now().Add(10*time.Second), "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2485,7 +2485,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{1, 1, -2})) }) - It("should HPersist", Label("hash-expiration"), func() { + It("should HPersist", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HPersist(ctx, "no_such_key", "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2507,7 +2507,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{1, -1, -2})) }) - It("should HExpireTime", Label("hash-expiration"), func() { + It("should HExpireTime", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2525,7 +2525,7 @@ var _ = Describe("Commands", func() { Expect(res[0]).To(BeNumerically("~", time.Now().Add(10*time.Second).Unix(), 1)) }) - It("should HPExpireTime", Label("hash-expiration"), func() { + It("should HPExpireTime", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HPExpireTime(ctx, "no_such_key", "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2543,7 +2543,7 @@ var _ = Describe("Commands", func() { Expect(res).To(BeEquivalentTo([]int64{time.Now().Add(10 * time.Second).UnixMilli(), -1, -2})) }) - It("should HTTL", Label("hash-expiration"), func() { + It("should HTTL", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() Expect(err).To(BeNil()) @@ -2561,7 +2561,7 @@ var _ = Describe("Commands", func() { Expect(res).To(Equal([]int64{10, -1, -2})) }) - It("should HPTTL", Label("hash-expiration"), func() { + It("should HPTTL", Label("hash-expiration", "NonRedisEnterprise"), func() { _, err := client.HPTTL(ctx, "no_such_key", "field1", "field2", "field3").Result() Expect(err).To(BeNil())