Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add transaction support for redis pipeline #1367

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Counter interface {
}

type PipeLiner interface {
Pipeline() redis.PipeClient
Pipeline(tx bool) redis.PipeClient
}

type Expirer interface {
Expand Down
16 changes: 8 additions & 8 deletions pkg/cache/mock/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/cache/testing/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *inMemoryCache) Expire(key string, expiration time.Duration) (bool, erro
return true, nil
}

func (c *inMemoryCache) Pipeline() redis.PipeClient {
func (c *inMemoryCache) Pipeline(tx bool) redis.PipeClient {
// TODO: implement
return nil
}
4 changes: 2 additions & 2 deletions pkg/cache/v3/redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (r *redisCache) PFAdd(key string, els ...string) (int64, error) {
return r.client.PFAdd(key, els...)
}

func (r *redisCache) Pipeline() redis.PipeClient {
return r.client.Pipeline()
func (r *redisCache) Pipeline(tx bool) redis.PipeClient {
return r.client.Pipeline(tx)
}

func (r *redisCache) Expire(key string, expiration time.Duration) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/redis/v3/mock/redis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions pkg/redis/v3/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type Client interface {
IncrByFloat(key string, value float64) (float64, error)
Del(key string) error
Incr(key string) (int64, error)
Pipeline() PipeClient
Pipeline(tx bool) PipeClient
Expire(key string, expiration time.Duration) (bool, error)
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *goredis.Cmd
Expand All @@ -112,6 +112,7 @@ type PipeClient interface {
}

type pipeClient struct {
ctx context.Context
pipe goredis.Pipeliner
cmds []string
opts *options
Expand Down Expand Up @@ -661,9 +662,17 @@ func (c *client) Eval(ctx context.Context, script string, keys []string, args ..
return c.rc.Eval(context.TODO(), script, keys, args...)
}

func (c *client) Pipeline() PipeClient {
func (c *client) Pipeline(tx bool) PipeClient {
var pipeliner goredis.Pipeliner
if tx {
// All commands in the transaction will either all succeed or none will be applied if an error occurs.
pipeliner = c.rc.TxPipeline()
} else {
c.rc.Pipeline()
}
return &pipeClient{
pipe: c.rc.Pipeline(),
ctx: context.TODO(),
pipe: pipeliner,
cmds: []string{},
opts: c.opts,
logger: c.logger,
Expand All @@ -672,17 +681,17 @@ func (c *client) Pipeline() PipeClient {

func (c *pipeClient) Incr(key string) *goredis.IntCmd {
c.cmds = append(c.cmds, incrCmdName)
return c.pipe.Incr(context.TODO(), key)
return c.pipe.Incr(c.ctx, key)
}

func (c *pipeClient) PFAdd(key string, els ...string) *goredis.IntCmd {
c.cmds = append(c.cmds, pfAddCmdName)
return c.pipe.PFAdd(context.TODO(), key, els)
return c.pipe.PFAdd(c.ctx, key, els)
}

func (c *pipeClient) TTL(key string) *goredis.DurationCmd {
c.cmds = append(c.cmds, ttlCmdName)
return c.pipe.TTL(context.TODO(), key)
return c.pipe.TTL(c.ctx, key)
}

func (c *pipeClient) Exec() ([]goredis.Cmder, error) {
Expand All @@ -692,7 +701,7 @@ func (c *pipeClient) Exec() ([]goredis.Cmder, error) {
cmdName += fmt.Sprintf("_%s", cmd)
}
redis.ReceivedCounter.WithLabelValues(clientVersion, c.opts.serverName, cmdName).Inc()
v, err := c.pipe.Exec(context.TODO())
v, err := c.pipe.Exec(c.ctx)
code := redis.CodeFail
switch err {
case nil:
Expand All @@ -708,17 +717,17 @@ func (c *pipeClient) Exec() ([]goredis.Cmder, error) {

func (c *pipeClient) PFCount(keys ...string) *goredis.IntCmd {
c.cmds = append(c.cmds, pfCountCmdName)
return c.pipe.PFCount(context.TODO(), keys...)
return c.pipe.PFCount(c.ctx, keys...)
}

func (c *pipeClient) Get(key string) *goredis.StringCmd {
c.cmds = append(c.cmds, getCmdName)
return c.pipe.Get(context.TODO(), key)
return c.pipe.Get(c.ctx, key)
}

func (c *pipeClient) Del(key string) *goredis.IntCmd {
c.cmds = append(c.cmds, pfCountCmdName)
return c.pipe.Del(context.TODO(), key)
return c.pipe.Del(c.ctx, key)
}

func (c *client) Dump(key string) (string, error) {
Expand Down
Loading