-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
44 lines (38 loc) · 925 Bytes
/
redis.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
package main
import (
"fmt"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stevenlee87/go-gin-example/pkg/setting"
)
func main() {
var RedisConn *redis.Pool
RedisConn = &redis.Pool{
MaxIdle: setting.RedisSetting.MaxIdle,
MaxActive: setting.RedisSetting.MaxActive,
IdleTimeout: setting.RedisSetting.IdleTimeout,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", setting.RedisSetting.Host)
if err != nil {
return nil, err
}
if setting.RedisSetting.Password != "" {
if _, err := c.Do("AUTH", setting.RedisSetting.Password); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
conn := RedisConn.Get()
defer conn.Close()
_, err := conn.Do("SET", "key4", "rabbit4")
if err != nil {
fmt.Println("redis set failed:", err)
}
}