src.dualinventive.com/go/devsim/simulator/storage.go

43 lines
998 B
Go

package simulator
import (
"src.dualinventive.com/go/lib/kv"
)
// Storage for key values
type Storage interface {
Set(key string, value interface{}) error
Get(key string) (interface{}, error)
Del(key string) error
}
// KVStorage wraps a single Storage onto a dinet DeviceUID
type KVStorage struct {
deviceUID string
storage kv.KV
}
// NewKVStorage create a dinet/kv storage bound onto DeviceUID
func NewKVStorage(deviceUID, KVType, KVParam string) (Storage, error) {
kvstorage, err := kv.New(KVType, KVParam)
if err != nil {
return nil, err
}
return &KVStorage{deviceUID: deviceUID, storage: kvstorage}, nil
}
// Set a value for key
func (k *KVStorage) Set(key string, value interface{}) error {
return k.storage.HSet(k.deviceUID, key, value)
}
// Get a value for key
func (k *KVStorage) Get(key string) (interface{}, error) {
return k.storage.HGet(k.deviceUID, key)
}
// Del a key/value
func (k *KVStorage) Del(key string) error {
return k.storage.HDel(k.deviceUID, key)
}