Skip to content

Commit

Permalink
memory store added
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Oct 25, 2024
1 parent 366e941 commit d8ae148
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gobist

import "sync"

type Store interface {
Set(key string, value any) error
Get(key string) (any, error)
Delete(key string) error
}

type memoryStore struct {
mx sync.Mutex
data map[string]any
}

func newMemoryStore() Store {
return &memoryStore{data: make(map[string]any)}
}

func (m *memoryStore) Set(key string, value any) error {
m.mx.Lock()
m.data[key] = value
m.mx.Unlock()

return nil
}

func (m *memoryStore) Get(key string) (any, error) {
m.mx.Lock()
defer m.mx.Unlock()

return m.data[key], nil
}

func (m *memoryStore) Delete(key string) error {
m.mx.Lock()
delete(m.data, key)
m.mx.Unlock()

return nil
}

0 comments on commit d8ae148

Please sign in to comment.