2023-10-20 23:51:29 +03:00
|
|
|
package redis
|
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"
|
2022-10-26 13:46:48 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-01-16 03:00:25 +03:00
|
|
|
"time"
|
2020-04-08 17:08:41 +03:00
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2023-10-21 00:57:12 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
"go.unistack.org/micro/v4/store"
|
2019-10-03 11:57:49 +03:00
|
|
|
)
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
type Store struct {
|
2021-01-16 03:00:25 +03:00
|
|
|
opts store.Options
|
2024-10-04 00:20:23 +03:00
|
|
|
cli redis.UniversalClient
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Connect(ctx context.Context) error {
|
2022-10-26 13:46:48 +03:00
|
|
|
return r.cli.Ping(ctx).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) Init(opts ...options.Option) error {
|
2020-05-13 12:59:23 +03:00
|
|
|
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
|
|
|
|
2024-10-04 00:20:23 +03:00
|
|
|
func (r *Store) Redis() redis.UniversalClient {
|
|
|
|
return r.cli
|
2023-12-12 13:48:47 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Disconnect(ctx context.Context) error {
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.cli.Close()
|
2020-04-08 16:34:27 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) Exists(ctx context.Context, key string, opts ...options.Option) error {
|
2023-10-20 23:51:29 +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
|
|
|
options := store.NewExistsOptions(opts...)
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
if options.Namespace != "" {
|
|
|
|
key = fmt.Sprintf("%s%s", r.opts.Namespace, key)
|
|
|
|
}
|
|
|
|
val, err := r.cli.Exists(ctx, key).Result()
|
2021-01-16 03:00:25 +03:00
|
|
|
if err != nil {
|
2021-05-20 22:49:30 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
if val == 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
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
2023-10-20 23:51:29 +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
|
|
|
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-10-26 13:46:48 +03:00
|
|
|
if options.Namespace != "" {
|
|
|
|
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2022-10-26 13:46:48 +03:00
|
|
|
buf, err := r.cli.Get(ctx, key).Bytes()
|
2021-01-16 03:00:25 +03:00
|
|
|
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
|
|
|
return r.opts.Codec.Unmarshal(buf, val)
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...options.Option) error {
|
2022-10-26 13:46:48 +03:00
|
|
|
if len(keys) == 1 {
|
|
|
|
vt := reflect.ValueOf(vals)
|
|
|
|
if vt.Kind() == reflect.Ptr {
|
|
|
|
vt = reflect.Indirect(vt)
|
|
|
|
return r.Read(ctx, keys[0], vt.Index(0).Interface(), opts...)
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
options := store.NewReadOptions(opts...)
|
|
|
|
rkeys := make([]string, 0, len(keys))
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
|
|
|
}
|
|
|
|
for _, key := range keys {
|
|
|
|
if options.Namespace != "" {
|
|
|
|
rkeys = append(rkeys, fmt.Sprintf("%s%s", options.Namespace, key))
|
|
|
|
} else {
|
|
|
|
rkeys = append(rkeys, key)
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2022-10-26 13:46:48 +03:00
|
|
|
rvals, err := r.cli.MGet(ctx, rkeys...).Result()
|
|
|
|
if err != nil && err == redis.Nil {
|
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(rvals) == 0 {
|
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
vv := reflect.ValueOf(vals)
|
|
|
|
vt := reflect.TypeOf(vals)
|
|
|
|
switch vv.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
vv = reflect.Indirect(vv)
|
|
|
|
vt = vt.Elem()
|
|
|
|
}
|
|
|
|
if vv.Kind() != reflect.Slice {
|
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
|
|
|
nvv := reflect.MakeSlice(vt, len(rvals), len(rvals))
|
|
|
|
vt = vt.Elem()
|
|
|
|
for idx := 0; idx < len(rvals); idx++ {
|
|
|
|
if rvals[idx] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
itm := nvv.Index(idx)
|
|
|
|
var buf []byte
|
|
|
|
switch b := rvals[idx].(type) {
|
|
|
|
case []byte:
|
|
|
|
buf = b
|
|
|
|
case string:
|
|
|
|
buf = []byte(b)
|
|
|
|
}
|
|
|
|
// special case for raw data
|
|
|
|
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
|
|
|
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
|
|
|
} else {
|
|
|
|
itm.Set(reflect.New(vt.Elem()))
|
|
|
|
}
|
|
|
|
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vv.Set(nvv)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...options.Option) error {
|
2023-10-20 23:51:29 +03:00
|
|
|
if len(keys) == 1 {
|
|
|
|
return r.Delete(ctx, keys[0], opts...)
|
|
|
|
}
|
|
|
|
if r.opts.Timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
2023-03-30 20:30:31 +03:00
|
|
|
options := store.NewDeleteOptions(opts...)
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
|
|
|
}
|
|
|
|
if options.Namespace == "" {
|
|
|
|
return r.cli.Del(ctx, keys...).Err()
|
|
|
|
}
|
|
|
|
for idx := range keys {
|
|
|
|
keys[idx] = options.Namespace + keys[idx]
|
|
|
|
}
|
|
|
|
return r.cli.Del(ctx, keys...).Err()
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) Delete(ctx context.Context, key string, opts ...options.Option) error {
|
2023-10-20 23:51:29 +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
|
|
|
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-10-26 13:46:48 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
return r.cli.Del(ctx, key).Err()
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
return r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...options.Option) error {
|
2023-10-20 23:51:29 +03:00
|
|
|
if len(keys) == 1 {
|
|
|
|
return r.Write(ctx, keys[0], vals[0], opts...)
|
|
|
|
}
|
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
|
|
|
options := store.NewWriteOptions(opts...)
|
2023-10-20 23:51:29 +03:00
|
|
|
kvs := make([]string, 0, len(keys)*2)
|
|
|
|
|
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
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
for idx, key := range keys {
|
|
|
|
if options.Namespace != "" {
|
|
|
|
kvs = append(kvs, fmt.Sprintf("%s%s", options.Namespace, key))
|
|
|
|
} else {
|
|
|
|
kvs = append(kvs, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vt := vals[idx].(type) {
|
|
|
|
case string:
|
|
|
|
kvs = append(kvs, vt)
|
|
|
|
case []byte:
|
|
|
|
kvs = append(kvs, string(vt))
|
|
|
|
default:
|
|
|
|
buf, err := r.opts.Codec.Marshal(vt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
kvs = append(kvs, string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds, err := r.cli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
|
|
for idx := 0; idx < len(kvs); idx += 2 {
|
|
|
|
pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-01-16 03:00:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if err = cmd.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
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()
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
options := store.NewWriteOptions(opts...)
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
switch vt := val.(type) {
|
|
|
|
case string:
|
|
|
|
buf = []byte(vt)
|
|
|
|
case []byte:
|
|
|
|
buf = vt
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
buf, err = r.opts.Codec.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Namespace != "" {
|
|
|
|
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:46:48 +03:00
|
|
|
return r.cli.Set(ctx, key, buf, options.TTL).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func (r *Store) List(ctx context.Context, opts ...options.Option) ([]string, error) {
|
2021-01-16 03:00:25 +03:00
|
|
|
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-10-26 13:46:48 +03:00
|
|
|
|
|
|
|
rkey := fmt.Sprintf("%s%s*", options.Namespace, options.Prefix)
|
|
|
|
if options.Suffix != "" {
|
|
|
|
rkey += options.Suffix
|
|
|
|
}
|
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
|
2022-10-26 13:46:48 +03:00
|
|
|
keys, err := r.cli.Keys(ctx, rkey).Result()
|
2019-10-03 11:57:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
return keys, nil
|
|
|
|
}
|
2019-12-04 13:36:30 +03:00
|
|
|
|
2022-10-26 13:46:48 +03:00
|
|
|
nkeys := make([]string, 0, len(keys))
|
|
|
|
for _, key := range keys {
|
|
|
|
nkeys = append(nkeys, strings.TrimPrefix(key, options.Namespace))
|
|
|
|
}
|
|
|
|
return nkeys, nil
|
2020-03-13 01:44:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Options() store.Options {
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.opts
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Name() string {
|
2021-01-29 22:47:57 +03:00
|
|
|
return r.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) String() string {
|
2019-10-03 11:57:49 +03:00
|
|
|
return "redis"
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:57:12 +03:00
|
|
|
func NewStore(opts ...options.Option) *Store {
|
2023-10-20 23:51:29 +03:00
|
|
|
return &Store{opts: store.NewOptions(opts...)}
|
2020-05-13 12:59:23 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) configure() error {
|
2024-10-04 00:20:23 +03:00
|
|
|
var redisUniversalOptions *redis.UniversalOptions
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2023-03-17 12:40:50 +03:00
|
|
|
if r.cli != nil && r.opts.Context == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
if r.opts.Context != nil {
|
2024-10-04 00:20:23 +03:00
|
|
|
if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
|
|
|
|
redisUniversalOptions = c
|
2023-02-01 01:48:47 +03:00
|
|
|
if r.opts.TLSConfig != nil {
|
2024-10-04 00:20:23 +03:00
|
|
|
redisUniversalOptions.TLSConfig = r.opts.TLSConfig
|
2023-02-01 01:48:47 +03:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 00:20:23 +03:00
|
|
|
if redisUniversalOptions == nil && r.cli != nil {
|
2023-03-17 12:40:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-04 00:20:23 +03:00
|
|
|
if redisUniversalOptions == nil {
|
|
|
|
redisUniversalOptions = &redis.UniversalOptions{
|
2023-03-17 12:40:50 +03:00
|
|
|
Username: "",
|
2023-02-01 01:48:47 +03:00
|
|
|
Password: "", // no password set
|
2024-10-04 00:20:23 +03:00
|
|
|
DB: 0, // use default DB
|
2023-02-01 01:48:47 +03:00
|
|
|
MaxRetries: 2,
|
|
|
|
MaxRetryBackoff: 256 * time.Millisecond,
|
|
|
|
DialTimeout: 1 * time.Second,
|
|
|
|
ReadTimeout: 1 * time.Second,
|
|
|
|
WriteTimeout: 1 * time.Second,
|
|
|
|
PoolTimeout: 1 * time.Second,
|
|
|
|
MinIdleConns: 10,
|
|
|
|
TLSConfig: r.opts.TLSConfig,
|
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2024-10-04 00:20:23 +03:00
|
|
|
if len(r.opts.Address) > 0 {
|
|
|
|
redisUniversalOptions.Addrs = r.opts.Address
|
|
|
|
} else if len(redisUniversalOptions.Addrs) == 0 {
|
|
|
|
redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
2020-05-13 12:59:23 +03:00
|
|
|
|
2024-10-04 00:20:23 +03:00
|
|
|
r.cli = redis.NewUniversalClient(redisUniversalOptions)
|
|
|
|
|
2020-05-13 12:59:23 +03:00
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|