Skip to content

Commit

Permalink
In memory store. Should have been here from the beginning.
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim committed Mar 6, 2015
1 parent 87e2edb commit 5372f3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
14 changes: 14 additions & 0 deletions store/memory_item.go
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
}
48 changes: 48 additions & 0 deletions store/memory_store.go
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),
}
}

0 comments on commit 5372f3a

Please sign in to comment.