2024-01-22 17:22:41: merge [master:56d3e06e49d7a75f142424293788a4f3b6245430];

This commit is contained in:
txix committed 2024-01-22 17:22:41 +03:00
1 parent 7014f56cbb
commit 2b91caece0
60 files changed
+3558

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
package repository
import (
"context"
"time"
"github.com/integration-system/isp-kit/json"
"github.com/pkg/errors"
"isp-gate-service/cache"
"isp-gate-service/domain"
)
type AuthenticationCache struct {
cache *cache.Cache
duration time.Duration
}
func NewAuthenticationCache(duration time.Duration) AuthenticationCache {
return AuthenticationCache{
duration: duration,
cache: cache.New(),
}
}
func (r AuthenticationCache) Get(ctx context.Context, token string) (*domain.AuthData, error) {
data, ok := r.cache.Get(token)
if !ok {
return nil, domain.ErrAuthenticationCacheMiss
}
result := domain.AuthData{}
err := json.Unmarshal(data, &result)
if err != nil {
return nil, errors.WithMessage(err, "json unmarshal auth data")
}
return &result, nil
}
func (r AuthenticationCache) 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")
}
r.cache.Set(token, value, r.duration)
return nil
}