-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8.go
201 lines (177 loc) · 4.4 KB
/
v8.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package redis
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"github.com/go-redis/redis/v8"
"github.com/team-ide/go-tool/util"
"golang.org/x/crypto/ssh"
"net"
"sort"
"strings"
"time"
)
// NewRedisService 创建客户端
func NewRedisService(config *Config) (IService, error) {
service := &V8Service{
Config: config,
CmdService: &CmdService{},
}
service.CmdService.GetClient = service.getClient
err := service.init(config.SSHClient)
if err != nil {
return nil, err
}
return service, nil
}
type V8Service struct {
*Config
client *redis.Client
*CmdService
}
func (this_ *V8Service) init(sshClient *ssh.Client) (err error) {
options := &redis.Options{
Addr: this_.Address,
DialTimeout: 100 * time.Second,
ReadTimeout: 100 * time.Second,
WriteTimeout: 100 * time.Second,
Password: this_.Auth,
Username: this_.Username,
}
if this_.CertPath != "" {
certPool := x509.NewCertPool()
var pemCerts []byte
pemCerts, err = util.ReadFile(this_.CertPath)
if err != nil {
return
}
if !certPool.AppendCertsFromPEM(pemCerts) {
err = errors.New("证书[" + this_.CertPath + "]解析失败")
return
}
TLSClientConfig := &tls.Config{
InsecureSkipVerify: true,
}
TLSClientConfig.RootCAs = certPool
options.TLSConfig = TLSClientConfig
}
if sshClient != nil {
options.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, e := sshClient.Dial("tcp", addr)
return &util.SSHChanConn{Conn: conn}, e
}
}
client := redis.NewClient(options)
this_.client = client
return
}
func (this_ *V8Service) Close() {
if this_ != nil && this_.client != nil {
_ = this_.client.Close()
}
}
func formatParam(param *Param) *Param {
if param == nil {
param = &Param{}
}
if param.Ctx == nil {
param.Ctx = context.Background()
}
return param
}
func (this_ *V8Service) getClient(param *Param) (client redis.Cmdable, err error) {
param = formatParam(param)
cmd := this_.client.Do(param.Ctx, "select", param.Database)
_, err = cmd.Result()
if err != nil {
return
}
client = this_.client
return
}
func (this_ *V8Service) GetClient(args ...Arg) (client redis.Cmdable, err error) {
argCache := getArgCache(args...)
param := formatParam(argCache.Param)
return this_.getClient(param)
}
func (this_ *V8Service) Keys(pattern string, args ...Arg) (keysResult *KeysResult, err error) {
argCache := getArgCache(args...)
param := formatParam(argCache.Param)
client, err := this_.GetClient(param)
if err != nil {
return
}
var size = -1
if argCache.SizeArg != nil {
size = argCache.SizeArg.Size
}
return Keys(param.Ctx, client, param.Database, pattern, size)
}
func (this_ *V8Service) GetValueInfo(key string, args ...Arg) (valueInfo *ValueInfo, err error) {
argCache := getArgCache(args...)
param := formatParam(argCache.Param)
client, err := this_.GetClient(param)
if err != nil {
return
}
var valueStart int64 = -1
var valueSize int64 = -1
if argCache.StartArg != nil {
valueStart = int64(argCache.StartArg.Start)
}
if argCache.SizeArg != nil {
valueSize = int64(argCache.SizeArg.Size)
}
return GetValueInfo(param.Ctx, client, param.Database, key, valueStart, valueSize)
}
func (this_ *V8Service) DelPattern(pattern string, args ...Arg) (count int, err error) {
argCache := getArgCache(args...)
param := formatParam(argCache.Param)
client, err := this_.GetClient(param)
if err != nil {
return
}
count = 0
keysResult, err := Keys(param.Ctx, client, param.Database, pattern, -1)
if err != nil {
return
}
for _, keyInfo := range keysResult.KeyList {
cmd := client.Del(param.Ctx, keyInfo.Key)
_, err = cmd.Result()
if err == nil {
count++
} else {
return
}
}
return
}
func Keys(ctx context.Context, client redis.Cmdable, database int, pattern string, size int) (keysResult *KeysResult, err error) {
keysResult = &KeysResult{}
var list []string
cmdKeys := client.Keys(ctx, pattern)
list, err = cmdKeys.Result()
if err != nil {
return
}
keysResult.Count = len(list)
sort.Slice(list, func(i, j int) bool {
return strings.ToLower(list[i]) < strings.ToLower(list[j]) //升序 即前面的值比后面的小 忽略大小写排序
})
var keys []string
if keysResult.Count <= size || size < 0 {
keys = list
} else {
keys = list[0:size]
}
for _, key := range keys {
info := &KeyInfo{
Key: key,
Database: database,
}
keysResult.KeyList = append(keysResult.KeyList, info)
}
return
}