feat: add store cache

This commit is contained in:
Steven 2023-06-21 08:46:52 +08:00
parent 01ed504735
commit 0ca88fac00
4 changed files with 35 additions and 8 deletions

View File

@ -140,7 +140,7 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh
id = ? id = ?
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, description, visibility RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, description, visibility
` `
var shortcut Shortcut var shortcut *Shortcut
if err := tx.QueryRowContext(ctx, query, args...).Scan( if err := tx.QueryRowContext(ctx, query, args...).Scan(
&shortcut.ID, &shortcut.ID,
&shortcut.CreatorID, &shortcut.CreatorID,
@ -159,7 +159,8 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh
return nil, err return nil, err
} }
return &shortcut, nil s.shortcutCache.Store(shortcut.ID, shortcut)
return shortcut, nil
} }
func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Shortcut, error) { func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Shortcut, error) {
@ -169,15 +170,24 @@ func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Short
} }
defer tx.Rollback() defer tx.Rollback()
shortcuts, err := listShortcuts(ctx, tx, find) list, err := listShortcuts(ctx, tx, find)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return shortcuts, nil for _, shortcut := range list {
s.shortcutCache.Store(shortcut.ID, shortcut)
}
return list, nil
} }
func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut, error) { func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut, error) {
if find.ID != nil {
if cache, ok := s.shortcutCache.Load(*find.ID); ok {
return cache.(*Shortcut), nil
}
}
tx, err := s.db.BeginTx(ctx, nil) tx, err := s.db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -192,7 +202,10 @@ func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut,
if len(shortcuts) == 0 { if len(shortcuts) == 0 {
return nil, nil return nil, nil
} }
return shortcuts[0], nil
shortcut := shortcuts[0]
s.shortcutCache.Store(shortcut.ID, shortcut)
return shortcut, nil
} }
func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) error { func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) error {
@ -211,6 +224,7 @@ func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) erro
return err return err
} }
s.shortcutCache.Delete(delete.ID)
return nil return nil
} }

View File

@ -12,8 +12,9 @@ type Store struct {
db *sql.DB db *sql.DB
profile *profile.Profile profile *profile.Profile
userCache sync.Map // map[int]*userRaw workspaceSettingCache sync.Map // map[string]*WorkspaceSetting
workspaceCache sync.Map // map[int]*workspaceRaw userCache sync.Map // map[int]*User
shortcutCache sync.Map // map[int]*Shortcut
} }
// New creates a new instance of Store. // New creates a new instance of Store.

View File

@ -181,7 +181,7 @@ func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error)
func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) { func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
if find.ID != nil { if find.ID != nil {
if cache, ok := s.workspaceCache.Load(*find.ID); ok { if cache, ok := s.userCache.Load(*find.ID); ok {
return cache.(*User), nil return cache.(*User), nil
} }
} }

View File

@ -40,6 +40,7 @@ func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSet
} }
workspaceSetting := upsert workspaceSetting := upsert
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
return workspaceSetting, nil return workspaceSetting, nil
} }
@ -55,10 +56,20 @@ func (s *Store) ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSe
return nil, err return nil, err
} }
for _, workspaceSetting := range list {
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
}
return list, nil return list, nil
} }
func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) { func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) {
if find.Key != "" {
if cache, ok := s.workspaceSettingCache.Load(find.Key); ok {
return cache.(*WorkspaceSetting), nil
}
}
tx, err := s.db.BeginTx(ctx, nil) tx, err := s.db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -75,6 +86,7 @@ func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSett
} }
workspaceSetting := list[0] workspaceSetting := list[0]
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
return workspaceSetting, nil return workspaceSetting, nil
} }