48 lines
675 B
Go
48 lines
675 B
Go
package mila
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
type MemoryStore struct {
|
|
mu sync.RWMutex
|
|
state ContentState
|
|
ready bool
|
|
}
|
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{}
|
|
}
|
|
|
|
func (s *MemoryStore) Get(ctx context.Context) (ContentState, error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ContentState{}, ctx.Err()
|
|
default:
|
|
}
|
|
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
if !s.ready {
|
|
return ContentState{}, ErrNotReady
|
|
}
|
|
return s.state, nil
|
|
}
|
|
|
|
func (s *MemoryStore) Set(ctx context.Context, state ContentState) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
s.state = state
|
|
s.ready = true
|
|
return nil
|
|
}
|