chore: remove resource cache (#1059)

pull/1060/head
boojack 2 years ago committed by GitHub
parent a004dcf320
commit f74d1b7bf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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)
}

@ -7,13 +7,11 @@ import (
"fmt" "fmt"
"github.com/VictoriaMetrics/fastcache" "github.com/VictoriaMetrics/fastcache"
"github.com/usememos/memos/api"
) )
var ( var (
// 64 MiB. // 64 MiB.
cacheSize = 1024 * 1024 * 64 cacheSize = 1024 * 1024 * 64
_ api.CacheService = (*CacheService)(nil)
) )
// CacheService implements a cache. // CacheService implements a cache.
@ -21,6 +19,18 @@ type CacheService struct {
cache *fastcache.Cache 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. // NewCacheService creates a cache service.
func NewCacheService() *CacheService { func NewCacheService() *CacheService {
return &CacheService{ return &CacheService{
@ -29,7 +39,7 @@ func NewCacheService() *CacheService {
} }
// FindCache finds the value in cache. // 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} buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf1, uint64(id)) 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. // 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} buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf1, uint64(id)) 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. // 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} buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf1, uint64(id)) binary.LittleEndian.PutUint64(buf1, uint64(id))

@ -102,7 +102,7 @@ func (s *Store) CreateMemo(ctx context.Context, create *api.MemoCreate) (*api.Me
return nil, FormatError(err) 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 return nil, err
} }
@ -130,7 +130,7 @@ func (s *Store) PatchMemo(ctx context.Context, patch *api.MemoPatch) (*api.Memo,
return nil, FormatError(err) 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 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) { func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, error) {
if find.ID != nil { if find.ID != nil {
memoRaw := &memoRaw{} memoRaw := &memoRaw{}
has, err := s.cache.FindCache(api.MemoCache, *find.ID, memoRaw) has, err := s.cache.FindCache(MemoCache, *find.ID, memoRaw)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -199,7 +199,7 @@ func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, er
} }
memoRaw := list[0] 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 return nil, err
} }
@ -229,7 +229,7 @@ func (s *Store) DeleteMemo(ctx context.Context, delete *api.MemoDelete) error {
return FormatError(err) return FormatError(err)
} }
s.cache.DeleteCache(api.MemoCache, delete.ID) s.cache.DeleteCache(MemoCache, delete.ID)
return nil return nil
} }

@ -97,10 +97,6 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate)
return nil, FormatError(err) return nil, FormatError(err)
} }
if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
return nil, err
}
resource := resourceRaw.toResource() resource := resourceRaw.toResource()
return resource, nil 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) { 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) tx, err := s.db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
@ -154,11 +139,6 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.
} }
resourceRaw := list[0] resourceRaw := list[0]
if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
return nil, err
}
resource := resourceRaw.toResource() resource := resourceRaw.toResource()
return resource, nil return resource, nil
@ -182,8 +162,6 @@ func (s *Store) DeleteResource(ctx context.Context, delete *api.ResourceDelete)
return FormatError(err) return FormatError(err)
} }
s.cache.DeleteCache(api.ResourceCache, delete.ID)
return nil return nil
} }
@ -203,10 +181,6 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a
return nil, FormatError(err) return nil, FormatError(err)
} }
if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
return nil, err
}
resource := resourceRaw.toResource() resource := resourceRaw.toResource()
return resource, nil return resource, nil

@ -56,7 +56,7 @@ func (s *Store) CreateShortcut(ctx context.Context, create *api.ShortcutCreate)
return nil, FormatError(err) 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 return nil, err
} }
@ -81,7 +81,7 @@ func (s *Store) PatchShortcut(ctx context.Context, patch *api.ShortcutPatch) (*a
return nil, FormatError(err) 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 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) { func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api.Shortcut, error) {
if find.ID != nil { if find.ID != nil {
shortcutRaw := &shortcutRaw{} shortcutRaw := &shortcutRaw{}
has, err := s.cache.FindCache(api.ShortcutCache, *find.ID, shortcutRaw) has, err := s.cache.FindCache(ShortcutCache, *find.ID, shortcutRaw)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -139,7 +139,7 @@ func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api.
shortcutRaw := list[0] 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 return nil, err
} }
@ -164,7 +164,7 @@ func (s *Store) DeleteShortcut(ctx context.Context, delete *api.ShortcutDelete)
return FormatError(err) return FormatError(err)
} }
s.cache.DeleteCache(api.ShortcutCache, *delete.ID) s.cache.DeleteCache(ShortcutCache, *delete.ID)
return nil return nil
} }

@ -4,7 +4,6 @@ import (
"context" "context"
"database/sql" "database/sql"
"github.com/usememos/memos/api"
"github.com/usememos/memos/server/profile" "github.com/usememos/memos/server/profile"
) )
@ -12,7 +11,7 @@ import (
type Store struct { type Store struct {
db *sql.DB db *sql.DB
profile *profile.Profile profile *profile.Profile
cache api.CacheService cache *CacheService
} }
// New creates a new instance of Store. // New creates a new instance of Store.

@ -78,7 +78,7 @@ func (s *Store) CreateUser(ctx context.Context, create *api.UserCreate) (*api.Us
return nil, FormatError(err) 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 return nil, err
} }
@ -103,7 +103,7 @@ func (s *Store) PatchUser(ctx context.Context, patch *api.UserPatch) (*api.User,
return nil, FormatError(err) 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 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) { func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, error) {
if find.ID != nil { if find.ID != nil {
userRaw := &userRaw{} userRaw := &userRaw{}
has, err := s.cache.FindCache(api.UserCache, *find.ID, userRaw) has, err := s.cache.FindCache(UserCache, *find.ID, userRaw)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -161,7 +161,7 @@ func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, er
userRaw := list[0] 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 return nil, err
} }
@ -188,7 +188,7 @@ func (s *Store) DeleteUser(ctx context.Context, delete *api.UserDelete) error {
return err return err
} }
s.cache.DeleteCache(api.UserCache, delete.ID) s.cache.DeleteCache(UserCache, delete.ID)
return nil return nil
} }

Loading…
Cancel
Save