From 97fcc21fc61b1316e9aa82e4da39e0d7073b1031 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 22 Apr 2022 17:13:11 +0400 Subject: [PATCH] MSP-623 reimplement redis cli --- entity/redis.go | 15 +++++ redis/client.go | 34 ---------- redis/redis.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 34 deletions(-) create mode 100644 entity/redis.go delete mode 100644 redis/client.go create mode 100644 redis/redis.go diff --git a/entity/redis.go b/entity/redis.go new file mode 100644 index 0000000..c03a5d1 --- /dev/null +++ b/entity/redis.go @@ -0,0 +1,15 @@ +package entity + +type RedisSetToken struct { + Token string + ExpireTime int + DomainIdentity int + ServiceIdentity int + ApplicationIdentity int +} + +type RedisApplicationPermission struct { + AppId int + Method string + Value bool +} diff --git a/redis/client.go b/redis/client.go deleted file mode 100644 index 8475aa3..0000000 --- a/redis/client.go +++ /dev/null @@ -1,34 +0,0 @@ -package redis - -import ( - "isp-system-service/log_code" - - rd "github.com/integration-system/isp-lib/v2/redis" - "github.com/integration-system/isp-lib/v2/structure" - log "github.com/integration-system/isp-log" -) - -var Client = &redisClient{ - cli: rd.NewRxClient( - rd.WithInitHandler(func(c *rd.Client, err error) { - if err != nil { - log.Fatal(log_code.ErrorRedisClient, err) - } - })), -} - -type redisClient struct { - cli *rd.RxClient -} - -func (c *redisClient) ReceiveConfiguration(configuration structure.RedisConfiguration) { - c.cli.ReceiveConfiguration(configuration) -} - -func (c *redisClient) Get() *rd.RxClient { - return c.cli -} - -func (c *redisClient) Close() { - _ = c.cli.Close() -} diff --git a/redis/redis.go b/redis/redis.go new file mode 100644 index 0000000..2b797ff --- /dev/null +++ b/redis/redis.go @@ -0,0 +1,164 @@ +package redis + +import ( + "context" + "fmt" + "time" + + rd "github.com/go-redis/redis/v8" + "github.com/integration-system/isp-lib/v2/redis" + "github.com/pkg/errors" + "isp-system-service/domain" + "isp-system-service/entity" +) + +const ( + systemIdentityFieldInDb = "1" + domainIdentityFieldInDb = "2" + serviceIdentityFieldInDb = "3" + applicationIdentityFieldInDb = "4" +) + +type Client struct { + instanceUuid string + cli *redis.RxClient +} + +func NewClient(instanceUuid string, cli *redis.RxClient) Client { + return Client{ + cli: cli, + instanceUuid: instanceUuid, + } +} + +func (r Client) SetApplicationToken(ctx context.Context, req entity.RedisSetToken) error { + _, err := r.cli.UseDbTx(redis.ApplicationTokenDb, func(pipeline rd.Pipeliner) error { + idMap := map[string]interface{}{ + systemIdentityFieldInDb: domain.DefaultSystemId, + domainIdentityFieldInDb: req.DomainIdentity, + serviceIdentityFieldInDb: req.ServiceIdentity, + applicationIdentityFieldInDb: req.ApplicationIdentity, + } + + key := fmt.Sprintf("%s|%s", req.Token, r.instanceUuid) + stat := pipeline.HMSet(ctx, key, idMap) + err := stat.Err() + if err != nil { + return errors.WithMessagef(err, "pipeline hmset") + } + + if req.ExpireTime > 0 { + err = pipeline.Expire(ctx, req.Token, time.Duration(req.ExpireTime)*time.Millisecond).Err() + if err != nil { + return errors.WithMessage(err, "pipeline expire token") + } + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_token transaction") + } + + return nil +} + +func (r Client) UpdateApplicationPermission(ctx context.Context, req entity.RedisApplicationPermission) error { + _, err := r.cli.UseDb(redis.ApplicationPermissionDb, func(pipeline rd.Pipeliner) error { + key := fmt.Sprintf("%d|%s", req.AppId, req.Method) + _, err := pipeline.Set(ctx, key, req.Value, 0).Result() + if err != nil { + return errors.WithMessage(err, "pipeline set") + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_permission transaction") + } + + return nil +} + +func (r Client) UpdateApplicationPermissionList(ctx context.Context, removed []string, added []interface{}) error { + _, err := r.cli.UseDb(redis.ApplicationPermissionDb, func(pipeline rd.Pipeliner) error { + if len(removed) > 0 { + _, err := pipeline.Del(ctx, removed...).Result() + if err != nil { + return errors.WithMessage(err, "pipeline del") + } + } + + if len(added) > 0 { + _, err := pipeline.MSet(ctx, added...).Result() + if err != nil { + return errors.WithMessage(err, "pipeline mset") + } + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_permission transaction") + } + + return nil +} + +func (r Client) DeleteApplication(ctx context.Context, appTokens []string, applicationPermissionList []string) error { + keys := make([]string, len(appTokens)) + for i, token := range appTokens { + keys[i] = fmt.Sprintf("%s|%s", token, r.instanceUuid) + } + + _, err := r.cli.UseDbTx(redis.ApplicationTokenDb, func(pipeline rd.Pipeliner) error { + if len(appTokens) > 0 { + _, err := pipeline.Del(ctx, keys...).Result() + if err != nil { + return errors.WithMessage(err, "pipeline del") + } + } + + if len(applicationPermissionList) > 0 { + _, err := r.cli.UseDb(redis.ApplicationPermissionDb, func(pipeliner rd.Pipeliner) error { + _, err := pipeliner.Del(ctx, applicationPermissionList...).Result() + if err != nil { + return errors.WithMessage(err, "pipeline del") + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_permission transaction") + } + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_token transaction") + } + + return nil +} + +func (r Client) DeleteToken(ctx context.Context, tokens []string) error { + keys := make([]string, len(tokens)) + for i, token := range tokens { + keys[i] = fmt.Sprintf("%s|%s", token, r.instanceUuid) + } + + _, err := r.cli.UseDbTx(redis.ApplicationTokenDb, func(p rd.Pipeliner) error { + _, err := p.Del(ctx, keys...).Result() + if err != nil { + return errors.WithMessage(err, "del keys") + } + + return nil + }) + if err != nil { + return errors.WithMessage(err, "application_token transaction") + } + + return nil +}