1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

In memory store. Should have been here from the beginning.

This commit is contained in:
Asim 2015-03-06 19:08:55 +00:00
parent 87e2edbeb5
commit 5372f3a009
3 changed files with 64 additions and 0 deletions

View File

@ -33,6 +33,8 @@ func Init() {
switch flagStore {
case "memcached":
store.DefaultStore = store.NewMemcacheStore()
case "memory":
store.DefaultStore = store.NewMemoryStore()
case "etcd":
store.DefaultStore = store.NewEtcdStore()
}

14
store/memory_item.go Normal file
View File

@ -0,0 +1,14 @@
package store
type MemoryItem struct {
key string
value []byte
}
func (m *MemoryItem) Key() string {
return m.key
}
func (m *MemoryItem) Value() []byte {
return m.value
}

48
store/memory_store.go Normal file
View File

@ -0,0 +1,48 @@
package store
import (
"errors"
"sync"
)
type MemoryStore struct {
sync.RWMutex
store map[string]Item
}
func (m *MemoryStore) Get(key string) (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 *MemoryStore) Del(key string) error {
m.Lock()
delete(m.store, key)
m.Unlock()
return nil
}
func (m *MemoryStore) Put(item Item) error {
m.Lock()
m.store[item.Key()] = item
m.Unlock()
return nil
}
func (m *MemoryStore) NewItem(key string, value []byte) Item {
return &MemoryItem{
key: key,
value: value,
}
}
func NewMemoryStore() Store {
return &MemoryStore{
store: make(map[string]Item),
}
}