Files
wonderwall/pkg/session/memory.go
2021-09-30 18:27:53 +02:00

53 lines
936 B
Go

package session
import (
"context"
"fmt"
"sync"
"time"
)
type memorySessionStore struct {
lock sync.Mutex
sessions map[string]*EncryptedData
}
var _ Store = &memorySessionStore{}
func NewMemory() Store {
return &memorySessionStore{
sessions: make(map[string]*EncryptedData),
}
}
func (s *memorySessionStore) Read(_ context.Context, key string) (*EncryptedData, error) {
s.lock.Lock()
defer s.lock.Unlock()
data, ok := s.sessions[key]
if !ok {
return nil, fmt.Errorf("no such session: %s", key)
}
return data, nil
}
func (s *memorySessionStore) Write(_ context.Context, key string, value *EncryptedData, expiration time.Duration) error {
s.lock.Lock()
defer s.lock.Unlock()
s.sessions[key] = value
return nil
}
func (s *memorySessionStore) Delete(_ context.Context, keys ...string) error {
s.lock.Lock()
defer s.lock.Unlock()
for _, key := range keys {
delete(s.sessions, key)
}
return nil
}