bouncer/internal/proxy/director/layer/authn/adapter/redis/adapter.go

59 lines
1.2 KiB
Go
Raw Normal View History

package redis
import (
"context"
"time"
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/authn"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
)
type StoreAdapter struct {
client redis.UniversalClient
}
// Del implements authn.StoreAdapter.
func (s *StoreAdapter) Del(ctx context.Context, key string) error {
if err := s.client.Del(ctx, key).Err(); err != nil {
return errors.WithStack(err)
}
return nil
}
// Get implements authn.StoreAdapter.
func (s *StoreAdapter) Get(ctx context.Context, key string) ([]byte, error) {
cmd := s.client.Get(ctx, key)
if err := cmd.Err(); err != nil {
return nil, errors.WithStack(err)
}
data, err := cmd.Bytes()
if err != nil {
return nil, errors.WithStack(err)
}
return data, nil
}
// Set implements authn.StoreAdapter.
func (s *StoreAdapter) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error {
if err := s.client.Set(ctx, key, data, ttl).Err(); err != nil {
return errors.WithStack(err)
}
return nil
}
func NewStoreAdapter(client redis.UniversalClient) *StoreAdapter {
return &StoreAdapter{
client: client,
}
}
var (
_ authn.StoreAdapter = &StoreAdapter{}
)