35 lines
633 B
Go
35 lines
633 B
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"github.com/abelith/gomax/state"
|
|
"sync"
|
|
)
|
|
|
|
type FSMStorage struct {
|
|
m sync.Map
|
|
}
|
|
|
|
func NewMemoryFSMStorage() *FSMStorage {
|
|
return &FSMStorage{}
|
|
}
|
|
|
|
func (f *FSMStorage) Get(_ context.Context, userID int) (*state.Snapshot, error) {
|
|
st, ok := f.m.Load(userID)
|
|
if !ok {
|
|
return nil, state.ErrStateNotFound
|
|
}
|
|
|
|
return st.(*state.Snapshot), nil
|
|
}
|
|
|
|
func (f *FSMStorage) Put(_ context.Context, snapshot *state.Snapshot) error {
|
|
f.m.Store(snapshot.ID, snapshot)
|
|
return nil
|
|
}
|
|
|
|
func (f *FSMStorage) Delete(_ context.Context, snapshot *state.Snapshot) error {
|
|
f.m.Delete(snapshot.ID)
|
|
return nil
|
|
}
|