-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathop_name.go
More file actions
78 lines (66 loc) · 1.76 KB
/
op_name.go
File metadata and controls
78 lines (66 loc) · 1.76 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package rueidisotel
import (
"context"
"strings"
"github.com/redis/rueidis"
)
type OpNameResolver interface {
OpName(ctx context.Context, cmd rueidis.Completed) string
MultiOpName(ctx context.Context, cmds rueidis.Commands) string
MultiCacheableOpName(ctx context.Context, cmds []rueidis.CacheableTTL) string
}
var _ OpNameResolver = (*DefaultOpNameResolver)(nil)
type DefaultOpNameResolver struct {
// Limit controls how many elements are used to compose the resulting operation name.
// If Limit is greater than zero, only the first Limit commands are used.
Limit int
}
func (DefaultOpNameResolver) OpName(_ context.Context, cmd rueidis.Completed) string {
return cmd.Commands()[0]
}
func (r DefaultOpNameResolver) MultiOpName(_ context.Context, cmds rueidis.Commands) string {
if len(cmds) == 0 {
return ""
}
if r.Limit > 0 && len(cmds) > r.Limit {
cmds = cmds[:r.Limit]
}
if len(cmds) == 1 {
return cmds[0].Commands()[0]
}
size := len(cmds) - 1
for _, cmd := range cmds {
size += len(cmd.Commands()[0])
}
sb := strings.Builder{}
sb.Grow(size)
sb.WriteString(cmds[0].Commands()[0])
for _, cmd := range cmds[1:] {
sb.WriteRune(' ')
sb.WriteString(cmd.Commands()[0])
}
return sb.String()
}
func (r DefaultOpNameResolver) MultiCacheableOpName(_ context.Context, cmds []rueidis.CacheableTTL) string {
if len(cmds) == 0 {
return ""
}
if r.Limit > 0 && len(cmds) > r.Limit {
cmds = cmds[:r.Limit]
}
if len(cmds) == 1 {
return cmds[0].Cmd.Commands()[0]
}
size := len(cmds) - 1
for _, cmd := range cmds {
size += len(cmd.Cmd.Commands()[0])
}
sb := strings.Builder{}
sb.Grow(size)
sb.WriteString(cmds[0].Cmd.Commands()[0])
for _, cmd := range cmds[1:] {
sb.WriteRune(' ')
sb.WriteString(cmd.Cmd.Commands()[0])
}
return sb.String()
}