Skip to content

Commit ddd7910

Browse files
committed
refactor:sync.Map replace map
1 parent 28ee991 commit ddd7910

File tree

9 files changed

+43
-42
lines changed

9 files changed

+43
-42
lines changed

controllers/hash_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (con hashController) Show(c *gin.Context) {
3131
return
3232
}
3333

34-
client := global.GlobalClients[req.Sk]
34+
client := global.GetClient(req.Sk)
3535

3636
val, _ := c.Get("username")
3737
ctx := context.WithValue(context.Background(), "username", val)
@@ -93,7 +93,7 @@ func (con hashController) Del(c *gin.Context) {
9393
return
9494
}
9595

96-
client := global.GlobalClients[req.Sk]
96+
client := global.GetClient(req.Sk)
9797

9898
val, _ := c.Get("username")
9999
ctx := context.WithValue(context.Background(), "username", val)
@@ -125,7 +125,7 @@ func (con hashController) AddItem(c *gin.Context) {
125125
return
126126
}
127127

128-
client := global.GlobalClients[req.Sk]
128+
client := global.GetClient(req.Sk)
129129

130130
val, _ := c.Get("username")
131131
ctx := context.WithValue(context.Background(), "username", val)

controllers/index_controller.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (con indexController) Open(c *gin.Context) {
4949
con.Error(c, err.Error())
5050
return
5151
}
52-
global.GlobalClients[keys[1]] = client
52+
global.SetClient(keys[1], client)
5353
global.GlobalConf.RedisServices.Store(keys[1], redisServer)
5454

