2019-10-03 11:57:49 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-04-08 17:08:41 +03:00
|
|
|
"fmt"
|
|
|
|
|
2020-07-06 15:59:32 +03:00
|
|
|
"github.com/go-redis/redis/v7"
|
2020-05-13 12:59:23 +03:00
|
|
|
log "github.com/micro/go-micro/v2/logger"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-03 11:57:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type rkv struct {
|
2019-12-16 18:41:04 +03:00
|
|
|
options store.Options
|
|
|
|
Client *redis.Client
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 12:59:23 +03:00
|
|
|
func (r *rkv) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.configure()
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-04-08 16:34:27 +03:00
|
|
|
func (r *rkv) Close() error {
|
|
|
|
return r.Client.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
func (r *rkv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
2020-05-13 14:45:16 +03:00
|
|
|
options := store.ReadOptions{}
|
|
|
|
options.Table = r.options.Table
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:07:12 +03:00
|
|
|
var keys []string
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-05-13 14:45:16 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Table, key)
|
2020-06-30 13:07:12 +03:00
|
|
|
// Handle Prefix
|
|
|
|
// TODO suffix
|
|
|
|
if options.Prefix {
|
2020-07-06 15:59:32 +03:00
|
|
|
prefixKey := fmt.Sprintf("%s*", rkey)
|
|
|
|
fkeys, err := r.Client.Keys(prefixKey).Result()
|
2020-06-30 13:07:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO Limit Offset
|
|
|
|
|
|
|
|
keys = append(keys, fkeys...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
keys = []string{rkey}
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2019-10-18 00:00:29 +03:00
|
|
|
|
2020-06-30 13:07:12 +03:00
|
|
|
records := make([]*store.Record, 0, len(keys))
|
2019-10-18 00:00:29 +03:00
|
|
|
|
2020-06-30 13:07:12 +03:00
|
|
|
for _, rkey = range keys {
|
|
|
|
val, err := r.Client.Get(rkey).Bytes()
|
|
|
|
|
|
|
|
if err != nil && err == redis.Nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-06-30 13:07:12 +03:00
|
|
|
if val == nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := r.Client.TTL(rkey).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
|
|
|
Expiry: d,
|
|
|
|
})
|
|
|
|
}
|
2020-02-12 18:53:18 +03:00
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
return records, nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:44:40 +03:00
|
|
|
func (r *rkv) Delete(key string, opts ...store.DeleteOption) error {
|
2020-05-13 14:43:21 +03:00
|
|
|
options := store.DeleteOptions{}
|
|
|
|
options.Table = r.options.Table
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
rkey := fmt.Sprintf("%s%s", options.Table, key)
|
2020-04-08 17:08:41 +03:00
|
|
|
return r.Client.Del(rkey).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:44:40 +03:00
|
|
|
func (r *rkv) Write(record *store.Record, opts ...store.WriteOption) error {
|
2020-05-13 14:43:21 +03:00
|
|
|
options := store.WriteOptions{}
|
|
|
|
options.Table = r.options.Table
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:45:16 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Table, record.Key)
|
2020-04-08 17:08:41 +03:00
|
|
|
return r.Client.Set(rkey, record.Value, record.Expiry).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:44:40 +03:00
|
|
|
func (r *rkv) List(opts ...store.ListOption) ([]string, error) {
|
2020-05-13 14:43:21 +03:00
|
|
|
options := store.ListOptions{}
|
|
|
|
options.Table = r.options.Table
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-10-03 11:57:49 +03:00
|
|
|
keys, err := r.Client.Keys("*").Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-04 13:36:30 +03:00
|
|
|
|
2020-03-13 01:44:40 +03:00
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) Options() store.Options {
|
|
|
|
return r.options
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) String() string {
|
|
|
|
return "redis"
|
|
|
|
}
|
|
|
|
|
2019-12-16 18:41:04 +03:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 12:59:23 +03:00
|
|
|
s := new(rkv)
|
|
|
|
s.options = options
|
|
|
|
|
|
|
|
if err := s.configure(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) configure() error {
|
2020-07-06 15:59:32 +03:00
|
|
|
var redisOptions *redis.Options
|
2020-05-13 12:59:23 +03:00
|
|
|
nodes := r.options.Nodes
|
2019-12-16 18:41:04 +03:00
|
|
|
|
2019-10-03 11:57:49 +03:00
|
|
|
if len(nodes) == 0 {
|
2020-07-06 15:59:32 +03:00
|
|
|
nodes = []string{"redis://127.0.0.1:6379"}
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 15:59:32 +03:00
|
|
|
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)
|
2020-05-13 12:59:23 +03:00
|
|
|
|
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|