-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisPool.go
43 lines (38 loc) · 855 Bytes
/
RedisPool.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
package DB
import (
"ServerIMR/config"
"time"
"github.com/gomodule/redigo/redis"
)
var Redis_pool *redis.Pool
func InitRedis() {
Redis_pool = NewRedisPool(config.Config.Redis_address, config.Config.Redis_password,
config.Config.Redis_db)
}
func NewRedisPool(server, password string, db int) *redis.Pool {
return &redis.Pool{
MaxIdle: 100,
MaxActive: 500,
IdleTimeout: 480 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.DialTimeout("tcp", server,
time.Duration(2)*time.Second, 0, 0)
if err != nil {
return nil, err
}
if len(password) > 0 {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
if db > 0 && db < 16 {
if _, err := c.Do("SELECT", db); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
}
}