diff --git a/store/shortcut.go b/store/shortcut.go index e97a8d9..54d56d7 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -140,7 +140,7 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh id = ? 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( &shortcut.ID, &shortcut.CreatorID, @@ -159,7 +159,8 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh 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) { @@ -169,15 +170,24 @@ func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Short } defer tx.Rollback() - shortcuts, err := listShortcuts(ctx, tx, find) + list, err := listShortcuts(ctx, tx, find) if err != nil { 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) { + if find.ID != nil { + if cache, ok := s.shortcutCache.Load(*find.ID); ok { + return cache.(*Shortcut), nil + } + } + tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err @@ -192,7 +202,10 @@ func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut, if len(shortcuts) == 0 { 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 { @@ -211,6 +224,7 @@ func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) erro return err } + s.shortcutCache.Delete(delete.ID) return nil } diff --git a/store/store.go b/store/store.go index 24fb9d8..1543444 100644 --- a/store/store.go +++ b/store/store.go @@ -12,8 +12,9 @@ type Store struct { db *sql.DB profile *profile.Profile - userCache sync.Map // map[int]*userRaw - workspaceCache sync.Map // map[int]*workspaceRaw + workspaceSettingCache sync.Map // map[string]*WorkspaceSetting + userCache sync.Map // map[int]*User + shortcutCache sync.Map // map[int]*Shortcut } // New creates a new instance of Store. diff --git a/store/user.go b/store/user.go index d843947..50cb4ed 100644 --- a/store/user.go +++ b/store/user.go @@ -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) { 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 } } diff --git a/store/workspace_setting.go b/store/workspace_setting.go index 2dd67e5..ec425cd 100644 --- a/store/workspace_setting.go +++ b/store/workspace_setting.go @@ -40,6 +40,7 @@ func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSet } workspaceSetting := upsert + s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting) return workspaceSetting, nil } @@ -55,10 +56,20 @@ func (s *Store) ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSe return nil, err } + for _, workspaceSetting := range list { + s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting) + } + return list, nil } 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) if err != nil { return nil, err @@ -75,6 +86,7 @@ func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSett } workspaceSetting := list[0] + s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting) return workspaceSetting, nil }