-
Notifications
You must be signed in to change notification settings - Fork 74
/
service.go
64 lines (52 loc) · 1.63 KB
/
service.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
package redis
import (
"github.com/go-redis/redis/v8"
"github.com/gohade/hade/framework"
"github.com/gohade/hade/framework/contract"
"sync"
)
// HadeRedis 代表hade框架的redis实现
type HadeRedis struct {
container framework.Container // 服务容器
clients map[string]*redis.Client // key为uniqKey, value为redis.Client (连接池)
lock *sync.RWMutex
}
// NewHadeRedis 代表实例化Client
func NewHadeRedis(params ...interface{}) (interface{}, error) {
container := params[0].(framework.Container)
clients := make(map[string]*redis.Client)
lock := &sync.RWMutex{}
return &HadeRedis{
container: container,
clients: clients,
lock: lock,
}, nil
}
// GetClient 获取Client实例
func (app *HadeRedis) GetClient(option ...contract.RedisOption) (*redis.Client, error) {
// 读取默认配置
config := GetBaseConfig(app.container)
// option对opt进行修改
for _, opt := range option {
if err := opt(app.container, config); err != nil {
return nil, err
}
}
// 如果最终的config没有设置dsn,就生成dsn
key := config.UniqKey()
// 判断是否已经实例化了redis.Client
app.lock.RLock()
if db, ok := app.clients[key]; ok {
app.lock.RUnlock()
return db, nil
}
app.lock.RUnlock()
// 没有实例化gorm.DB,那么就要进行实例化操作
app.lock.Lock()
defer app.lock.Unlock()
// 实例化gorm.DB
client := redis.NewClient(config.Options)
// 挂载到map中,结束配置
app.clients[key] = client
return client, nil
}