github.com/crewjam/saml@v0.4.14/samlidp/memory_store.go (about) 1 package samlidp 2 3 import ( 4 "encoding/json" 5 "strings" 6 "sync" 7 ) 8 9 // MemoryStore is an implementation of Store that resides completely 10 // in memory. 11 type MemoryStore struct { 12 mu sync.RWMutex 13 data map[string]string 14 } 15 16 // Get fetches the data stored in `key` and unmarshals it into `value`. 17 func (s *MemoryStore) Get(key string, value interface{}) error { 18 s.mu.RLock() 19 defer s.mu.RUnlock() 20 21 v, ok := s.data[key] 22 if !ok { 23 return ErrNotFound 24 } 25 return json.Unmarshal([]byte(v), value) 26 } 27 28 // Put marshals `value` and stores it in `key`. 29 func (s *MemoryStore) Put(key string, value interface{}) error { 30 s.mu.Lock() 31 defer s.mu.Unlock() 32 if s.data == nil { 33 s.data = map[string]string{} 34 } 35 36 buf, err := json.Marshal(value) 37 if err != nil { 38 return err 39 } 40 s.data[key] = string(buf) 41 return nil 42 } 43 44 // Delete removes `key` 45 func (s *MemoryStore) Delete(key string) error { 46 s.mu.Lock() 47 defer s.mu.Unlock() 48 delete(s.data, key) 49 return nil 50 } 51 52 // List returns all the keys that start with `prefix`. The prefix is 53 // stripped from each returned value. So if keys are ["aa", "ab", "cd"] 54 // then List("a") would produce []string{"a", "b"} 55 func (s *MemoryStore) List(prefix string) ([]string, error) { 56 rv := []string{} 57 for k := range s.data { 58 if strings.HasPrefix(k, prefix) { 59 rv = append(rv, strings.TrimPrefix(k, prefix)) 60 } 61 } 62 return rv, nil 63 }