buildr/modules/state/in_memory.go
Peter 1261932bdc
All checks were successful
continuous-integration/drone/push Build is passing
refactor: apply golangci-lint findings
2023-06-22 19:16:00 +02:00

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
}
}