-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
192 lines (158 loc) · 4.24 KB
/
topic.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package topic
import (
"fmt"
"log"
"strconv"
"time"
"github.com/pkg/errors"
redis "gopkg.in/redis.v5"
)
const (
defaultTopicKeyFormat = "topic:%s"
defaultLastPushedAtKeyFormat = "users:%d:push"
defaultSessionKeyFormat = "sess:%s"
defaultSessionTTL = time.Minute * 5
)
var (
sharedClient *redis.Client
)
// Type represents topic type.
type Type interface {
String() string
}
// storeImpl implements Store interface.
type storeImpl struct {
topicKeyFormat string
lastPushedAtKeyFormat string
sessionKeyFormat string
sessionTTL time.Duration
}
// Config represents redis client options.
type Config struct {
Redis *redis.Options
TopicKeyFormat string
LastPushedAtKeyFormat string
SessionKeyFormat string
SessionTTL time.Duration
}
// NewStore creates a new store
func NewStore(config *Config) Store {
if config.Redis == nil {
config.Redis = &redis.Options{
Addr: "localhost:6379",
}
}
connect(config.Redis)
store := &storeImpl{
topicKeyFormat: defaultTopicKeyFormat,
lastPushedAtKeyFormat: defaultLastPushedAtKeyFormat,
sessionKeyFormat: defaultSessionKeyFormat,
sessionTTL: defaultSessionTTL,
}
if config.TopicKeyFormat != "" {
store.topicKeyFormat = config.TopicKeyFormat
}
if config.LastPushedAtKeyFormat != "" {
store.lastPushedAtKeyFormat = config.LastPushedAtKeyFormat
}
if config.SessionKeyFormat != "" {
store.sessionKeyFormat = config.SessionKeyFormat
}
if config.SessionTTL > 0 {
store.sessionTTL = config.SessionTTL
}
return store
}
func connect(options *redis.Options) {
if sharedClient == nil {
// create redis client
client := redis.NewClient(options)
// check connection
if _, err := client.Ping().Result(); err != nil {
log.Print("failed to connect to redis client", err)
}
sharedClient = client
}
}
func (s *storeImpl) Subscribe(topic Type, userIDs ...uint64) error {
if len(userIDs) == 0 {
return nil
}
key := fmt.Sprintf(s.topicKeyFormat, topic)
uids := make([]interface{}, len(userIDs))
for i, userID := range userIDs {
uids[i] = userID
}
err := sharedClient.SAdd(key, uids...).Err()
return errors.WithStack(err)
}
func (s *storeImpl) Unsubscribe(topic Type, userIDs ...uint64) error {
if len(userIDs) == 0 {
return nil
}
key := fmt.Sprintf(s.topicKeyFormat, topic)
uids := make([]interface{}, len(userIDs))
for i, userID := range userIDs {
uids[i] = userID
}
err := sharedClient.SRem(key, uids...).Err()
return errors.WithStack(err)
}
func (s *storeImpl) ExpireAt(topic Type, expireAt time.Time) error {
key := fmt.Sprintf(s.topicKeyFormat, topic)
err := sharedClient.ExpireAt(key, expireAt).Err()
return errors.WithStack(err)
}
// SaveLastPushedAt saves last pushed time to hash.
func (s *storeImpl) SaveLastPushedAt(userIDs []uint64, lastPushDate time.Time) error {
if len(userIDs) == 0 {
return nil
}
cmds, err := sharedClient.TxPipelined(func(pipe *redis.Pipeline) error {
for _, id := range userIDs {
pipe.HSet(fmt.Sprintf(s.lastPushedAtKeyFormat, id), "like", lastPushDate.Unix())
}
return nil
})
if err != nil {
err = txError(cmds)
}
return errors.WithStack(err)
}
// GetLastPushedAt get last pushed time to hash.
func (s *storeImpl) GetLastPushedAt(userIDs []uint64) (res []time.Time, err error) {
if len(userIDs) == 0 {
return nil, nil
}
res = make([]time.Time, 0, len(userIDs))
outputs := make([]*redis.StringCmd, 0, len(userIDs))
cmds, err := sharedClient.TxPipelined(func(pipe *redis.Pipeline) error {
for _, id := range userIDs {
outputs = append(outputs, pipe.HGet(fmt.Sprintf(s.lastPushedAtKeyFormat, id), "like"))
}
return nil
})
if err != nil {
for _, v := range cmds {
if v.Err() != nil && v.Err() != redis.Nil {
return nil, txError(cmds)
}
}
}
for _, cmd := range outputs {
unixTimeStr, err := cmd.Result()
if unixTimeStr == "" || err != nil {
res = append(res, time.Time{})
} else {
unixTime, err := strconv.ParseInt(unixTimeStr, 10, 64)
if err != nil {
res = append(res, time.Time{})
}
lastPushedAt := time.Unix(unixTime, 0)
res = append(res, lastPushedAt)
}
}
return res, nil
}
// Bulk represents scan callback method.
type Bulk func(userIDs []uint64) (int64, error)