mirror of
https://github.com/skidoodle/pastebin
synced 2025-10-14 09:44:48 +02:00
init
This commit is contained in:
36
store/memory.go
Normal file
36
store/memory.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package store
|
||||
|
||||
import "sync"
|
||||
|
||||
type MemoryStore struct {
|
||||
data map[string]string
|
||||
mx sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
data: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(key string) (string, bool) {
|
||||
s.mx.RLock()
|
||||
defer s.mx.RUnlock()
|
||||
|
||||
content, exists := s.data[key]
|
||||
return content, exists
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Set(key string, value string) {
|
||||
s.mx.Lock()
|
||||
defer s.mx.Unlock()
|
||||
|
||||
s.data[key] = value
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Del(key string) {
|
||||
s.mx.Lock()
|
||||
defer s.mx.Unlock()
|
||||
|
||||
delete(s.data, key)
|
||||
}
|
||||
44
store/memory_test.go
Normal file
44
store/memory_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package store
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMemoryStoreGet(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
|
||||
store.Set("key", "value")
|
||||
if val, _ := store.Get("key"); val != "value" {
|
||||
t.Errorf("get() = %s, want %s", val, "value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryStoreExists(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
|
||||
if _, exists := store.Get("something"); exists {
|
||||
t.Errorf("get() = %t, want %t", exists, false)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryStoreOverride(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
|
||||
store.Set("key", "value")
|
||||
store.Set("key", "new_value")
|
||||
if val, _ := store.Get("key"); val != "new_value" {
|
||||
t.Errorf("get() = %s, want %s", val, "new_value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryStoreDelete(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
|
||||
store.Set("key", "value")
|
||||
if _, exists := store.Get("key"); !exists {
|
||||
t.Errorf("get() = %t, want %t", exists, true)
|
||||
}
|
||||
|
||||
store.Del("key")
|
||||
if val, _ := store.Get("key"); val != "" {
|
||||
t.Errorf("del() = %s, want %s", val, "")
|
||||
}
|
||||
}
|
||||
7
store/store.go
Normal file
7
store/store.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package store
|
||||
|
||||
type store interface {
|
||||
Get(key string) (string, bool)
|
||||
Set(key string, value string)
|
||||
Del(key string)
|
||||
}
|
||||
Reference in New Issue
Block a user