/
rediscache.go
50 lines (46 loc) · 1016 Bytes
/
rediscache.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
package main
import (
"github.com/hoisie/redis"
)
/*
直接从redis中获取解析结果(首选)
*/
func resolveFromRedis(domain string) (string, error) {
pool := newRedisPool(redisaddr)
conn := pool.Get()
defer conn.Close()
res, err := redis.String(conn.Do("GET", domain))
if err != nil {
return "", err
}
return res, nil
}
/*
解析到的结果缓存到redis中
*/
func cacheRespToRedis(domain, ipaddr string) (bool, error) {
pool := newRedisPool(address)
conn := pool.Get()
defer conn.Close()
ok, err := redis.String(conn.Do("SET", domain, ipaddr))
if err != nil {
return false, err
}
return true, nil
}
//初始化redis pool
func newRedisPool(address string) *redis.Pool {
RedisPool := &redis.Pool{
// Maximum number of idle connections in the pool
MaxIdle: 10,
//Dial is for creating and configure connection
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", address)
if err != nil {
return nil, err
}
return conn, err
},
}
return RedisPool
}