-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemory_store.go
More file actions
53 lines (43 loc) · 1.09 KB
/
memory_store.go
File metadata and controls
53 lines (43 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package barong
import (
"context"
"sync"
"time"
)
// MemoryStore implements the Store interface using an in-memory map
// This is primarily intended for testing purposes
type MemoryStore struct {
data map[string]string
mu sync.RWMutex
}
// NewMemoryStore creates a new MemoryStore
func NewMemoryStore(ctx context.Context) *MemoryStore {
return &MemoryStore{
data: make(map[string]string),
}
}
// Set stores a key with a value
// For MemoryStore, we ignore the TTL parameter as noted in the spec
func (s *MemoryStore) Set(ctx context.Context, key, value string, ttl time.Duration) error {
s.mu.Lock()
defer s.mu.Unlock()
s.data[key] = value
return nil
}
// Get retrieves a value by key
func (s *MemoryStore) Get(ctx context.Context, key string) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
value, ok := s.data[key]
if !ok {
return "", ErrInvalidToken
}
return value, nil
}
// Clear removes all data from the store
// This is useful for testing to reset the store between tests
func (s *MemoryStore) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.data = make(map[string]string)
}