forked from zorkian/go-datadog-api
/
redis.go
112 lines (101 loc) · 2.04 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package datadog
import (
"io/ioutil"
"log"
"strings"
"time"
"github.com/gomodule/redigo/redis"
"github.com/kiyor/env"
)
var Redis *RedisPool
type RedisPool struct {
Pool redis.Pool
}
var redisHost string
func inDocker() bool {
b, err := ioutil.ReadFile("/proc/1/cgroup")
if err == nil && strings.HasSuffix(strings.Split(string(b), "\n")[0], "/") {
return false
}
return true
}
func init() {
env.StringVar(&redisHost, "REDIS_HOST", "127.0.0.1", redisHost)
host := "127.0.0.1"
if inDocker() {
if redisHost == "127.0.0.1" {
host = "redis"
} else {
host = redisHost
}
}
Redis = &RedisPool{
redis.Pool{
MaxIdle: 6,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", host+":6379")
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
},
}
}
func (r *RedisPool) Reset() {
conn := r.Pool.Get()
defer conn.Close()
conn.Do("FLUSHALL")
}
func (r *RedisPool) Get(key string) ([]byte, bool) {
conn := r.Pool.Get()
defer conn.Close()
res, err := conn.Do("GET", key)
if err != nil {
log.Println("redis", err)
return nil, false
}
if res != nil {
b := res.([]byte)
return b, true
} else {
return nil, false
}
}
func (r *RedisPool) GetWithTTL(key string) ([]byte, int64, bool) {
conn := r.Pool.Get()
defer conn.Close()
res, err := conn.Do("GET", key)
if err != nil {
log.Println("redis", err)
return nil, 0, false
}
if res != nil {
b := res.([]byte)
ttl, _ := conn.Do("TTL", key)
return b, ttl.(int64), true
} else {
return nil, 0, false
}
}
func (r *RedisPool) Set(key string, value []byte) error {
conn := r.Pool.Get()
defer conn.Close()
_, err := conn.Do("SET", key, value)
return err
}
func (r *RedisPool) SetWithTTL(key string, value []byte, second int) error {
conn := r.Pool.Get()
defer conn.Close()
_, err := conn.Do("SET", key, value)
if err != nil {
return err
}
_, err = conn.Do("EXPIRE", key, second)
return err
}