From 4603f414db9435611a57324c0f2394eaf14a185b Mon Sep 17 00:00:00 2001 From: boojack Date: Fri, 28 Apr 2023 00:02:54 +0800 Subject: [PATCH] chore: add system setting cache (#1609) --- store/store.go | 11 ++++++----- store/system_setting.go | 12 +++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/store/store.go b/store/store.go index b26f8bf8..1db667cb 100644 --- a/store/store.go +++ b/store/store.go @@ -13,11 +13,12 @@ type Store struct { db *sql.DB profile *profile.Profile - userCache sync.Map // map[int]*userRaw - userSettingCache sync.Map // map[string]*userSettingRaw - memoCache sync.Map // map[int]*memoRaw - shortcutCache sync.Map // map[int]*shortcutRaw - idpCache sync.Map // map[int]*identityProviderMessage + systemSettingCache sync.Map // map[string]*systemSettingRaw + userCache sync.Map // map[int]*userRaw + 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. diff --git a/store/system_setting.go b/store/system_setting.go index f3fced79..6cb428b9 100644 --- a/store/system_setting.go +++ b/store/system_setting.go @@ -41,7 +41,7 @@ func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *api.SystemSetti } systemSetting := systemSettingRaw.toSystemSetting() - + s.systemSettingCache.Store(systemSettingRaw.Name, systemSettingRaw) return systemSetting, nil } @@ -63,13 +63,17 @@ func (s *Store) FindSystemSettingList(ctx context.Context, find *api.SystemSetti list := []*api.SystemSetting{} for _, raw := range systemSettingRawList { + s.systemSettingCache.Store(raw.Name, raw) list = append(list, raw.toSystemSetting()) } - return list, nil } func (s *Store) FindSystemSetting(ctx context.Context, find *api.SystemSettingFind) (*api.SystemSetting, error) { + if systemSetting, ok := s.systemSettingCache.Load(find.Name); ok { + systemSettingRaw := systemSetting.(*systemSettingRaw) + return systemSettingRaw.toSystemSetting(), nil + } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, FormatError(err) @@ -85,7 +89,9 @@ func (s *Store) FindSystemSetting(ctx context.Context, find *api.SystemSettingFi return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")} } - return systemSettingRawList[0].toSystemSetting(), nil + systemSettingRaw := systemSettingRawList[0] + s.systemSettingCache.Store(systemSettingRaw.Name, systemSettingRaw) + return systemSettingRaw.toSystemSetting(), nil } func upsertSystemSetting(ctx context.Context, tx *sql.Tx, upsert *api.SystemSettingUpsert) (*systemSettingRaw, error) {