Delete store, this is not a core requirement of microservices, will be included in go-platform
This commit is contained in:
		| @@ -10,7 +10,6 @@ An example server can be found in go-micro/template. | |||||||
| - Discovery | - Discovery | ||||||
| - Client/Server | - Client/Server | ||||||
| - Pub/Sub | - Pub/Sub | ||||||
| - Key/Value store |  | ||||||
|  |  | ||||||
| ### Planned | ### Planned | ||||||
| - Metrics | - Metrics | ||||||
|   | |||||||
							
								
								
									
										31
									
								
								cmd/cmd.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								cmd/cmd.go
									
									
									
									
									
								
							| @@ -12,7 +12,6 @@ import ( | |||||||
| 	"github.com/myodc/go-micro/client" | 	"github.com/myodc/go-micro/client" | ||||||
| 	"github.com/myodc/go-micro/registry" | 	"github.com/myodc/go-micro/registry" | ||||||
| 	"github.com/myodc/go-micro/server" | 	"github.com/myodc/go-micro/server" | ||||||
| 	"github.com/myodc/go-micro/store" |  | ||||||
| 	"github.com/myodc/go-micro/transport" | 	"github.com/myodc/go-micro/transport" | ||||||
|  |  | ||||||
| 	// brokers | 	// brokers | ||||||
| @@ -23,12 +22,6 @@ import ( | |||||||
| 	"github.com/myodc/go-micro/registry/consul" | 	"github.com/myodc/go-micro/registry/consul" | ||||||
| 	"github.com/myodc/go-micro/registry/kubernetes" | 	"github.com/myodc/go-micro/registry/kubernetes" | ||||||
|  |  | ||||||
| 	// stores |  | ||||||
| 	sconsul "github.com/myodc/go-micro/store/consul" |  | ||||||
| 	"github.com/myodc/go-micro/store/etcd" |  | ||||||
| 	"github.com/myodc/go-micro/store/memcached" |  | ||||||
| 	"github.com/myodc/go-micro/store/memory" |  | ||||||
|  |  | ||||||
| 	// transport | 	// transport | ||||||
| 	thttp "github.com/myodc/go-micro/transport/http" | 	thttp "github.com/myodc/go-micro/transport/http" | ||||||
| 	tnats "github.com/myodc/go-micro/transport/nats" | 	tnats "github.com/myodc/go-micro/transport/nats" | ||||||
| @@ -65,17 +58,6 @@ var ( | |||||||
| 			EnvVar: "MICRO_REGISTRY_ADDRESS", | 			EnvVar: "MICRO_REGISTRY_ADDRESS", | ||||||
| 			Usage:  "Comma-separated list of registry addresses", | 			Usage:  "Comma-separated list of registry addresses", | ||||||
| 		}, | 		}, | ||||||
| 		cli.StringFlag{ |  | ||||||
| 			Name:   "store", |  | ||||||
| 			EnvVar: "MICRO_STORE", |  | ||||||
| 			Value:  "consul", |  | ||||||
| 			Usage:  "Store used as a basic key/value store using consul, memcached, etc", |  | ||||||
| 		}, |  | ||||||
| 		cli.StringFlag{ |  | ||||||
| 			Name:   "store_address", |  | ||||||
| 			EnvVar: "MICRO_STORE_ADDRESS", |  | ||||||
| 			Usage:  "Comma-separated list of store addresses", |  | ||||||
| 		}, |  | ||||||
| 		cli.StringFlag{ | 		cli.StringFlag{ | ||||||
| 			Name:   "transport", | 			Name:   "transport", | ||||||
| 			EnvVar: "MICRO_TRANSPORT", | 			EnvVar: "MICRO_TRANSPORT", | ||||||
| @@ -111,19 +93,6 @@ func Setup(c *cli.Context) error { | |||||||
| 		registry.DefaultRegistry = consul.NewRegistry(rAddrs) | 		registry.DefaultRegistry = consul.NewRegistry(rAddrs) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	sAddrs := strings.Split(c.String("store_address"), ",") |  | ||||||
|  |  | ||||||
| 	switch c.String("store") { |  | ||||||
| 	case "consul": |  | ||||||
| 		store.DefaultStore = sconsul.NewStore(sAddrs) |  | ||||||
| 	case "memcached": |  | ||||||
| 		store.DefaultStore = memcached.NewStore(sAddrs) |  | ||||||
| 	case "memory": |  | ||||||
| 		store.DefaultStore = memory.NewStore(sAddrs) |  | ||||||
| 	case "etcd": |  | ||||||
| 		store.DefaultStore = etcd.NewStore(sAddrs) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	tAddrs := strings.Split(c.String("transport_address"), ",") | 	tAddrs := strings.Split(c.String("transport_address"), ",") | ||||||
|  |  | ||||||
| 	switch c.String("transport") { | 	switch c.String("transport") { | ||||||
|   | |||||||
| @@ -1,11 +0,0 @@ | |||||||
| package consul |  | ||||||
|  |  | ||||||
| // This is a hack |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"github.com/myodc/go-micro/store" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| func NewStore(addrs []string, opt ...store.Option) store.Store { |  | ||||||
| 	return store.NewStore(addrs, opt...) |  | ||||||
| } |  | ||||||
| @@ -1,53 +0,0 @@ | |||||||
| package store |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"errors" |  | ||||||
|  |  | ||||||
| 	consul "github.com/hashicorp/consul/api" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| type consulStore struct { |  | ||||||
| 	Client *consul.Client |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (c *consulStore) Get(key string) (*Item, error) { |  | ||||||
| 	kv, _, err := c.Client.KV().Get(key, nil) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
| 	if kv == nil { |  | ||||||
| 		return nil, errors.New("key not found") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return &Item{ |  | ||||||
| 		Key:   kv.Key, |  | ||||||
| 		Value: kv.Value, |  | ||||||
| 	}, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (c *consulStore) Del(key string) error { |  | ||||||
| 	_, err := c.Client.KV().Delete(key, nil) |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (c *consulStore) Put(item *Item) error { |  | ||||||
| 	_, err := c.Client.KV().Put(&consul.KVPair{ |  | ||||||
| 		Key:   item.Key, |  | ||||||
| 		Value: item.Value, |  | ||||||
| 	}, nil) |  | ||||||
|  |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func newConsulStore(addrs []string, opt ...Option) Store { |  | ||||||
| 	config := consul.DefaultConfig() |  | ||||||
| 	if len(addrs) > 0 { |  | ||||||
| 		config.Address = addrs[0] |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	client, _ := consul.NewClient(config) |  | ||||||
|  |  | ||||||
| 	return &consulStore{ |  | ||||||
| 		Client: client, |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -1,50 +0,0 @@ | |||||||
| package etcd |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"errors" |  | ||||||
|  |  | ||||||
| 	"github.com/coreos/go-etcd/etcd" |  | ||||||
| 	"github.com/myodc/go-micro/store" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| type estore struct { |  | ||||||
| 	Client *etcd.Client |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (e *estore) Get(key string) (*store.Item, error) { |  | ||||||
| 	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), |  | ||||||
| 	}, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (e *estore) Del(key string) error { |  | ||||||
| 	_, 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) |  | ||||||
|  |  | ||||||
| 	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) |  | ||||||
|  |  | ||||||
| 	return &estore{ |  | ||||||
| 		Client: client, |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -1,50 +0,0 @@ | |||||||
| package memcached |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"errors" |  | ||||||
|  |  | ||||||
| 	mc "github.com/bradfitz/gomemcache/memcache" |  | ||||||
| 	"github.com/myodc/go-micro/store" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| type mstore struct { |  | ||||||
| 	Client *mc.Client |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Get(key string) (*store.Item, error) { |  | ||||||
| 	kv, err := m.Client.Get(key) |  | ||||||
| 	if err != nil && err == mc.ErrCacheMiss { |  | ||||||
| 		return nil, errors.New("key not found") |  | ||||||
| 	} else if err != nil { |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if kv == nil { |  | ||||||
| 		return nil, errors.New("key not found") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return &store.Item{ |  | ||||||
| 		Key:   kv.Key, |  | ||||||
| 		Value: kv.Value, |  | ||||||
| 	}, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Del(key string) error { |  | ||||||
| 	return m.Client.Delete(key) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Put(item *store.Item) error { |  | ||||||
| 	return m.Client.Set(&mc.Item{ |  | ||||||
| 		Key:   item.Key, |  | ||||||
| 		Value: item.Value, |  | ||||||
| 	}) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func NewStore(addrs []string, opts ...store.Option) store.Store { |  | ||||||
| 	if len(addrs) == 0 { |  | ||||||
| 		addrs = []string{"127.0.0.1:11211"} |  | ||||||
| 	} |  | ||||||
| 	return &mstore{ |  | ||||||
| 		Client: mc.New(addrs...), |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -1,43 +0,0 @@ | |||||||
| package memory |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"errors" |  | ||||||
| 	"sync" |  | ||||||
|  |  | ||||||
| 	"github.com/myodc/go-micro/store" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| type mstore struct { |  | ||||||
| 	sync.RWMutex |  | ||||||
| 	store map[string]*store.Item |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Get(key string) (*store.Item, error) { |  | ||||||
| 	m.RLock() |  | ||||||
| 	v, ok := m.store[key] |  | ||||||
| 	m.RUnlock() |  | ||||||
| 	if !ok { |  | ||||||
| 		return nil, errors.New("key not found") |  | ||||||
| 	} |  | ||||||
| 	return v, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Del(key string) error { |  | ||||||
| 	m.Lock() |  | ||||||
| 	delete(m.store, key) |  | ||||||
| 	m.Unlock() |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (m *mstore) Put(item *store.Item) error { |  | ||||||
| 	m.Lock() |  | ||||||
| 	m.store[item.Key] = item |  | ||||||
| 	m.Unlock() |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func NewStore(addrs []string, opt ...store.Option) store.Store { |  | ||||||
| 	return &mstore{ |  | ||||||
| 		store: make(map[string]*store.Item), |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @@ -1,36 +0,0 @@ | |||||||
| package store |  | ||||||
|  |  | ||||||
| type Store interface { |  | ||||||
| 	Get(string) (*Item, error) |  | ||||||
| 	Del(string) error |  | ||||||
| 	Put(*Item) error |  | ||||||
| } |  | ||||||
|  |  | ||||||
| type Item struct { |  | ||||||
| 	Key   string |  | ||||||
| 	Value []byte |  | ||||||
| } |  | ||||||
|  |  | ||||||
| type options struct{} |  | ||||||
|  |  | ||||||
| type Option func(*options) |  | ||||||
|  |  | ||||||
| var ( |  | ||||||
| 	DefaultStore = newConsulStore([]string{}) |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| func NewStore(addrs []string, opt ...Option) Store { |  | ||||||
| 	return newConsulStore(addrs, opt...) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func Get(key string) (*Item, error) { |  | ||||||
| 	return DefaultStore.Get(key) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func Del(key string) error { |  | ||||||
| 	return DefaultStore.Del(key) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func Put(item *Item) error { |  | ||||||
| 	return DefaultStore.Put(item) |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user