-
Notifications
You must be signed in to change notification settings - Fork 11
/
redis.go
145 lines (116 loc) · 2.57 KB
/
redis.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
package cache
import (
"context"
"sync"
"time"
"github.com/go-redis/redis/v8"
)
// Redis support two interface: Adapter and Pubsub
type Redis interface {
Adapter
Pubsub
}
// NewRedis generates Adapter with go-redis
func NewRedis(ring *redis.Ring) Redis {
return &rds{
ring: ring,
messChan: make(chan Message),
}
}
type rds struct {
ring *redis.Ring
subscriber *redis.PubSub
subOnce sync.Once
closeOnce sync.Once
messChan chan Message
subMut sync.Mutex
}
func (r *rds) MSet(
ctx context.Context, keyVals map[string][]byte, ttl time.Duration, options ...MSetOptions,
) error {
if len(keyVals) == 0 {
return nil
}
_, err := r.ring.WithContext(ctx).Pipelined(ctx, func(pipe redis.Pipeliner) error {
// set multiple pairs
pairSlice := make([]interface{}, len(keyVals)*2)
i := 0
for key, b := range keyVals {
pairSlice[i] = key
pairSlice[i+1] = b
i += 2
}
pipe.MSet(ctx, pairSlice)
// set expiration for each key
for key := range keyVals {
pipe.PExpire(ctx, key, ttl)
}
return nil
})
return err
}
func (r *rds) MGet(ctx context.Context, keys []string) ([]Value, error) {
vals, err := r.ring.WithContext(ctx).MGet(ctx, keys...).Result()
if err != nil {
return nil, err
}
values := make([]Value, len(vals))
for i, val := range vals {
if val == nil {
values[i] = Value{Valid: false, Bytes: nil}
continue
}
s, ok := val.(string)
if !ok {
values[i] = Value{Valid: false, Bytes: nil}
continue
}
values[i] = Value{Valid: ok, Bytes: []byte(s)}
}
return values, nil
}
func (r *rds) Del(ctx context.Context, keys ...string) error {
_, err := r.ring.WithContext(ctx).Del(ctx, keys...).Result()
return err
}
type rdsMessage struct {
topic string
content string
}
func (m *rdsMessage) Topic() string {
return m.topic
}
func (m *rdsMessage) Content() []byte {
return []byte(m.content)
}
func (r *rds) Pub(ctx context.Context, topic string, message []byte) error {
return r.ring.Publish(ctx, topic, message).Err()
}
func (r *rds) Sub(ctx context.Context, topic ...string) <-chan Message {
r.subOnce.Do(func() {
subscriber := r.ring.Subscribe(ctx, topic...)
r.subMut.Lock()
r.subscriber = subscriber
r.subMut.Unlock()
go func() {
for mess := range subscriber.Channel() {
r.messChan <- &rdsMessage{
topic: mess.Channel,
content: mess.Payload,
}
}
close(r.messChan)
}()
})
return r.messChan
}
func (r *rds) Close() {
r.closeOnce.Do(func() {
r.subMut.Lock()
subscriber := r.subscriber
r.subMut.Unlock()
if subscriber != nil {
subscriber.Close()
}
})
}