feat: add store cache for idp (#1116)

feat: add cache for idp
This commit is contained in:
boojack 2023-02-18 18:41:52 +08:00 committed by GitHub
parent 852903bdbd
commit cbbd284e7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -102,7 +102,10 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProv
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
return create, nil
identityProviderMessage := create
s.idpCache.Store(identityProviderMessage.ID, identityProviderMessage)
return identityProviderMessage, nil
}
func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityProviderMessage) ([]*IdentityProviderMessage, error) {
@ -117,10 +120,19 @@ func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityPro
return nil, err
}
for _, item := range list {
s.idpCache.Store(item.ID, item)
}
return list, nil
}
func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProviderMessage) (*IdentityProviderMessage, error) {
if find.ID != nil {
if cache, ok := s.idpCache.Load(*find.ID); ok {
return cache.(*IdentityProviderMessage), nil
}
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@ -135,7 +147,9 @@ func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProvi
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
}
return list[0], nil
identityProviderMessage := list[0]
s.idpCache.Store(identityProviderMessage.ID, identityProviderMessage)
return identityProviderMessage, nil
}
func (s *Store) UpdateIdentityProvider(ctx context.Context, update *UpdateIdentityProviderMessage) (*IdentityProviderMessage, error) {
@ -195,6 +209,7 @@ func (s *Store) UpdateIdentityProvider(ctx context.Context, update *UpdateIdenti
return nil, fmt.Errorf("unsupported idp type %s", string(identityProviderMessage.Type))
}
s.idpCache.Store(identityProviderMessage.ID, identityProviderMessage)
return &identityProviderMessage, nil
}
@ -219,6 +234,7 @@ func (s *Store) DeleteIdentityProvider(ctx context.Context, delete *DeleteIdenti
if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("idp not found")}
}
s.idpCache.Delete(delete.ID)
return nil
}

View file

@ -17,6 +17,7 @@ type Store struct {
userSettingCache sync.Map // map[string]*userSettingRaw
memoCache sync.Map // map[int]*memoRaw
shortcutCache sync.Map // map[int]*shortcutRaw
idpCache sync.Map // map[int]*identityProviderMessage
}
// New creates a new instance of Store.