MSP-437 add unused redis cache
This commit is contained in:
1 parent
e663429dfe
commit
773fbe7883
2 files changed
+141
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package unused
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/integration-system/isp-kit/json"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type RedisAuthCache struct {
|
||||
cli redis.UniversalClient
|
||||
duration time.Duration
|
||||
db int
|
||||
}
|
||||
|
||||
func NewRedisAuthCache(cli redis.UniversalClient, duration time.Duration, db int) RedisAuthCache {
|
||||
return RedisAuthCache{
|
||||
cli: cli,
|
||||
duration: duration,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r RedisAuthCache) Get(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
results, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.Get(ctx, token)
|
||||
return nil
|
||||
})
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
data, err := results[1].(*redis.StringCmd).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, domain.ErrAuthenticationCacheMiss
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get")
|
||||
}
|
||||
|
||||
result := domain.AuthData{}
|
||||
err = json.Unmarshal([]byte(data), &result)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "json unmarshal auth data")
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r RedisAuthCache) Set(ctx context.Context, token string, data domain.AuthData) error {
|
||||
value, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "json marshal auth data")
|
||||
}
|
||||
|
||||
results, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.SetEX(ctx, token, string(value), r.duration)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
err = results[1].Err()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set ex")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package unused
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type RedisAuthzCache struct {
|
||||
cli redis.UniversalClient
|
||||
duration time.Duration
|
||||
db int
|
||||
}
|
||||
|
||||
func NewRedisAuthzCache(cli redis.UniversalClient, duration time.Duration, db int) *RedisAuthzCache {
|
||||
return &RedisAuthzCache{
|
||||
cli: cli,
|
||||
duration: duration,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) Get(ctx context.Context, applicationId int, endpoint string) (bool, error) {
|
||||
result, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.Get(ctx, r.key(applicationId, endpoint))
|
||||
return nil
|
||||
})
|
||||
if err != nil && err != redis.Nil {
|
||||
return false, errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
_, err = result[1].(*redis.StringCmd).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "get")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) SetAuthorized(ctx context.Context, applicationId int, endpoint string) error {
|
||||
result, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.SetEX(ctx, r.key(applicationId, endpoint), "authorized", r.duration)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
err = result[1].Err()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set ex")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) key(applicationId int, endpoint string) string {
|
||||
return fmt.Sprintf("%d:%s", applicationId, endpoint)
|
||||
}
|
||||
Reference in new issue
Block a user