-
Notifications
You must be signed in to change notification settings - Fork 21
/
redis.go
62 lines (48 loc) · 1.05 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"os"
"github.com/garyburd/redigo/redis"
)
var (
address string
port string
password string
proto string
)
func Config() {
proto = os.Getenv("REDIS_PORT_6379_TCP_PROTO")
address = os.Getenv("REDIS_PORT_6379_TCP_ADDR")
port = os.Getenv("REDIS_PORT_6379_TCP_PORT")
password = os.Getenv("REDIS_PASSWORD")
}
var conn redis.Conn
func ConnectRedis() (err error) {
conn, err = redis.Dial(proto, address+":"+port)
if err != nil {
return err
}
if password != "" {
if _, err = conn.Do("AUTH", password); err != nil {
conn.Close()
return err
}
}
return nil
}
func MustConnectRedis() {
if err := ConnectRedis(); err != nil {
panic(err)
}
}
func GetNum() (request int, err error) {
request, err = redis.Int(conn.Do("GET", "request"))
return request, err
}
func IncNum() error {
_, err := conn.Do("INCR", "request")
return err
}
func Drop() error {
_, err := conn.Do("DEL", "request")
return err
}