/
redis.go
174 lines (134 loc) · 3.11 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
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
package redis
import (
"context"
"fmt"
log "github.com/asim/go-micro/v3/logger"
"github.com/asim/go-micro/v3/store"
"github.com/go-redis/redis/v8"
)
type rkv struct {
ctx context.Context
options store.Options
Client *redis.Client
}
func (r *rkv) Init(opts ...store.Option) error {
for _, o := range opts {
o(&r.options)
}
return r.configure()
}
func (r *rkv) Close() error {
return r.Client.Close()
}
func (r *rkv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
options := store.ReadOptions{}
options.Table = r.options.Table
for _, o := range opts {
o(&options)
}
var keys []string
rkey := fmt.Sprintf("%s%s", options.Table, key)
// Handle Prefix
// TODO suffix
if options.Prefix {
prefixKey := fmt.Sprintf("%s*", rkey)
fkeys, err := r.Client.Keys(r.ctx, prefixKey).Result()
if err != nil {
return nil, err
}
// TODO Limit Offset
keys = append(keys, fkeys...)
} else {
keys = []string{rkey}
}
records := make([]*store.Record, 0, len(keys))
for _, rkey = range keys {
val, err := r.Client.Get(r.ctx, rkey).Bytes()
if err != nil && err == redis.Nil {
return nil, store.ErrNotFound
} else if err != nil {
return nil, err
}
if val == nil {
return nil, store.ErrNotFound
}
d, err := r.Client.TTL(r.ctx, rkey).Result()
if err != nil {
return nil, err
}
records = append(records, &store.Record{
Key: key,
Value: val,
Expiry: d,
})
}
return records, nil
}
func (r *rkv) Delete(key string, opts ...store.DeleteOption) error {
options := store.DeleteOptions{}
options.Table = r.options.Table
for _, o := range opts {
o(&options)
}
rkey := fmt.Sprintf("%s%s", options.Table, key)
return r.Client.Del(r.ctx, rkey).Err()
}
func (r *rkv) Write(record *store.Record, opts ...store.WriteOption) error {
options := store.WriteOptions{}
options.Table = r.options.Table
for _, o := range opts {
o(&options)
}
rkey := fmt.Sprintf("%s%s", options.Table, record.Key)
return r.Client.Set(r.ctx, rkey, record.Value, record.Expiry).Err()
}
func (r *rkv) List(opts ...store.ListOption) ([]string, error) {
options := store.ListOptions{}
options.Table = r.options.Table
for _, o := range opts {
o(&options)
}
keys, err := r.Client.Keys(r.ctx, "*").Result()
if err != nil {
return nil, err
}
return keys, nil
}
func (r *rkv) Options() store.Options {
return r.options
}
func (r *rkv) String() string {
return "redis"
}
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}
s := &rkv{
ctx: context.Background(),
options: options,
}
if err := s.configure(); err != nil {
log.Fatal(err)
}
return s
}
func (r *rkv) configure() error {
var redisOptions *redis.Options
nodes := r.options.Nodes
if len(nodes) == 0 {
nodes = []string{"redis://127.0.0.1:6379"}
}
redisOptions, err := redis.ParseURL(nodes[0])
if err != nil {
//Backwards compatibility
redisOptions = &redis.Options{
Addr: nodes[0],
Password: "", // no password set
DB: 0, // use default DB
}
}
r.Client = redis.NewClient(redisOptions)
return nil
}