/
redisadapter.go
119 lines (105 loc) · 2.76 KB
/
redisadapter.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
115
116
117
118
119
package redisadapter
import (
"fmt"
"time"
"github.com/kevinyjn/gocom/caching/cachingenv"
"github.com/kevinyjn/gocom/logger"
"github.com/go-redis/redis"
)
// Constants
const (
RedisPingInterval = 5
)
// RedisCacheSession instance
type RedisCacheSession struct {
Client redis.UniversalClient
Name string
Addr string
ticker *time.Ticker
}
// NewRedisSession new session
func NewRedisSession(name string, conf cachingenv.CacheConnectorConfig) (*RedisCacheSession, error) {
redisAddr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
s := &RedisCacheSession{
Name: name,
Addr: fmt.Sprintf("%s/%d", redisAddr, conf.Index),
}
if conf.ClusterMode {
s.Client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{redisAddr},
Password: conf.Password,
OnConnect: s.OnConnected,
})
} else {
s.Client = redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: conf.Password,
DB: conf.Index,
OnConnect: s.OnConnected,
})
}
_, err := s.Client.Ping().Result()
if err != nil {
logger.Error.Printf("connect redis %s %s failed with error:%v", name, redisAddr, err)
go s.StartKeepalive()
return s, err
}
logger.Info.Printf("initialized redis connection %s %s on db:%d", name, redisAddr, conf.Index)
return s, nil
}
// GetName getter
func (s *RedisCacheSession) GetName() string {
return s.Name
}
// GetConnectionString getter
func (s *RedisCacheSession) GetConnectionString() string {
return s.Addr
}
// Initialized getter
func (s *RedisCacheSession) Initialized() bool {
return s.Client != nil
}
// OnConnected event
func (s *RedisCacheSession) OnConnected(*redis.Conn) error {
logger.Info.Printf("redis %s connection:%s connected.", s.GetName(), s.GetConnectionString())
if s.ticker == nil {
go s.StartKeepalive()
}
return nil
}
// StartKeepalive keepalive
func (s *RedisCacheSession) StartKeepalive() {
if s.ticker != nil {
return
}
s.ticker = time.NewTicker(time.Second * RedisPingInterval)
for {
select {
case <-s.ticker.C:
_, err := s.Client.Ping().Result()
if err != nil {
logger.Error.Printf("ping redis %s %s failed error:%s", s.GetName(), s.GetConnectionString(), err)
} else {
// logger.Trace.Printf("ping redis %s %s %s", s.GetName(), s.GetConnectionString(), txt)
}
}
}
}
// Get value by key
func (s *RedisCacheSession) Get(key string) ([]byte, error) {
val, err := s.Client.Get(key).Bytes()
if nil != err {
logger.Warning.Printf("get %s failed with error:%v", key, err)
return nil, err
}
return val, err
}
// Set value by key
func (s *RedisCacheSession) Set(key string, value []byte, expire time.Duration) bool {
err := s.Client.Set(key, value, expire).Err()
if nil != err {
logger.Error.Printf("set %s failed with error:%v", key, err)
return false
}
return true
}