Skip to content

Commit f8b88dc

Browse files
author
Feng Zhou
committed
Add NewTransaction and AreKeysInSameSlot.
1 parent 9cd2176 commit f8b88dc

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cluster.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,16 @@ func (c *ClusterClient) cmdsMoved(
14231423
return nil
14241424
}
14251425

1426+
// NewTransation returns a transaction, the related slot is determined by key.
1427+
func (c *ClusterClient) NewTransation(ctx context.Context, key string) (*Tx, error) {
1428+
slot := hashtag.Slot(key)
1429+
node, err := c.slotMasterNode(ctx, slot)
1430+
if err != nil {
1431+
return nil, err
1432+
}
1433+
return node.Client.newTx(ctx), nil
1434+
}
1435+
14261436
func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
14271437
if len(keys) == 0 {
14281438
return fmt.Errorf("redis: Watch requires at least one key")

redis.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/go-redis/redis/v8/internal"
11+
"github.com/go-redis/redis/v8/internal/hashtag"
1112
"github.com/go-redis/redis/v8/internal/pool"
1213
"github.com/go-redis/redis/v8/internal/proto"
1314
"go.opentelemetry.io/otel/label"
@@ -790,3 +791,16 @@ func (c *Conn) TxPipeline() Pipeliner {
790791
pipe.init()
791792
return &pipe
792793
}
794+
795+
func AreKeysInSameSlot(keys ...string) bool {
796+
if len(keys) == 0 {
797+
return true
798+
}
799+
slot := hashtag.Slot(keys[0])
800+
for _, key := range keys[1:] {
801+
if hashtag.Slot(key) != slot {
802+
return false
803+
}
804+
}
805+
return true
806+
}

0 commit comments

Comments
 (0)