From cbbd284e7af52860921717408a9ae6aae9dfa942 Mon Sep 17 00:00:00 2001 From: boojack Date: Sat, 18 Feb 2023 18:41:52 +0800 Subject: [PATCH] feat: add store cache for idp (#1116) feat: add cache for idp --- store/idp.go | 20 ++++++++++++++++++-- store/store.go | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/store/idp.go b/store/idp.go index 0d6905d6..04d66182 100644 --- a/store/idp.go +++ b/store/idp.go @@ -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 } diff --git a/store/store.go b/store/store.go index f4a3ae02..50f3afc3 100644 --- a/store/store.go +++ b/store/store.go @@ -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.