redis.go
926 Bytes
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
package zredis
import (
"github.com/gomodule/redigo/redis"
"time"
)
var (
_addr string
_pass string
_db int
)
var pool *redis.Pool
func Init(addr, pass string, db int) {
_addr, _pass, _db = addr, pass, db
pool = &redis.Pool{
MaxActive: 10240,
MaxIdle: 10240, // Maximum number of idle connections in the pool.
IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration
Dial: dial,
Wait: true,
}
}
func dial() (redis.Conn, error) {
conn, err := redis.Dial("tcp", _addr,
redis.DialPassword(_pass),
redis.DialDatabase(_db),
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(5*time.Second),
redis.DialWriteTimeout(5*time.Second),
)
return conn, err
}
func GetConn() redis.Conn {
if pool == nil {
return nil
}
return pool.Get()
}
func AutoClose(conn redis.Conn) {
err := conn.Close()
if err != nil {
}
}