Skip to content

Commit 762eba5

Browse files
committed
refactor🎨: 重构 Setup 函数,拆分为多个子函数以提高可读性和维护性
1 parent e82128f commit 762eba5

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

Diff for: common/storage/initialize.go

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* @Author: lwnmengjing
3-
* @Date: 2021/6/10 3:39 下午
4-
* @Last Modified by: lwnmengjing
5-
* @Last Modified time: 2021/6/10 3:39 下午
2+
* @Author: zhangwenjian
3+
* @Date: 2025/04/13 22:03
4+
* @Last Modified by: zhangwenjian
5+
* @Last Modified time: 2025/04/13 22:03
66
*/
77

88
package storage
@@ -17,27 +17,34 @@ import (
1717

1818
// Setup 配置storage组件
1919
func Setup() {
20-
//4. 设置缓存
20+
setupCache()
21+
setupCaptcha()
22+
setupQueue()
23+
}
24+
25+
func setupCache() {
2126
cacheAdapter, err := config.CacheConfig.Setup()
2227
if err != nil {
2328
log.Fatalf("cache setup error, %s\n", err.Error())
2429
}
2530
sdk.Runtime.SetCacheAdapter(cacheAdapter)
26-
//5. 设置验证码store
27-
captcha.SetStore(captcha.NewCacheStore(cacheAdapter, 600))
31+
}
32+
33+
func setupCaptcha() {
34+
captcha.SetStore(captcha.NewCacheStore(sdk.Runtime.GetCacheAdapter(), 600))
35+
}
2836

29-
//6. 设置队列
30-
if !config.QueueConfig.Empty() {
31-
if q := sdk.Runtime.GetQueueAdapter(); q != nil {
32-
q.Shutdown()
33-
}
34-
queueAdapter, err := config.QueueConfig.Setup()
35-
if err != nil {
36-
log.Fatalf("queue setup error, %s\n", err.Error())
37-
}
38-
sdk.Runtime.SetQueueAdapter(queueAdapter)
39-
defer func() {
40-
go queueAdapter.Run()
41-
}()
37+
func setupQueue() {
38+
if config.QueueConfig.Empty() {
39+
return
40+
}
41+
if q := sdk.Runtime.GetQueueAdapter(); q != nil {
42+
q.Shutdown()
43+
}
44+
queueAdapter, err := config.QueueConfig.Setup()
45+
if err != nil {
46+
log.Fatalf("queue setup error, %s\n", err.Error())
4247
}
48+
sdk.Runtime.SetQueueAdapter(queueAdapter)
49+
go queueAdapter.Run()
4350
}

0 commit comments

Comments
 (0)