mirror of
https://github.com/nais/wonderwall.git
synced 2026-05-08 09:27:12 +00:00
Co-authored-by: Morten Lied Johansen <morten.lied.johansen@nav.no> Co-authored-by: Sindre Rødseth Hansen <sindre.rodseth.hansen@nav.no>
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package session
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/nais/wonderwall/pkg/metrics"
|
|
"time"
|
|
)
|
|
|
|
type redisSessionStore struct {
|
|
client redis.Cmdable
|
|
}
|
|
|
|
var _ Store = &redisSessionStore{}
|
|
|
|
func NewRedis(client redis.Cmdable) Store {
|
|
return &redisSessionStore{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (s *redisSessionStore) Read(ctx context.Context, key string) (*EncryptedData, error) {
|
|
encryptedData := &EncryptedData{}
|
|
err := metrics.ObserveRedisLatency("Read", func() error {
|
|
return s.client.Get(ctx, key).Scan(encryptedData)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return encryptedData, nil
|
|
}
|
|
|
|
func (s *redisSessionStore) Write(ctx context.Context, key string, value *EncryptedData, expiration time.Duration) error {
|
|
return metrics.ObserveRedisLatency("Write", func() error {
|
|
return s.client.Set(ctx, key, value, expiration).Err()
|
|
})
|
|
}
|
|
|
|
func (s *redisSessionStore) Delete(ctx context.Context, keys ...string) error {
|
|
return metrics.ObserveRedisLatency("Delete", func() error {
|
|
return s.client.Del(ctx, keys...).Err()
|
|
})
|
|
}
|