41 lines
722 B
Go
41 lines
722 B
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
var _ Store = (*InMemoryStore)(nil)
|
|
|
|
type InMemoryStore struct {
|
|
data map[string][]byte
|
|
lock sync.RWMutex
|
|
}
|
|
|
|
func (i *InMemoryStore) Set(_ context.Context, key Key, state []byte, opts ...EntryOption) error {
|
|
i.lock.Lock()
|
|
defer i.lock.Unlock()
|
|
|
|
if i.data == nil {
|
|
i.data = make(map[string][]byte)
|
|
}
|
|
|
|
i.data[string(key.Bytes())] = state
|
|
return nil
|
|
}
|
|
|
|
func (i *InMemoryStore) Get(ctx context.Context, key Key) (state []byte, meta Metadata, err error) {
|
|
i.lock.RLock()
|
|
defer i.lock.RUnlock()
|
|
|
|
if i.data == nil {
|
|
return nil, Metadata{}, nil
|
|
}
|
|
|
|
if d, ok := i.data[string(key.Bytes())]; ok {
|
|
return d, Metadata{}, nil
|
|
} else {
|
|
return nil, Metadata{}, nil
|
|
}
|
|
}
|