micro/store/memcached/memcached.go

51 lines
890 B
Go
Raw Normal View History

package memcached
2015-01-13 23:31:27 +00:00
import (
"errors"
mc "github.com/bradfitz/gomemcache/memcache"
"github.com/myodc/go-micro/store"
2015-01-13 23:31:27 +00:00
)
type mstore struct {
2015-01-13 23:31:27 +00:00
Client *mc.Client
}
func (m *mstore) Get(key string) (*store.Item, error) {
2015-01-13 23:31:27 +00:00
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,
2015-01-13 23:31:27 +00:00
}, nil
}
func (m *mstore) Del(key string) error {
2015-01-13 23:31:27 +00:00
return m.Client.Delete(key)
}
func (m *mstore) Put(item *store.Item) error {
2015-01-13 23:31:27 +00:00
return m.Client.Set(&mc.Item{
Key: item.Key,
Value: item.Value,
2015-01-13 23:31:27 +00:00
})
}
func NewStore(addrs []string, opts ...store.Option) store.Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:11211"}
2015-01-13 23:31:27 +00:00
}
return &mstore{
Client: mc.New(addrs...),
2015-01-13 23:31:27 +00:00
}
}