micro/store/etcd/etcd.go

51 lines
855 B
Go
Raw Normal View History

package etcd
2015-02-07 23:12:57 +03:00
import (
"errors"
"github.com/coreos/go-etcd/etcd"
"github.com/myodc/go-micro/store"
2015-02-07 23:12:57 +03:00
)
type estore struct {
2015-02-07 23:12:57 +03:00
Client *etcd.Client
}
func (e *estore) Get(key string) (*store.Item, error) {
2015-02-07 23:12:57 +03:00
kv, err := e.Client.Get(key, false, false)
if err != nil {
return nil, err
}
if kv == nil {
return nil, errors.New("key not found")
}
return &store.Item{
Key: kv.Node.Key,
Value: []byte(kv.Node.Value),
2015-02-07 23:12:57 +03:00
}, nil
}
func (e *estore) Del(key string) error {
2015-02-07 23:12:57 +03:00
_, err := e.Client.Delete(key, false)
return err
}
func (e *estore) Put(item *store.Item) error {
_, err := e.Client.Set(item.Key, string(item.Value), 0)
2015-02-07 23:12:57 +03:00
return err
}
func NewStore(addrs []string, opts ...store.Option) store.Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:2379"}
}
client := etcd.NewClient(addrs)
2015-02-07 23:12:57 +03:00
return &estore{
2015-02-07 23:12:57 +03:00
Client: client,
}
}