Files
Supervisor/internal/store/memory/store.go

52 lines
1.0 KiB
Go

package memory
import (
"context"
"sort"
"supervisor/internal/domain"
"sync"
)
type Store struct {
mu sync.RWMutex
sessions map[string]domain.Session
}
func NewStore() *Store {
return &Store{sessions: make(map[string]domain.Session)}
}
func (s *Store) Upsert(_ context.Context, session domain.Session) error {
s.mu.Lock()
defer s.mu.Unlock()
s.sessions[session.ID] = session
return nil
}
func (s *Store) Get(_ context.Context, id string) (domain.Session, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
session, ok := s.sessions[id]
return session, ok, nil
}
func (s *Store) List(_ context.Context) ([]domain.Session, error) {
s.mu.RLock()
defer s.mu.RUnlock()
items := make([]domain.Session, 0, len(s.sessions))
for _, session := range s.sessions {
items = append(items, session)
}
sort.Slice(items, func(i, j int) bool {
return items[i].CreatedAt.After(items[j].CreatedAt)
})
return items, nil
}
func (s *Store) Delete(_ context.Context, id string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.sessions, id)
return nil
}