2015-05-23 22:04:16 +03:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/myodc/go-micro/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mstore struct {
|
|
|
|
sync.RWMutex
|
2015-05-23 23:25:55 +03:00
|
|
|
store map[string]*store.Item
|
2015-05-23 22:04:16 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 23:25:55 +03:00
|
|
|
func (m *mstore) Get(key string) (*store.Item, error) {
|
2015-05-23 22:04:16 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:25:55 +03:00
|
|
|
func (m *mstore) Put(item *store.Item) error {
|
2015-05-23 22:04:16 +03:00
|
|
|
m.Lock()
|
2015-05-23 23:25:55 +03:00
|
|
|
m.store[item.Key] = item
|
2015-05-23 22:04:16 +03:00
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStore(addrs []string, opt ...store.Option) store.Store {
|
|
|
|
return &mstore{
|
2015-05-23 23:25:55 +03:00
|
|
|
store: make(map[string]*store.Item),
|
2015-05-23 22:04:16 +03:00
|
|
|
}
|
|
|
|
}
|