-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisPipeline.go
43 lines (36 loc) · 1.04 KB
/
redisPipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package redis
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
)
type redisPipeline struct {
*redisManager
}
func (receiver *redisPipeline) Transaction(executeFn func()) error {
var err error
traceDetail := receiver.traceManager.TraceRedis("TxPipeline", "", "")
defer func() { traceDetail.End(err) }()
// 开启事务
txPipeline := receiver.GetClient().TxPipeline()
routineRedisClient.Set(txPipeline)
defer func() { routineRedisClient.Remove() }()
executeFn()
_, err = txPipeline.Exec(context.Background())
return err
}
func (receiver *redisPipeline) Pipeline(executeFn func()) (PipelineCmder, error) {
var err error
traceDetail := receiver.traceManager.TraceRedis("Pipeline", "", "")
defer func() { traceDetail.End(err) }()
// 开启管道
txPipeline := receiver.GetClient().Pipeline()
routineRedisClient.Set(txPipeline)
defer func() { routineRedisClient.Remove() }()
executeFn()
result, err := txPipeline.Exec(context.Background())
if errors.Is(err, redis.Nil) {
err = nil
}
return PipelineCmder{cmder: result}, err
}