-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
114 lines (100 loc) · 2.66 KB
/
conn.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 conn
import (
"fmt"
// "github.com/apache/pulsar/pulsar-client-go/pulsar"
"github.com/go-redis/redis"
"github.com/tendermint/tendermint/libs/log"
)
type Client struct {
redisCli *redis.Client
log log.Logger
}
func NewClient(redisURL, redisPassword string, db int, log log.Logger) (client *Client, err error) {
rc := redis.NewClient(&redis.Options{
Addr: redisURL,
Password: redisPassword, // no password set
DB: db, // use default DB
})
client = &Client{redisCli: rc, log: log}
return client, err
}
// PrivatePub push data to private topic
func (c Client) PrivatePub(key, val string) (err error) {
logger := c.log.With("module", "redis-Client")
err = c.redisCli.Publish(key, val).Err()
if err != nil {
if err == redis.Nil {
logger.Debug("Redis Publish error")
} else {
return err
}
}
return
}
// PublicPub push data to public topic
func (c Client) PublicPub(key, val string) (err error) {
logger := c.log.With("module", "redis-Client")
err = c.redisCli.Publish(key, val).Err()
if err != nil {
if err == redis.Nil {
logger.Debug("Redis Publish error")
} else {
return err
}
}
return
}
// DepthPub push data to depth topic
func (c Client) DepthPub(key, val string) (err error) {
logger := c.log.With("module", "redis-Client")
err = c.redisCli.Publish(key, val).Err()
if err != nil {
if err == redis.Nil {
logger.Debug("Redis Publish error")
} else {
return err
}
}
return
}
// Set push data to redis, only used by public channel
func (c Client) Set(key, val string) (err error) {
logger := c.log.With("module", "redis-Client")
err = c.redisCli.Set(key, val, 0).Err()
if err != nil {
if err == redis.Nil {
logger.Debug("Redis set", "key not found, set a new k-v")
} else {
return err
}
}
return nil
}
// Get get data from redis
func (c Client) Get(key string) (val string, err error) {
val, err = c.redisCli.Get(key).Result()
if err != nil {
return "", fmt.Errorf("failed to get val for key=%s, err=%s", key, err.Error())
}
return val, nil
}
// MGet get data from redis, many keys once
func (c Client) MGet(keys []string) (vals []interface{}, err error) {
vals, err = c.redisCli.MGet(keys...).Result()
if err != nil {
return nil, fmt.Errorf("failed to get vals for keys=%v, err=%s", keys, err.Error())
}
return vals, nil
}
// Close close redis connect to server
func (c Client) Close() error {
return c.redisCli.Close()
}
// HGetAll
func (c Client) HGetAll(key string) (vals map[string]string, err error) {
vals, err = c.redisCli.HGetAll(key).Result()
if err != nil {
return nil, fmt.Errorf("failed to HGETALL vals for key=%v, err=%s", key, err.Error())
}
return vals, nil
}