Skip to content

Commit 9d1e8a5

Browse files
authored
Merge pull request #1263 from go-redis/fix/hset-hmset
Rename HMSet to HSet and restore old HMSet
2 parents 726f680 + 7df36b4 commit 9d1e8a5

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v7.2
4+
5+
- Existing `HMSet` is renamed to `HSet` and old deprecated `HMSet` is restored for Redis 3 users.
6+
37
## v7
48

59
- *Important*. Tx.Pipeline now returns a non-transactional pipeline. Use Tx.TxPipeline for a transactional pipeline.
@@ -11,6 +15,7 @@
1115
- Add PubSub.ChannelWithSubscriptions that sends `*Subscription` in addition to `*Message` to allow detecting reconnections.
1216
- `time.Time` is now marshalled in RFC3339 format. `rdb.Get("foo").Time()` helper is added to parse the time.
1317
- `SetLimiter` is removed and added `Options.Limiter` instead.
18+
- `HMSet` is deprecated as of Redis v4.
1419

1520
## v6.15
1621

commands.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ type Cmdable interface {
130130
HKeys(key string) *StringSliceCmd
131131
HLen(key string) *IntCmd
132132
HMGet(key string, fields ...string) *SliceCmd
133-
HMSet(key string, values ...interface{}) *IntCmd
134-
HSet(key, field string, value interface{}) *BoolCmd
133+
HSet(key string, values ...interface{}) *IntCmd
134+
HMSet(key string, values ...interface{}) *BoolCmd
135135
HSetNX(key, field string, value interface{}) *BoolCmd
136136
HVals(key string) *StringSliceCmd
137137
BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
@@ -983,13 +983,13 @@ func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
983983
return cmd
984984
}
985985

986-
// HMSet is like HSet, but accepts multiple values:
986+
// HSet accepts values in following formats:
987987
// - HMSet("myhash", "key1", "value1", "key2", "value2")
988988
// - HMSet("myhash", []string{"key1", "value1", "key2", "value2"})
989989
// - HMSet("myhash", map[string]interface{}{"key1": "value1", "key2": "value2"})
990990
//
991-
// Note that it uses HSET Redis command underneath because HMSET is deprecated.
992-
func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
991+
// Note that it requires Redis v4 for multiple field/value pairs support.
992+
func (c cmdable) HSet(key string, values ...interface{}) *IntCmd {
993993
args := make([]interface{}, 2, 2+len(values))
994994
args[0] = "hset"
995995
args[1] = key
@@ -999,8 +999,13 @@ func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
999999
return cmd
10001000
}
10011001

1002-
func (c cmdable) HSet(key, field string, value interface{}) *BoolCmd {
1003-
cmd := NewBoolCmd("hset", key, field, value)
1002+
// HMSet is a deprecated version of HSet left for compatibility with Redis 3.
1003+
func (c cmdable) HMSet(key string, values ...interface{}) *BoolCmd {
1004+
args := make([]interface{}, 2, 2+len(values))
1005+
args[0] = "hmset"
1006+
args[1] = key
1007+
args = appendArgs(args, values)
1008+
cmd := NewBoolCmd(args...)
10041009
_ = c(cmd)
10051010
return cmd
10061011
}

commands_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,15 +1325,15 @@ var _ = Describe("Commands", func() {
13251325
It("should HIncrByFloat", func() {
13261326
hSet := client.HSet("hash", "field", "10.50")
13271327
Expect(hSet.Err()).NotTo(HaveOccurred())
1328-
Expect(hSet.Val()).To(Equal(true))
1328+
Expect(hSet.Val()).To(Equal(int64(1)))
13291329

13301330
hIncrByFloat := client.HIncrByFloat("hash", "field", 0.1)
13311331
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
13321332
Expect(hIncrByFloat.Val()).To(Equal(10.6))
13331333

13341334
hSet = client.HSet("hash", "field", "5.0e3")
13351335
Expect(hSet.Err()).NotTo(HaveOccurred())
1336-
Expect(hSet.Val()).To(Equal(false))
1336+
Expect(hSet.Val()).To(Equal(int64(0)))
13371337

13381338
hIncrByFloat = client.HIncrByFloat("hash", "field", 2.0e2)
13391339
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
@@ -1367,16 +1367,16 @@ var _ = Describe("Commands", func() {
13671367
})
13681368

13691369
It("should HMGet", func() {
1370-
err := client.HMSet("hash", "key1", "hello1", "key2", "hello2").Err()
1370+
err := client.HSet("hash", "key1", "hello1", "key2", "hello2").Err()
13711371
Expect(err).NotTo(HaveOccurred())
13721372

13731373
vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
13741374
Expect(err).NotTo(HaveOccurred())
13751375
Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
13761376
})
13771377

1378-
It("should HMSet", func() {
1379-
ok, err := client.HMSet("hash", map[string]interface{}{
1378+
It("should HSet", func() {
1379+
ok, err := client.HSet("hash", map[string]interface{}{
13801380
"key1": "hello1",
13811381
"key2": "hello2",
13821382
}).Result()
@@ -1399,7 +1399,7 @@ var _ = Describe("Commands", func() {
13991399
It("should HSet", func() {
14001400
hSet := client.HSet("hash", "key", "hello")
14011401
Expect(hSet.Err()).NotTo(HaveOccurred())
1402-
Expect(hSet.Val()).To(Equal(true))
1402+
Expect(hSet.Val()).To(Equal(int64(1)))
14031403

14041404
hGet := client.HGet("hash", "key")
14051405
Expect(hGet.Err()).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)