mirror of
https://github.com/skidoodle/pastebin
synced 2025-10-14 09:44:48 +02:00
43 lines
818 B
Go
43 lines
818 B
Go
package store
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// MemoryStore is an in-memory implementation of the Store interface.
|
|
type MemoryStore struct {
|
|
data map[string]string
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewMemoryStore creates a new MemoryStore.
|
|
func NewMemoryStore() *MemoryStore {
|
|
return &MemoryStore{
|
|
data: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
// Get retrieves a value from the store.
|
|
func (s *MemoryStore) Get(key string) (string, bool, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
val, ok := s.data[key]
|
|
return val, ok, nil
|
|
}
|
|
|
|
// Set adds a value to the store.
|
|
func (s *MemoryStore) Set(key, value string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.data[key] = value
|
|
return nil
|
|
}
|
|
|
|
// Del removes a value from the store.
|
|
func (s *MemoryStore) Del(key string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
delete(s.data, key)
|
|
return nil
|
|
}
|