Merge pull request #145 from alimy/pr-optimize-cache-index

optimize post cache index service integrate logic
pull/146/head
Michael Li 2 years ago committed by GitHub
commit f5e915cd5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,7 @@ import (
var ( var (
_ core.CacheIndexService = (*simpleCacheIndexServant)(nil) _ core.CacheIndexService = (*simpleCacheIndexServant)(nil)
_ core.CacheIndexService = (*bigCacheIndexServant)(nil) _ core.CacheIndexService = (*bigCacheIndexServant)(nil)
_ core.CacheIndexService = (*noneCacheIndexServant)(nil)
) )
type postsEntry struct { type postsEntry struct {
@ -38,3 +39,7 @@ type simpleCacheIndexServant struct {
checkTick *time.Ticker checkTick *time.Ticker
expireIndexTick *time.Ticker expireIndexTick *time.Ticker
} }
type noneCacheIndexServant struct {
getIndexPosts indexPostsFunc
}

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

@ -49,7 +49,7 @@ func newSimpleCacheIndexServant(getIndexPosts indexPostsFunc) *simpleCacheIndexS
return cacheIndex 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) posts := s.atomicIndex.Load().([]*model.PostFormated)
end := offset + limit end := offset + limit
size := len(posts) size := len(posts)
@ -57,7 +57,9 @@ func (s *simpleCacheIndexServant) IndexPosts(_user *model.User, offset int, limi
if size >= end { if size >= end {
return posts[offset:end], nil 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) { func (s *simpleCacheIndexServant) SendAction(act core.IndexActionT) {

@ -14,11 +14,10 @@ var (
) )
type dataServant struct { type dataServant struct {
useCacheIndex bool core.CacheIndexService
cacheIndex core.CacheIndexService
ams core.AuthorizationManageService ams core.AuthorizationManageService
engine *gorm.DB engine *gorm.DB
getIndexPostsFunc indexPostsFunc
} }
type simpleAuthorizationManageService struct { type simpleAuthorizationManageService struct {
@ -36,21 +35,14 @@ func NewDataService() core.DataService {
} }
// initialize CacheIndex if needed // initialize CacheIndex if needed
ds.useCacheIndex = true
if conf.CfgIf("SimpleCacheIndex") { if conf.CfgIf("SimpleCacheIndex") {
ds.getIndexPostsFunc = ds.simpleCacheIndexGetPosts ds.CacheIndexService = newSimpleCacheIndexServant(ds.simpleCacheIndexGetPosts)
ds.cacheIndex = newSimpleCacheIndexServant(ds.simpleCacheIndexGetPosts)
} else if conf.CfgIf("BigCacheIndex") { } else if conf.CfgIf("BigCacheIndex") {
ds.getIndexPostsFunc = ds.getIndexPosts ds.CacheIndexService = newBigCacheIndexServant(ds.getIndexPosts)
ds.cacheIndex = newBigCacheIndexServant(ds.getIndexPosts)
} else { } else {
ds.getIndexPostsFunc = ds.getIndexPosts ds.CacheIndexService = newNoneCacheIndexServant(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())
} }
logrus.Infof("use %s as cache index service by version: %s", ds.CacheIndexService.Name(), ds.CacheIndexService.Version())
return ds return ds
} }

@ -14,7 +14,7 @@ func (d *dataServant) CreatePost(post *model.Post) (*model.Post, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
d.indexActive(core.IdxActCreatePost) d.SendAction(core.IdxActCreatePost)
return p, nil return p, nil
} }
@ -22,7 +22,7 @@ func (d *dataServant) DeletePost(post *model.Post) error {
if err := post.Delete(d.engine); err != nil { if err := post.Delete(d.engine); err != nil {
return err return err
} }
d.indexActive(core.IdxActDeletePost) d.SendAction(core.IdxActDeletePost)
return nil return nil
} }
@ -36,7 +36,7 @@ func (d *dataServant) StickPost(post *model.Post) error {
if err := post.Update(d.engine); err != nil { if err := post.Update(d.engine); err != nil {
return err return err
} }
d.indexActive(core.IdxActStickPost) d.SendAction(core.IdxActStickPost)
return nil return nil
} }
@ -76,7 +76,7 @@ func (d *dataServant) VisiblePost(post *model.Post, visibility model.PostVisible
} }
} }
db.Commit() db.Commit()
d.indexActive(core.IdxActVisiblePost) d.SendAction(core.IdxActVisiblePost)
return nil return nil
} }
@ -101,7 +101,7 @@ func (d *dataServant) UpdatePost(post *model.Post) error {
if err := post.Update(d.engine); err != nil { if err := post.Update(d.engine); err != nil {
return err return err
} }
d.indexActive(core.IdxActUpdatePost) d.SendAction(core.IdxActUpdatePost)
return nil return nil
} }

@ -1,23 +1,11 @@
package dao package dao
import ( import (
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/types" "github.com/rocboss/paopao-ce/pkg/types"
"github.com/sirupsen/logrus" "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查询广场推文列表简单做到不同用户的主页都是不同的 // getIndexPosts TODO: 未来可能根据userId查询广场推文列表简单做到不同用户的主页都是不同的
func (d *dataServant) getIndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) { func (d *dataServant) getIndexPosts(user *model.User, offset int, limit int) ([]*model.PostFormated, error) {
predicates := model.Predicates{ predicates := model.Predicates{
@ -61,9 +49,3 @@ func (d *dataServant) simpleCacheIndexGetPosts(_user *model.User, offset int, li
return d.MergePosts(posts) return d.MergePosts(posts)
} }
func (d *dataServant) indexActive(act core.IndexActionT) {
if d.useCacheIndex {
d.cacheIndex.SendAction(act)
}
}

Loading…
Cancel
Save