forked from micro/go-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In memory store. Should have been here from the beginning.
- Loading branch information
Asim
committed
Mar 6, 2015
1 parent
87e2edb
commit 5372f3a
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |