-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.go
38 lines (30 loc) · 811 Bytes
/
init.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
package rdis
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/mangohow/cloud-ide/cmd/webserver/internal/conf"
)
var client *redis.Client
func RedisInstance() *redis.Client {
return client
}
func InitRedis() error {
client = redis.NewClient(&redis.Options{
Addr: conf.RedisConfig.Addr,
Password: conf.RedisConfig.Password,
DB: int(conf.RedisConfig.DB),
PoolSize: int(conf.RedisConfig.PoolSize), // 连接池最大socket连接数
MinIdleConns: int(conf.RedisConfig.MinIdleConns), // 最少连接维持数
})
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_, err := client.Ping(timeoutCtx).Result()
if err != nil {
return err
}
return nil
}
func CloseRedisConn() {
client.Close()
}