From f8b88dcce2aecf6a0c89544a83694789a662b035 Mon Sep 17 00:00:00 2001 From: Feng Zhou Date: Tue, 22 Dec 2020 15:22:11 +0800 Subject: [PATCH 1/3] Add NewTransaction and AreKeysInSameSlot. --- cluster.go | 10 ++++++++++ redis.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cluster.go b/cluster.go index a6ce5c5846..e536d0195e 100644 --- a/cluster.go +++ b/cluster.go @@ -1423,6 +1423,16 @@ func (c *ClusterClient) cmdsMoved( return nil } +// NewTransation returns a transaction, the related slot is determined by key. +func (c *ClusterClient) NewTransation(ctx context.Context, key string) (*Tx, error) { + slot := hashtag.Slot(key) + node, err := c.slotMasterNode(ctx, slot) + if err != nil { + return nil, err + } + return node.Client.newTx(ctx), nil +} + func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error { if len(keys) == 0 { return fmt.Errorf("redis: Watch requires at least one key") diff --git a/redis.go b/redis.go index 712579d4ca..36065d355e 100644 --- a/redis.go +++ b/redis.go @@ -8,6 +8,7 @@ import ( "time" "github.com/go-redis/redis/v8/internal" + "github.com/go-redis/redis/v8/internal/hashtag" "github.com/go-redis/redis/v8/internal/pool" "github.com/go-redis/redis/v8/internal/proto" "go.opentelemetry.io/otel/label" @@ -790,3 +791,16 @@ func (c *Conn) TxPipeline() Pipeliner { pipe.init() return &pipe } + +func AreKeysInSameSlot(keys ...string) bool { + if len(keys) == 0 { + return true + } + slot := hashtag.Slot(keys[0]) + for _, key := range keys[1:] { + if hashtag.Slot(key) != slot { + return false + } + } + return true +} From 264b1078ed0962e7fad94b2483e1b049c060cf95 Mon Sep 17 00:00:00 2001 From: Feng Zhou Date: Fri, 29 Jul 2022 10:15:15 +0800 Subject: [PATCH 2/3] Update cluster.go. --- cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cluster.go b/cluster.go index 948bfce5d3..5ec48c5863 100644 --- a/cluster.go +++ b/cluster.go @@ -1414,7 +1414,7 @@ func (c *ClusterClient) NewTransation(ctx context.Context, key string) (*Tx, err if err != nil { return nil, err } - return node.Client.newTx(ctx), nil + return node.Client.newTx(), nil } func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error { From 7c12c674bebbef725e77b838d39550d81b49fcd7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 5 Dec 2022 03:10:43 +0000 Subject: [PATCH 3/3] chore(deps): bump github.com/cespare/xxhash/v2 from 2.1.2 to 2.2.0 Bumps [github.com/cespare/xxhash/v2](https://github.com/cespare/xxhash) from 2.1.2 to 2.2.0. - [Release notes](https://github.com/cespare/xxhash/releases) - [Commits](https://github.com/cespare/xxhash/compare/v2.1.2...v2.2.0) --- updated-dependencies: - dependency-name: github.com/cespare/xxhash/v2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 927841e0d3..faa9023937 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/go-redis/redis/v9 go 1.17 require ( - github.com/cespare/xxhash/v2 v2.1.2 + github.com/cespare/xxhash/v2 v2.2.0 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.20.0 diff --git a/go.sum b/go.sum index 224bfa9034..e7213528b4 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=