5555
val, _ := c.Get("username")
@@ -124,7 +124,7 @@ func (con indexController) GetKeys(c *gin.Context) {
124124

125125
dbInfo := strings.Split(req.Index, "-")
126126
index, _ := strconv.Atoi(dbInfo[0])
127-
client := global.GlobalClients[dbInfo[1]]
127+
client := global.GetClient(dbInfo[1])
128128

129129
val, _ := c.Get("username")
130130
ctx := context.WithValue(context.Background(), "username", val)
@@ -188,7 +188,7 @@ func (con indexController) GetKeyType(c *gin.Context) {
188188
return
189189
}
190190

191-
client := global.GlobalClients[req.Sk]
191+
client := global.GetClient(req.Sk)
192192

193193
val, _ := c.Get("username")
194194
ctx := context.WithValue(context.Background(), "username", val)
@@ -221,7 +221,7 @@ func (con indexController) DelKey(c *gin.Context) {
221221
return
222222
}
223223

224-
client := global.GlobalClients[req.Sk]
224+
client := global.GetClient(req.Sk)
225225

226226
val, _ := c.Get("username")
227227
ctx := context.WithValue(context.Background(), "username", val)
@@ -263,7 +263,7 @@ func (con indexController) TtlKey(c *gin.Context) {
263263
return
264264
}
265265

266-
client := global.GlobalClients[req.Sk]
266+
client := global.GetClient(req.Sk)
267267

268268
val, _ := c.Get("username")
269269
ctx := context.WithValue(context.Background(), "username", val)
@@ -293,7 +293,6 @@ func (con indexController) TtlKey(c *gin.Context) {
293293
func (con indexController) SerInfo(c *gin.Context) {
294294
var req model.InfoReq
295295
var client *redis.Client
296-
var ok bool
297296

298297
err := con.FormBind(c, &req)
299298
if err != nil {
@@ -304,14 +303,14 @@ func (con indexController) SerInfo(c *gin.Context) {
304303
redisServer, _ := global.GlobalConf.RedisServices.Load(req.Key)
305304
redisServers := redisServer.(global.RedisService)
306305

307-
client, ok = global.GlobalClients[req.Key]
308-
if !ok {
306+
client = global.GetClient(req.Key)
307+
if client == nil {
309308
client, err = service.NewRedisClient(redisServers)
310309
if err != nil {
311310
con.Error(c, err.Error())
312311
return
313312
}
314-
global.GlobalClients[req.Key] = client
313+
global.SetClient(req.Key, client)
315314
}
316315

317316
val, _ := c.Get("username")
@@ -341,7 +340,7 @@ func (con indexController) LuaRun(c *gin.Context) {
341340
return
342341
}
343342

344-
client := global.GlobalClients[req.Sk]
343+
client := global.GetClient(req.Sk)
345344

346345
val, _ := c.Get("username")
347346
ctx := context.WithValue(context.Background(), "username", val)

controllers/list_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (con listController) Show(c *gin.Context) {
3131
return
3232
}
3333

34-
client := global.GlobalClients[req.Sk]
34+
client := global.GetClient(req.Sk)
3535

3636
val, _ := c.Get("username")
3737
ctx := context.WithValue(context.Background(), "username", val)
@@ -92,7 +92,7 @@ func (con listController) Del(c *gin.Context) {
9292
return
9393
}
9494

95-
client := global.GlobalClients[req.Sk]
95+
client := global.GetClient(req.Sk)
9696

9797
val, _ := c.Get("username")
9898
ctx := context.WithValue(context.Background(), "username", val)
@@ -124,7 +124,7 @@ func (con listController) AddItem(c *gin.Context) {
124124
return
125125
}
126126

127-
client := global.GlobalClients[req.Sk]
127+
client := global.GetClient(req.Sk)
128128

129129
val, _ := c.Get("username")
130130
ctx := context.WithValue(context.Background(), "username", val)

controllers/set_controller.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (con setController) Show(c *gin.Context) {
3030
return
3131
}
3232

33-
client := global.GlobalClients[req.Sk]
33+
client := global.GetClient(req.Sk)
3434

3535
val, _ := c.Get("username")
3636
ctx := context.WithValue(context.Background(), "username", val)
@@ -86,8 +86,7 @@ func (con setController) Del(c *gin.Context) {
8686
return
8787
}
8888

89-
client := global.GlobalClients[req.Sk]
90-
89+
client := global.GetClient(req.Sk)
9190
val, _ := c.Get("username")
9291
ctx := context.WithValue(context.Background(), "username", val)
9392

@@ -118,8 +117,7 @@ func (con setController) AddItem(c *gin.Context) {
118117
return
119118
}
120119

121-
client := global.GlobalClients[req.Sk]
122-
120+
client := global.GetClient(req.Sk)
123121
val, _ := c.Get("username")
124122
ctx := context.WithValue(context.Background(), "username", val)
125123

controllers/stream_controller.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func (con streamController) Show(c *gin.Context) {
3737
return
3838
}
3939

40-
client := global.GlobalClients[req.Sk]
41-
40+
client := global.GetClient(req.Sk)
4241
val, _ := c.Get("username")
4342
ctx := context.WithValue(context.Background(), "username", val)
4443

@@ -125,8 +124,7 @@ func (con streamController) Del(c *gin.Context) {
125124
return
126125
}
127126

128-
client := global.GlobalClients[req.Sk]
129-
127+
client := global.GetClient(req.Sk)
130128
val, _ := c.Get("username")
131129
ctx := context.WithValue(context.Background(), "username", val)
132130

@@ -157,8 +155,7 @@ func (con streamController) AddItem(c *gin.Context) {
157155
return
158156
}
159157

160-
client := global.GlobalClients[req.Sk]
161-
158+
client := global.GetClient(req.Sk)
162159
val, _ := c.Get("username")
163160
ctx := context.WithValue(context.Background(), "username", val)
164161

controllers/string_controller.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ func (con stringController) Show(c *gin.Context) {
2626
return
2727
}
2828

29-
client := global.GlobalClients[req.Sk]
30-
29+
client := global.GetClient(req.Sk)
3130
val, _ := c.Get("username")
3231
ctx := context.WithValue(context.Background(), "username", val)
3332

@@ -65,8 +64,7 @@ func (con stringController) Add(c *gin.Context) {
6564
return
6665
}
6766

68-
client := global.GlobalClients[req.Sk]
69-
67+
client := global.GetClient(req.Sk)
7068
val, _ := c.Get("username")
7169
ctx := context.WithValue(context.Background(), "username", val)
7270

controllers/ws_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func (con wsController) Ws(c *gin.Context) {
116116

117117
ctx := context.WithValue(context.Background(), "username", username)
118118
var client *redis.Client
119-
client = global.GlobalClients[cmd.Sk]
119+
120+
client = global.GetClient(cmd.Sk)
120121
if client == nil {
121122
redisServer, _ := global.GlobalConf.RedisServices.Load(cmd.Sk)
122123
client, err = service.NewRedisClient(redisServer.(global.RedisService))
@@ -128,7 +129,8 @@ func (con wsController) Ws(c *gin.Context) {
128129
}
129130
goto LOOP
130131
}
131-
global.GlobalClients[cmd.Sk] = client
132+
133+
global.SetClient(cmd.Sk, client)
132134
global.GlobalConf.RedisServices.Store(cmd.Sk, redisServer)
133135
}
134136
err = client.Do(ctx, "select", cmd.Db).Err()

controllers/zset_controller.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ func (con zsetController) Show(c *gin.Context) {
3434
return
3535
}
3636

37-
client := global.GlobalClients[req.Sk]
38-
37+
client := global.GetClient(req.Sk)
3938
val, _ := c.Get("username")
4039
ctx := context.WithValue(context.Background(), "username", val)
4140

@@ -96,8 +95,7 @@ func (con zsetController) Del(c *gin.Context) {
9695
return
9796
}
9897

99-
client := global.GlobalClients[req.Sk]
100-
98+
client := global.GetClient(req.Sk)
10199
val, _ := c.Get("username")
102100
ctx := context.WithValue(context.Background(), "username", val)
103101

@@ -128,8 +126,7 @@ func (con zsetController) AddItem(c *gin.Context) {
128126
return
129127
}
130128

131-
client := global.GlobalClients[req.Sk]
132-
129+
client := global.GetClient(req.Sk)
133130
val, _ := c.Get("username")
134131
ctx := context.WithValue(context.Background(), "username", val)
135132

global/default.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ import (
1818
"github.com/go-redis/redis"
1919
)
2020

21-
var GlobalClients map[string]*redis.Client
21+
var globalClients sync.Map
22+
23+
func SetClient(key string, client *redis.Client) {
24+
globalClients.Store(key, client)
25+
}
26+
27+
func GetClient(key string) *redis.Client {
28+
client, ok := globalClients.Load(key)
29+
if !ok {
30+
return nil
31+
}
32+
return client.(*redis.Client)
33+
}
2234

2335
type GlobalConfig struct {
2436
Accounts map[string]string
@@ -44,8 +56,6 @@ var GlobalConf = GlobalConfig{
4456

4557
func init() {
4658

47-
GlobalClients = make(map[string]*redis.Client)
48-
4959
data, err := common.ReadData()
5060
if err != nil && err != io.EOF {
5161
fmt.Println(err)

0 commit comments

Comments
 (0)