Skip to content

Commit c7ff15e

Browse files
committed
feat: add hstrlen command for hash
Signed-off-by: rfyiamcool <[email protected]>
1 parent a1f16f2 commit c7ff15e

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

commands_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,9 +2421,10 @@ var _ = Describe("Commands", func() {
24212421

24222422
hStrLen := client.HStrLen(ctx, "hash", "key")
24232423
Expect(hStrLen.Err()).NotTo(HaveOccurred())
2424-
Expect(hStrLen.Val()).NotTo(Equal(int64(len("hello"))))
2424+
Expect(hStrLen.Val()).To(Equal(int64(len("hello"))))
24252425

2426-
nonHStrLen := client.HGet(ctx, "hash", "key1")
2426+
nonHStrLen := client.HGet(ctx, "hash", "keyNon")
2427+
Expect(hStrLen.Err()).NotTo(HaveOccurred())
24272428
Expect(nonHStrLen.Val()).To(Equal(int64(0)))
24282429

24292430
hDel := client.HDel(ctx, "hash", "key")

example/del-keys-without-ttl/main.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"sync"
77
"time"
88

9-
"go.uber.org/zap"
10-
119
"github.com/redis/go-redis/v9"
10+
"go.uber.org/zap"
1211
)
1312

1413
func main() {
@@ -22,6 +21,25 @@ func main() {
2221
_ = rdb.Set(ctx, "key_without_ttl_1", "", 0).Err()
2322
_ = rdb.Set(ctx, "key_without_ttl_2", "", 0).Err()
2423

24+
rdb.HSet(ctx, "hash", "f1", "hello")
25+
res := rdb.HStrLen(ctx, "hash", "f1")
26+
fmt.Println(res.Val(), res.Err())
27+
28+
res = rdb.HStrLen(ctx, "hash", "f2")
29+
fmt.Println(res.Val(), res.Err())
30+
31+
bb, err := rdb.HStrLen(ctx, "hash", "xxxx").Result()
32+
fmt.Println("hstrlen non", bb, err)
33+
34+
dd, err := rdb.HExists(ctx, "hash", "non-extext").Result()
35+
fmt.Println("exits non", dd, err)
36+
37+
incr, err := rdb.HIncrBy(ctx, "hash", "xxx", 1).Result()
38+
fmt.Println("incr non", incr, err)
39+
40+
aa, err := rdb.HGet(ctx, "hash", "hgetxx").Result()
41+
fmt.Println("hget non", aa, err)
42+
2543
checker := NewKeyChecker(rdb, 100)
2644

2745
start := time.Now()

hash_commands.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type HashCmdable interface {
66
HDel(ctx context.Context, key string, fields ...string) *IntCmd
77
HExists(ctx context.Context, key, field string) *BoolCmd
88
HGet(ctx context.Context, key, field string) *StringCmd
9-
HStrLen(ctx context.Context, key, field string) *IntCmd
109
HGetAll(ctx context.Context, key string) *MapStringStringCmd
1110
HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd
1211
HIncrByFloat(ctx context.Context, key, field string, incr float64) *FloatCmd
@@ -20,6 +19,7 @@ type HashCmdable interface {
2019
HVals(ctx context.Context, key string) *StringSliceCmd
2120
HRandField(ctx context.Context, key string, count int) *StringSliceCmd
2221
HRandFieldWithValues(ctx context.Context, key string, count int) *KeyValueSliceCmd
22+
HStrLen(ctx context.Context, key, field string) *IntCmd
2323
}
2424

2525
func (c cmdable) HDel(ctx context.Context, key string, fields ...string) *IntCmd {
@@ -46,12 +46,6 @@ func (c cmdable) HGet(ctx context.Context, key, field string) *StringCmd {
4646
return cmd
4747
}
4848

49-
func (c cmdable) HStrLen(ctx context.Context, key, field string) *IntCmd {
50-
cmd := NewIntCmd(ctx, "hstrlen", key, field)
51-
_ = c(ctx, cmd)
52-
return cmd
53-
}
54-
5549
func (c cmdable) HGetAll(ctx context.Context, key string) *MapStringStringCmd {
5650
cmd := NewMapStringStringCmd(ctx, "hgetall", key)
5751
_ = c(ctx, cmd)
@@ -179,3 +173,9 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str
179173
_ = c(ctx, cmd)
180174
return cmd
181175
}
176+
177+
func (c cmdable) HStrLen(ctx context.Context, key, field string) *IntCmd {
178+
cmd := NewIntCmd(ctx, "hstrlen", key, field)
179+
_ = c(ctx, cmd)
180+
return cmd
181+
}

0 commit comments

Comments
 (0)