From f74d1b7bf8af2d3c852b6ed4d534881dc86ca9ca Mon Sep 17 00:00:00 2001 From: boojack Date: Fri, 10 Feb 2023 08:43:39 +0800 Subject: [PATCH] chore: remove resource cache (#1059) --- api/cache.go | 22 ---------------------- store/cache.go | 22 ++++++++++++++++------ store/memo.go | 10 +++++----- store/resource.go | 26 -------------------------- store/shortcut.go | 10 +++++----- store/store.go | 3 +-- store/user.go | 10 +++++----- 7 files changed, 32 insertions(+), 71 deletions(-) delete mode 100644 api/cache.go diff --git a/api/cache.go b/api/cache.go deleted file mode 100644 index 302b5522..00000000 --- a/api/cache.go +++ /dev/null @@ -1,22 +0,0 @@ -package api - -// CacheNamespace is the type of a cache. -type CacheNamespace string - -const ( - // UserCache is the cache type of users. - UserCache CacheNamespace = "u" - // MemoCache is the cache type of memos. - MemoCache CacheNamespace = "m" - // ShortcutCache is the cache type of shortcuts. - ShortcutCache CacheNamespace = "s" - // ResourceCache is the cache type of resources. - ResourceCache CacheNamespace = "r" -) - -// CacheService is the service for caches. -type CacheService interface { - FindCache(namespace CacheNamespace, id int, entry interface{}) (bool, error) - UpsertCache(namespace CacheNamespace, id int, entry interface{}) error - DeleteCache(namespace CacheNamespace, id int) -} diff --git a/store/cache.go b/store/cache.go index e511cbbb..efcf5d6f 100644 --- a/store/cache.go +++ b/store/cache.go @@ -7,13 +7,11 @@ import ( "fmt" "github.com/VictoriaMetrics/fastcache" - "github.com/usememos/memos/api" ) var ( // 64 MiB. - cacheSize = 1024 * 1024 * 64 - _ api.CacheService = (*CacheService)(nil) + cacheSize = 1024 * 1024 * 64 ) // CacheService implements a cache. @@ -21,6 +19,18 @@ type CacheService struct { cache *fastcache.Cache } +// CacheNamespace is the type of a cache. +type CacheNamespace string + +const ( + // UserCache is the cache type of users. + UserCache CacheNamespace = "u" + // MemoCache is the cache type of memos. + MemoCache CacheNamespace = "m" + // ShortcutCache is the cache type of shortcuts. + ShortcutCache CacheNamespace = "s" +) + // NewCacheService creates a cache service. func NewCacheService() *CacheService { return &CacheService{ @@ -29,7 +39,7 @@ func NewCacheService() *CacheService { } // FindCache finds the value in cache. -func (s *CacheService) FindCache(namespace api.CacheNamespace, id int, entry interface{}) (bool, error) { +func (s *CacheService) FindCache(namespace CacheNamespace, id int, entry interface{}) (bool, error) { buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0} binary.LittleEndian.PutUint64(buf1, uint64(id)) @@ -46,7 +56,7 @@ func (s *CacheService) FindCache(namespace api.CacheNamespace, id int, entry int } // UpsertCache upserts the value to cache. -func (s *CacheService) UpsertCache(namespace api.CacheNamespace, id int, entry interface{}) error { +func (s *CacheService) UpsertCache(namespace CacheNamespace, id int, entry interface{}) error { buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0} binary.LittleEndian.PutUint64(buf1, uint64(id)) @@ -61,7 +71,7 @@ func (s *CacheService) UpsertCache(namespace api.CacheNamespace, id int, entry i } // DeleteCache deletes the cache. -func (s *CacheService) DeleteCache(namespace api.CacheNamespace, id int) { +func (s *CacheService) DeleteCache(namespace CacheNamespace, id int) { buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0} binary.LittleEndian.PutUint64(buf1, uint64(id)) diff --git a/store/memo.go b/store/memo.go index 1e44566b..4b70d14e 100644 --- a/store/memo.go +++ b/store/memo.go @@ -102,7 +102,7 @@ func (s *Store) CreateMemo(ctx context.Context, create *api.MemoCreate) (*api.Me return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { + if err := s.cache.UpsertCache(MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } @@ -130,7 +130,7 @@ func (s *Store) PatchMemo(ctx context.Context, patch *api.MemoPatch) (*api.Memo, return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { + if err := s.cache.UpsertCache(MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } @@ -170,7 +170,7 @@ 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 { memoRaw := &memoRaw{} - has, err := s.cache.FindCache(api.MemoCache, *find.ID, memoRaw) + has, err := s.cache.FindCache(MemoCache, *find.ID, memoRaw) if err != nil { return nil, err } @@ -199,7 +199,7 @@ func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, er } memoRaw := list[0] - if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil { + if err := s.cache.UpsertCache(MemoCache, memoRaw.ID, memoRaw); err != nil { return nil, err } @@ -229,7 +229,7 @@ func (s *Store) DeleteMemo(ctx context.Context, delete *api.MemoDelete) error { return FormatError(err) } - s.cache.DeleteCache(api.MemoCache, delete.ID) + s.cache.DeleteCache(MemoCache, delete.ID) return nil } diff --git a/store/resource.go b/store/resource.go index 943b2957..39986c29 100644 --- a/store/resource.go +++ b/store/resource.go @@ -97,10 +97,6 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil { - return nil, err - } - resource := resourceRaw.toResource() return resource, nil @@ -127,17 +123,6 @@ 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 { - resourceRaw := &resourceRaw{} - has, err := s.cache.FindCache(api.ResourceCache, *find.ID, resourceRaw) - if err != nil { - return nil, err - } - if has { - return resourceRaw.toResource(), nil - } - } - tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, FormatError(err) @@ -154,11 +139,6 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api. } resourceRaw := list[0] - - if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil { - return nil, err - } - resource := resourceRaw.toResource() return resource, nil @@ -182,8 +162,6 @@ func (s *Store) DeleteResource(ctx context.Context, delete *api.ResourceDelete) return FormatError(err) } - s.cache.DeleteCache(api.ResourceCache, delete.ID) - return nil } @@ -203,10 +181,6 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a return nil, FormatError(err) } - 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 b56d2efe..7a81d17b 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -56,7 +56,7 @@ func (s *Store) CreateShortcut(ctx context.Context, create *api.ShortcutCreate) return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { + if err := s.cache.UpsertCache(ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } @@ -81,7 +81,7 @@ func (s *Store) PatchShortcut(ctx context.Context, patch *api.ShortcutPatch) (*a return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { + if err := s.cache.UpsertCache(ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } @@ -113,7 +113,7 @@ 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 { shortcutRaw := &shortcutRaw{} - has, err := s.cache.FindCache(api.ShortcutCache, *find.ID, shortcutRaw) + has, err := s.cache.FindCache(ShortcutCache, *find.ID, shortcutRaw) if err != nil { return nil, err } @@ -139,7 +139,7 @@ func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api. shortcutRaw := list[0] - if err := s.cache.UpsertCache(api.ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { + if err := s.cache.UpsertCache(ShortcutCache, shortcutRaw.ID, shortcutRaw); err != nil { return nil, err } @@ -164,7 +164,7 @@ func (s *Store) DeleteShortcut(ctx context.Context, delete *api.ShortcutDelete) return FormatError(err) } - s.cache.DeleteCache(api.ShortcutCache, *delete.ID) + s.cache.DeleteCache(ShortcutCache, *delete.ID) return nil } diff --git a/store/store.go b/store/store.go index 1f80736b..8d9b3e0b 100644 --- a/store/store.go +++ b/store/store.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" - "github.com/usememos/memos/api" "github.com/usememos/memos/server/profile" ) @@ -12,7 +11,7 @@ import ( type Store struct { db *sql.DB profile *profile.Profile - cache api.CacheService + cache *CacheService } // New creates a new instance of Store. diff --git a/store/user.go b/store/user.go index 5d142d48..0bf3288d 100644 --- a/store/user.go +++ b/store/user.go @@ -78,7 +78,7 @@ func (s *Store) CreateUser(ctx context.Context, create *api.UserCreate) (*api.Us return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { + if err := s.cache.UpsertCache(UserCache, userRaw.ID, userRaw); err != nil { return nil, err } @@ -103,7 +103,7 @@ func (s *Store) PatchUser(ctx context.Context, patch *api.UserPatch) (*api.User, return nil, FormatError(err) } - if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { + if err := s.cache.UpsertCache(UserCache, userRaw.ID, userRaw); err != nil { return nil, err } @@ -135,7 +135,7 @@ 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 { userRaw := &userRaw{} - has, err := s.cache.FindCache(api.UserCache, *find.ID, userRaw) + has, err := s.cache.FindCache(UserCache, *find.ID, userRaw) if err != nil { return nil, err } @@ -161,7 +161,7 @@ func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, er userRaw := list[0] - if err := s.cache.UpsertCache(api.UserCache, userRaw.ID, userRaw); err != nil { + if err := s.cache.UpsertCache(UserCache, userRaw.ID, userRaw); err != nil { return nil, err } @@ -188,7 +188,7 @@ func (s *Store) DeleteUser(ctx context.Context, delete *api.UserDelete) error { return err } - s.cache.DeleteCache(api.UserCache, delete.ID) + s.cache.DeleteCache(UserCache, delete.ID) return nil }