forked from crewjam/saml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_store.go
63 lines (55 loc) · 1.32 KB
/
memory_store.go
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
54
55
56
57
58
59
60
61
62
63
package samlidp
import (
"encoding/json"
"strings"
"sync"
)
// MemoryStore is an implementation of Store that resides completely
// in memory.
type MemoryStore struct {
mu sync.RWMutex
data map[string]string
}
// Get fetches the data stored in `key` and unmarshals it into `value`.
func (s *MemoryStore) Get(key string, value interface{}) error {
s.mu.RLock()
defer s.mu.RUnlock()
v, ok := s.data[key]
if !ok {
return ErrNotFound
}
return json.Unmarshal([]byte(v), value)
}
// Put marshals `value` and stores it in `key`.
func (s *MemoryStore) Put(key string, value interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.data == nil {
s.data = map[string]string{}
}
buf, err := json.Marshal(value)
if err != nil {
return err
}
s.data[key] = string(buf)
return nil
}
// Delete removes `key`
func (s *MemoryStore) Delete(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.data, key)
return nil
}
// List returns all the keys that start with `prefix`. The prefix is
// stripped from each returned value. So if keys are ["aa", "ab", "cd"]
// then List("a") would produce []string{"a", "b"}
func (s *MemoryStore) List(prefix string) ([]string, error) {
rv := []string{}
for k := range s.data {
if strings.HasPrefix(k, prefix) {
rv = append(rv, strings.TrimPrefix(k, prefix))
}
}
return rv, nil
}