From 9f8c0ce5676a85ed22fa458428dc329e91354285 Mon Sep 17 00:00:00 2001 From: steven Date: Sat, 1 Oct 2022 10:37:02 +0800 Subject: [PATCH] fix: raw data cache --- store/memo.go | 27 ++++++++++++++++----------- store/memo_resource.go | 25 ------------------------- store/resource.go | 31 +++++++++++++++++++++++-------- store/shortcut.go | 24 +++++++++++++----------- store/user.go | 25 ++++++++++++++----------- 5 files changed, 66 insertions(+), 66 deletions(-) diff --git a/store/memo.go b/store/memo.go index bf96ecea..9cca2938 100644 --- a/store/memo.go +++ b/store/memo.go @@ -83,12 +83,12 @@ func (s *Store) CreateMemo(ctx context.Context, create *api.MemoCreate) (*api.Me return nil, FormatError(err) } - memo, err := s.composeMemo(ctx, memoRaw) - if err != nil { + if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } - if err := s.cache.UpsertCache(api.MemoCache, memo.ID, memo); err != nil { + memo, err := s.composeMemo(ctx, memoRaw) + if err != nil { return nil, err } @@ -111,12 +111,12 @@ func (s *Store) PatchMemo(ctx context.Context, patch *api.MemoPatch) (*api.Memo, return nil, FormatError(err) } - memo, err := s.composeMemo(ctx, memoRaw) - if err != nil { + if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } - if err := s.cache.UpsertCache(api.MemoCache, memo.ID, memo); err != nil { + memo, err := s.composeMemo(ctx, memoRaw) + if err != nil { return nil, err } @@ -150,12 +150,16 @@ func (s *Store) FindMemoList(ctx context.Context, find *api.MemoFind) ([]*api.Me func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, error) { if find.ID != nil { - memo := &api.Memo{} - has, err := s.cache.FindCache(api.MemoCache, *find.ID, memo) + memoRaw := &memoRaw{} + has, err := s.cache.FindCache(api.MemoCache, *find.ID, memoRaw) if err != nil { return nil, err } if has { + memo, err := s.composeMemo(ctx, memoRaw) + if err != nil { + return nil, err + } return memo, nil } } @@ -175,12 +179,13 @@ func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, er return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")} } - memo, err := s.composeMemo(ctx, list[0]) - if err != nil { + memoRaw := list[0] + if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } - if err := s.cache.UpsertCache(api.MemoCache, memo.ID, memo); err != nil { + memo, err := s.composeMemo(ctx, memoRaw) + if err != nil { return nil, err } diff --git a/store/memo_resource.go b/store/memo_resource.go index 54729c7c..0ad13a49 100644 --- a/store/memo_resource.go +++ b/store/memo_resource.go @@ -28,31 +28,6 @@ func (raw *memoResourceRaw) toMemoResource() *api.MemoResource { } } -func (s *Store) ComposeMemoResourceList(ctx context.Context, memo *api.Memo) error { - memoResourceList, err := s.FindMemoResourceList(ctx, &api.MemoResourceFind{ - MemoID: &memo.ID, - }) - if err != nil { - return err - } - - resourceList := []*api.Resource{} - for _, memoResource := range memoResourceList { - resource, err := s.FindResource(ctx, &api.ResourceFind{ - ID: &memoResource.ResourceID, - }) - if err != nil { - return err - } - - resourceList = append(resourceList, resource) - } - - memo.ResourceList = resourceList - - return nil -} - func (s *Store) FindMemoResourceList(ctx context.Context, find *api.MemoResourceFind) ([]*api.MemoResource, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { diff --git a/store/resource.go b/store/resource.go index e10e2a83..ba90ce2d 100644 --- a/store/resource.go +++ b/store/resource.go @@ -44,6 +44,19 @@ func (raw *resourceRaw) toResource() *api.Resource { } } +func (s *Store) ComposeMemoResourceList(ctx context.Context, memo *api.Memo) error { + resourceList, err := s.FindResourceList(ctx, &api.ResourceFind{ + MemoID: &memo.ID, + }) + if err != nil { + return err + } + + memo.ResourceList = resourceList + + return nil +} + func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) (*api.Resource, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { @@ -60,12 +73,12 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) return nil, FormatError(err) } - resource := resourceRaw.toResource() - - if err := s.cache.UpsertCache(api.ResourceCache, resource.ID, resource); err != nil { + if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil { return nil, err } + resource := resourceRaw.toResource() + return resource, nil } @@ -91,13 +104,13 @@ func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.Resource, error) { if find.ID != nil { - resource := &api.Resource{} - has, err := s.cache.FindCache(api.ResourceCache, *find.ID, resource) + resourceRaw := &resourceRaw{} + has, err := s.cache.FindCache(api.ResourceCache, *find.ID, resourceRaw) if err != nil { return nil, err } if has { - return resource, nil + return resourceRaw.toResource(), nil } } @@ -116,12 +129,14 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api. return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")} } - resource := list[0].toResource() + resourceRaw := list[0] - if err := s.cache.UpsertCache(api.ResourceCache, resource.ID, resource); err != nil { + if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil { return nil, err } + resource := resourceRaw.toResource() + return resource, nil } diff --git a/store/shortcut.go b/store/shortcut.go index 2fae474a..9f3d1dda 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -56,12 +56,12 @@ func (s *Store) CreateShortcut(ctx context.Context, create *api.ShortcutCreate) return nil, FormatError(err) } - shortcut := shortcutRaw.toShortcut() - - if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil { + if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } + shortcut := shortcutRaw.toShortcut() + return shortcut, nil } @@ -81,12 +81,12 @@ func (s *Store) PatchShortcut(ctx context.Context, patch *api.ShortcutPatch) (*a return nil, FormatError(err) } - shortcut := shortcutRaw.toShortcut() - - if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil { + if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } + shortcut := shortcutRaw.toShortcut() + return shortcut, nil } @@ -112,13 +112,13 @@ func (s *Store) FindShortcutList(ctx context.Context, find *api.ShortcutFind) ([ func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api.Shortcut, error) { if find.ID != nil { - shortcut := &api.Shortcut{} - has, err := s.cache.FindCache(api.ShortcutCache, *find.ID, shortcut) + shortcutRaw := &shortcutRaw{} + has, err := s.cache.FindCache(api.ShortcutCache, *find.ID, shortcutRaw) if err != nil { return nil, err } if has { - return shortcut, nil + return shortcutRaw.toShortcut(), nil } } @@ -137,12 +137,14 @@ func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api. return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")} } - shortcut := list[0].toShortcut() + shortcutRaw := list[0] - if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil { + if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } + shortcut := shortcutRaw.toShortcut() + return shortcut, nil } diff --git a/store/user.go b/store/user.go index 1bff2586..03440455 100644 --- a/store/user.go +++ b/store/user.go @@ -51,6 +51,7 @@ func (s *Store) ComposeMemoCreator(ctx context.Context, memo *api.Memo) error { if err != nil { return err } + user.OpenID = "" user.UserSettingList = nil memo.Creator = user @@ -74,12 +75,12 @@ func (s *Store) CreateUser(ctx context.Context, create *api.UserCreate) (*api.Us return nil, FormatError(err) } - user := userRaw.toUser() - - if err := s.cache.UpsertCache(api.UserCache, user.ID, user); err != nil { + if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { return nil, err } + user := userRaw.toUser() + return user, nil } @@ -99,12 +100,12 @@ func (s *Store) PatchUser(ctx context.Context, patch *api.UserPatch) (*api.User, return nil, FormatError(err) } - user := userRaw.toUser() - - if err := s.cache.UpsertCache(api.UserCache, user.ID, user); err != nil { + if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { return nil, err } + user := userRaw.toUser() + return user, nil } @@ -130,13 +131,13 @@ func (s *Store) FindUserList(ctx context.Context, find *api.UserFind) ([]*api.Us func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, error) { if find.ID != nil { - user := &api.User{} - has, err := s.cache.FindCache(api.UserCache, *find.ID, user) + userRaw := &userRaw{} + has, err := s.cache.FindCache(api.UserCache, *find.ID, userRaw) if err != nil { return nil, err } if has { - return user, nil + return userRaw.toUser(), nil } } @@ -157,12 +158,14 @@ func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, er return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d users with filter %+v, expect 1", len(list), find)} } - user := list[0].toUser() + userRaw := list[0] - if err := s.cache.UpsertCache(api.UserCache, user.ID, user); err != nil { + if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { return nil, err } + user := userRaw.toUser() + return user, nil }