@@ -3,57 +3,49 @@ package sp
33import (
44 "fmt"
55 "log/slog"
6- "sync "
6+ "time "
77
88 "github.com/crewjam/saml/samlsp"
9+ "github.com/karlseguin/ccache/v3"
910)
1011
1112type MemoryAttributeStore struct {
12- store map [ string ]samlsp. Attributes
13- mu sync. RWMutex
13+ ttl time. Duration
14+ store * ccache. Cache [samlsp. Attributes ]
1415}
1516
16- func NewMemoryAttributeStore () (* MemoryAttributeStore , error ) {
17+ func NewMemoryAttributeStore (ttl time. Duration ) (* MemoryAttributeStore , error ) {
1718 return & MemoryAttributeStore {
18- store : make ( map [ string ] samlsp.Attributes ),
19+ store : ccache . New ( ccache . Configure [ samlsp.Attributes ]() ),
1920 }, nil
2021}
2122
2223func (s * MemoryAttributeStore ) Get (id string ) (samlsp.Attributes , error ) {
23- s . mu . RLock ()
24- defer s . mu . RUnlock ( )
24+ if item := s . store . Get ( id ); item != nil {
25+ slog . Debug ( "getting attributes from store" , "id" , id , "attrs" , item . Value () )
2526
26- if attrs , found := s .store [id ]; found {
27- slog .Debug ("getting attributes from store" , "id" , id , "attrs" , attrs )
28-
29- return attrs , nil
27+ return item .Value (), nil
3028 }
3129
3230 return nil , fmt .Errorf ("not found" )
3331}
3432
3533func (s * MemoryAttributeStore ) Set (id string , attrs samlsp.Attributes ) {
36- s .mu .Lock ()
37- defer s .mu .Unlock ()
38-
3934 if s .store == nil {
40- s .store = make ( map [ string ] samlsp.Attributes )
35+ s .store = ccache . New ( ccache . Configure [ samlsp.Attributes ]() )
4136 }
4237
4338 slog .Debug ("setting attributes in store" , "id" , id , "attrs" , attrs )
4439
45- s .store [ id ] = attrs
40+ s .store . Set ( id , attrs , s . ttl )
4641}
4742
4843func (s * MemoryAttributeStore ) Delete (id string ) {
49- s .mu .Lock ()
50- defer s .mu .Unlock ()
51-
5244 if s .store == nil {
5345 return
5446 }
5547
5648 slog .Debug ("deleting attributes in store" , "id" , id )
5749
58- delete ( s .store , id )
50+ s .store . Delete ( id )
5951}
0 commit comments