/
rdshelper.go
114 lines (102 loc) · 2.4 KB
/
rdshelper.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
113
114
package datasource
import (
"fmt"
"log"
"sync"
"time"
"github.com/gomodule/redigo/redis"
"lottery/conf"
)
// 互斥锁
var rdsLock sync.Mutex
var cacheInstance *RedisConn
// 封装成一个redis资源池
type RedisConn struct {
pool *redis.Pool
// 设置是否打印redis日志
showDebug bool
}
// 对外只有一个命令,封装了一个redis的命令
func (rds *RedisConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
// 从Redis连接池获取1个连接
conn := rds.pool.Get()
// 使用完该连接,需要把连接放回连接池
defer conn.Close()
// debug功能
t1 := time.Now().UnixNano()
reply, err = conn.Do(commandName, args...)
if err != nil {
// 读取错误信息
e := conn.Err()
if e != nil {
log.Println("rdshelper Do", err, e)
}
}
t2 := time.Now().UnixNano()
if rds.showDebug {
// 微秒 us = 纳秒 / 1000
fmt.Printf("[redis] [info] [%dus]cmd=%s, err=%s, args=%v, reply=%s\n",
(t2-t1)/1000, commandName, err, args, reply)
}
return reply, err
}
// 设置是否打印操作日志
func (rds *RedisConn) ShowDebug(b bool) {
// 私有字段showDebug的访问要提供公开方法
rds.showDebug = b
}
// 单例模式 - 得到唯一的redis缓存实例
func InstanceCache() *RedisConn {
if cacheInstance != nil {
return cacheInstance
}
rdsLock.Lock()
defer rdsLock.Unlock()
// 要有二次判断
if cacheInstance != nil {
return cacheInstance
}
return NewCache()
}
// RedisConn 实例化
func NewCache() *RedisConn {
pool := redis.Pool{
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp",
fmt.Sprintf("%s:%d", conf.RdsCache.Host, conf.RdsCache.Port))
if err != nil {
log.Fatal("rdshelper.NewCache Dial error ", err)
return nil, err
}
// 创建Redis连接池成功 返回创建的连接
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := c.Do("PING")
return err
},
// 最大连接数
MaxIdle: 10000,
// 最大活跃数
MaxActive: 10000,
// 超时时间
IdleTimeout: 0,
// 等待
Wait: false,
// 最大连接活跃时间 一直活跃
MaxConnLifetime: 0,
}
// 构建 RedisConn 对象
instance := &RedisConn{
pool: &pool,
}
// 单例赋值
cacheInstance = instance
// 打印redis执行命令日志
cacheInstance.ShowDebug(true)
//cacheInstance.ShowDebug(false)
return cacheInstance
}