mirror of
https://github.com/usememos/memos.git
synced 2025-03-18 19:50:09 +01:00
chore: remove resource cache (#1059)
This commit is contained in:
parent
a004dcf320
commit
f74d1b7bf8
22
api/cache.go
22
api/cache.go
@ -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"
|
||||
|
||||
"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))
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user