2021-10-27 23:29:16 +03:00
|
|
|
package redis // import "go.unistack.org/micro-store-redis/v3"
|
2019-10-03 11:57:49 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-16 03:00:25 +03:00
|
|
|
"context"
|
2020-04-08 17:08:41 +03:00
|
|
|
"fmt"
|
2021-01-16 03:00:25 +03:00
|
|
|
"time"
|
2020-04-08 17:08:41 +03:00
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
"github.com/go-redis/redis/v8"
|
2021-10-27 23:29:16 +03:00
|
|
|
"go.unistack.org/micro/v3/store"
|
2019-10-03 11:57:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type rkv struct {
|
2021-01-16 03:00:25 +03:00
|
|
|
opts store.Options
|
|
|
|
cli redisClient
|
|
|
|
}
|
|
|
|
|
2022-07-08 23:17:14 +03:00
|
|
|
// TODO: add ability to set some redis options https://pkg.go.dev/github.com/go-redis/redis/v8#Options
|
2021-01-16 03:00:25 +03:00
|
|
|
type redisClient interface {
|
|
|
|
Get(ctx context.Context, key string) *redis.StringCmd
|
|
|
|
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
|
|
|
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
|
|
|
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
|
|
|
|
Exists(ctx context.Context, keys ...string) *redis.IntCmd
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) Connect(ctx context.Context) error {
|
|
|
|
return nil
|
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 {
|
2021-01-16 03:00:25 +03:00
|
|
|
o(&r.opts)
|
2020-05-13 12:59:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.configure()
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
func (r *rkv) Disconnect(ctx context.Context) error {
|
|
|
|
return r.cli.Close()
|
2020-04-08 16:34:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:47:57 +03:00
|
|
|
func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
2021-11-24 15:23:47 +03:00
|
|
|
options := store.NewExistsOptions(opts...)
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
|
|
|
}
|
2022-07-08 23:17:14 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-11-24 15:23:47 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
2021-05-20 22:49:30 +03:00
|
|
|
st, err := r.cli.Exists(ctx, rkey).Result()
|
2021-01-16 03:00:25 +03:00
|
|
|
if err != nil {
|
2021-05-20 22:49:30 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if st == 0 {
|
2021-01-16 03:00:25 +03:00
|
|
|
return store.ErrNotFound
|
2020-05-13 14:45:16 +03:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 14:45:16 +03:00
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
|
|
|
options := store.NewReadOptions(opts...)
|
2021-11-24 15:23:47 +03:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
2022-07-08 23:17:14 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-11-24 15:23:47 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
2021-01-16 03:00:25 +03:00
|
|
|
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
|
|
|
if err != nil && err == redis.Nil {
|
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
if buf == nil {
|
|
|
|
return store.ErrNotFound
|
2020-06-30 13:07:12 +03:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
/*
|
|
|
|
d, err := r.Client.TTL(rkey).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
|
|
|
Expiry: d,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return r.opts.Codec.Unmarshal(buf, val)
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
|
|
|
options := store.NewDeleteOptions(opts...)
|
2021-11-24 15:23:47 +03:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2022-07-08 23:17:14 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-11-24 15:23:47 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.cli.Del(ctx, rkey).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
|
|
|
options := store.NewWriteOptions(opts...)
|
2021-11-24 15:23:47 +03:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
2020-05-13 14:43:21 +03:00
|
|
|
|
2021-11-24 15:23:47 +03:00
|
|
|
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
2021-01-16 03:00:25 +03:00
|
|
|
buf, err := r.opts.Codec.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2022-07-08 23:17:14 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
|
|
|
options := store.NewListOptions(opts...)
|
2021-11-24 15:23:47 +03:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2022-07-08 23:17:14 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
// TODO: add support for prefix/suffix/limit
|
|
|
|
keys, err := r.cli.Keys(ctx, "*").Result()
|
2019-10-03 11:57:49 +03:00
|
|
|
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 {
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.opts
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:47:57 +03:00
|
|
|
func (r *rkv) Name() string {
|
|
|
|
return r.opts.Name
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-16 03:00:25 +03:00
|
|
|
return &rkv{opts: store.NewOptions(opts...)}
|
2020-05-13 12:59:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) configure() error {
|
2020-07-06 15:59:32 +03:00
|
|
|
var redisOptions *redis.Options
|
2021-01-16 03:00:25 +03:00
|
|
|
var redisClusterOptions *redis.ClusterOptions
|
|
|
|
var err error
|
|
|
|
|
2021-11-24 15:23:47 +03:00
|
|
|
nodes := r.opts.Addrs
|
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
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
if r.opts.Context != nil {
|
|
|
|
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
|
|
|
redisOptions = c
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok {
|
|
|
|
redisClusterOptions = c
|
2020-07-06 15:59:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
if redisOptions != nil && redisClusterOptions != nil {
|
|
|
|
return fmt.Errorf("must specify only one option Config or ClusterConfig")
|
|
|
|
}
|
|
|
|
|
|
|
|
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 {
|
|
|
|
redisOptions, err = redis.ParseURL(nodes[0])
|
|
|
|
if err != nil {
|
2021-11-24 15:23:47 +03:00
|
|
|
// Backwards compatibility
|
2021-01-16 03:00:25 +03:00
|
|
|
redisOptions = &redis.Options{
|
|
|
|
Addr: nodes[0],
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 {
|
|
|
|
redisClusterOptions = &redis.ClusterOptions{Addrs: nodes}
|
|
|
|
}
|
|
|
|
|
|
|
|
if redisOptions != nil {
|
|
|
|
r.cli = redis.NewClient(redisOptions)
|
|
|
|
} else if redisClusterOptions != nil {
|
|
|
|
r.cli = redis.NewClusterClient(redisClusterOptions)
|
|
|
|
}
|
2020-05-13 12:59:23 +03:00
|
|
|
|
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|