From 83371edd32474e6ed1033cbfbf589e253865a33f Mon Sep 17 00:00:00 2001 From: alimy Date: Thu, 30 Jun 2022 08:13:30 +0800 Subject: [PATCH] optimize post cache index service integrate logic --- internal/dao/{cache.go => cache_index.go} | 5 ++++ internal/dao/cache_index_none.go | 29 +++++++++++++++++++++++ internal/dao/cache_index_simple.go | 6 +++-- internal/dao/dao.go | 24 +++++++------------ internal/dao/post.go | 10 ++++---- internal/dao/post_index.go | 18 -------------- 6 files changed, 51 insertions(+), 41 deletions(-) rename internal/dao/{cache.go => cache_index.go} (88%) create mode 100644 internal/dao/cache_index_none.go diff --git a/internal/dao/cache.go b/internal/dao/cache_index.go similarity index 88% rename from internal/dao/cache.go rename to internal/dao/cache_index.go index 48a06c05..3ffc5cf1 100644 --- a/internal/dao/cache.go +++ b/internal/dao/cache_index.go @@ -12,6 +12,7 @@ import ( var ( _ core.CacheIndexService = (*simpleCacheIndexServant)(nil) _ core.CacheIndexService = (*bigCacheIndexServant)(nil) + _ core.CacheIndexService = (*noneCacheIndexServant)(nil) ) type postsEntry struct { @@ -38,3 +39,7 @@ type simpleCacheIndexServant struct { checkTick *time.Ticker expireIndexTick *time.Ticker } + +type noneCacheIndexServant struct { + getIndexPosts indexPostsFunc +} diff --git a/internal/dao/cache_index_none.go b/internal/dao/cache_index_none.go new file mode 100644 index 00000000..d0431c6e --- /dev/null +++ b/internal/dao/cache_index_none.go @@ -0,0 +1,29 @@ +package dao + +import ( + "github.com/Masterminds/semver/v3" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/model" +) + +func newNoneCacheIndexServant(getIndexPosts indexPostsFunc) *noneCacheIndexServant { + return &noneCacheIndexServant{ + getIndexPosts: getIndexPosts, + } +} + +func (s *noneCacheIndexServant) IndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) { + return s.getIndexPosts(user, offset, limit) +} + +func (s *noneCacheIndexServant) SendAction(act core.IndexActionT) { + // empty +} + +func (s *noneCacheIndexServant) Name() string { + return "NoneCacheIndex" +} + +func (s *noneCacheIndexServant) Version() *semver.Version { + return semver.MustParse("v0.1.0") +} diff --git a/internal/dao/cache_index_simple.go b/internal/dao/cache_index_simple.go index 456d760a..1d82a37e 100644 --- a/internal/dao/cache_index_simple.go +++ b/internal/dao/cache_index_simple.go @@ -49,7 +49,7 @@ func newSimpleCacheIndexServant(getIndexPosts indexPostsFunc) *simpleCacheIndexS return cacheIndex } -func (s *simpleCacheIndexServant) IndexPosts(_user *model.User, offset int, limit int) ([]*model.PostFormated, error) { +func (s *simpleCacheIndexServant) IndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) { posts := s.atomicIndex.Load().([]*model.PostFormated) end := offset + limit size := len(posts) @@ -57,7 +57,9 @@ func (s *simpleCacheIndexServant) IndexPosts(_user *model.User, offset int, limi if size >= end { return posts[offset:end], nil } - return nil, errNotExist + + logrus.Debugln("simpleCacheIndexServant.IndexPosts get index posts from database") + return s.getIndexPosts(user, offset, limit) } func (s *simpleCacheIndexServant) SendAction(act core.IndexActionT) { diff --git a/internal/dao/dao.go b/internal/dao/dao.go index 6526b4a1..00a0fcff 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -14,11 +14,10 @@ var ( ) type dataServant struct { - useCacheIndex bool - cacheIndex core.CacheIndexService - ams core.AuthorizationManageService - engine *gorm.DB - getIndexPostsFunc indexPostsFunc + core.CacheIndexService + + ams core.AuthorizationManageService + engine *gorm.DB } type simpleAuthorizationManageService struct { @@ -36,21 +35,14 @@ func NewDataService() core.DataService { } // initialize CacheIndex if needed - ds.useCacheIndex = true if conf.CfgIf("SimpleCacheIndex") { - ds.getIndexPostsFunc = ds.simpleCacheIndexGetPosts - ds.cacheIndex = newSimpleCacheIndexServant(ds.simpleCacheIndexGetPosts) + ds.CacheIndexService = newSimpleCacheIndexServant(ds.simpleCacheIndexGetPosts) } else if conf.CfgIf("BigCacheIndex") { - ds.getIndexPostsFunc = ds.getIndexPosts - ds.cacheIndex = newBigCacheIndexServant(ds.getIndexPosts) + ds.CacheIndexService = newBigCacheIndexServant(ds.getIndexPosts) } else { - ds.getIndexPostsFunc = ds.getIndexPosts - ds.useCacheIndex = false - } - - if ds.useCacheIndex { - logrus.Infof("use %s as cache index service by version: %s", ds.cacheIndex.Name(), ds.cacheIndex.Version()) + ds.CacheIndexService = newNoneCacheIndexServant(ds.getIndexPosts) } + logrus.Infof("use %s as cache index service by version: %s", ds.CacheIndexService.Name(), ds.CacheIndexService.Version()) return ds } diff --git a/internal/dao/post.go b/internal/dao/post.go index 028d0fbc..a99bde24 100644 --- a/internal/dao/post.go +++ b/internal/dao/post.go @@ -14,7 +14,7 @@ func (d *dataServant) CreatePost(post *model.Post) (*model.Post, error) { if err != nil { return nil, err } - d.indexActive(core.IdxActCreatePost) + d.SendAction(core.IdxActCreatePost) return p, nil } @@ -22,7 +22,7 @@ func (d *dataServant) DeletePost(post *model.Post) error { if err := post.Delete(d.engine); err != nil { return err } - d.indexActive(core.IdxActDeletePost) + d.SendAction(core.IdxActDeletePost) return nil } @@ -36,7 +36,7 @@ func (d *dataServant) StickPost(post *model.Post) error { if err := post.Update(d.engine); err != nil { return err } - d.indexActive(core.IdxActStickPost) + d.SendAction(core.IdxActStickPost) return nil } @@ -76,7 +76,7 @@ func (d *dataServant) VisiblePost(post *model.Post, visibility model.PostVisible } } db.Commit() - d.indexActive(core.IdxActVisiblePost) + d.SendAction(core.IdxActVisiblePost) return nil } @@ -101,7 +101,7 @@ func (d *dataServant) UpdatePost(post *model.Post) error { if err := post.Update(d.engine); err != nil { return err } - d.indexActive(core.IdxActUpdatePost) + d.SendAction(core.IdxActUpdatePost) return nil } diff --git a/internal/dao/post_index.go b/internal/dao/post_index.go index b3ae8e10..a3ffcb90 100644 --- a/internal/dao/post_index.go +++ b/internal/dao/post_index.go @@ -1,23 +1,11 @@ package dao import ( - "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/pkg/types" "github.com/sirupsen/logrus" ) -func (d *dataServant) IndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) { - if d.useCacheIndex { - if posts, err := d.cacheIndex.IndexPosts(user, offset, limit); err == nil { - logrus.Debugf("get index posts from cached") - return posts, nil - } - } - logrus.Debugf("get index posts from database but useCacheIndex: %t", d.useCacheIndex) - return d.getIndexPostsFunc(user, offset, limit) -} - // getIndexPosts TODO: 未来可能根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; func (d *dataServant) getIndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) { predicates := model.Predicates{ @@ -61,9 +49,3 @@ func (d *dataServant) simpleCacheIndexGetPosts(_user *model.User, offset int, li return d.MergePosts(posts) } - -func (d *dataServant) indexActive(act core.IndexActionT) { - if d.useCacheIndex { - d.cacheIndex.SendAction(act) - } -}