diff --git a/internal/core/cache.go b/internal/core/cache.go index 25394957..08eae18f 100644 --- a/internal/core/cache.go +++ b/internal/core/cache.go @@ -1,9 +1,5 @@ package core -import ( - "github.com/rocboss/paopao-ce/internal/model" -) - const ( IdxActNop IndexActionT = iota + 1 IdxActCreatePost @@ -33,6 +29,6 @@ func (a IndexActionT) String() string { // CacheIndexService cache index service interface type CacheIndexService interface { - IndexPosts(offset int, limit int) ([]*model.PostFormated, bool) + IndexPostsService SendAction(active IndexActionT) } diff --git a/internal/core/core.go b/internal/core/core.go index cdd1a92b..f815525b 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -8,6 +8,7 @@ import ( type DataService interface { WalletService SearchService + IndexPostsService GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) GetCommentByID(id int64) (*model.Comment, error) @@ -34,7 +35,6 @@ type DataService interface { LockPost(post *model.Post) error StickPost(post *model.Post) error GetPostByID(id int64) (*model.Post, error) - IndexPosts(offset int, limit int) ([]*model.PostFormated, error) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) GetPostCount(conditions *model.ConditionsT) (int64, error) UpdatePost(post *model.Post) error diff --git a/internal/core/index.go b/internal/core/index.go new file mode 100644 index 00000000..afc02048 --- /dev/null +++ b/internal/core/index.go @@ -0,0 +1,9 @@ +package core + +import ( + "github.com/rocboss/paopao-ce/internal/model" +) + +type IndexPostsService interface { + IndexPosts(offset int, limit int) ([]*model.PostFormated, error) +} diff --git a/internal/dao/cache_index.go b/internal/dao/cache_index.go index ce213789..b8626be9 100644 --- a/internal/dao/cache_index.go +++ b/internal/dao/cache_index.go @@ -1,6 +1,7 @@ package dao import ( + "errors" "time" "github.com/rocboss/paopao-ce/internal/conf" @@ -9,6 +10,10 @@ import ( "github.com/sirupsen/logrus" ) +var ( + errNotExist = errors.New("index posts cache not exist") +) + func newSimpleCacheIndexServant(getIndexPosts func(offset, limit int) ([]*model.PostFormated, error)) *simpleCacheIndexServant { s := conf.SimpleCacheIndexSetting cacheIndex := &simpleCacheIndexServant{ @@ -43,15 +48,15 @@ func newSimpleCacheIndexServant(getIndexPosts func(offset, limit int) ([]*model. return cacheIndex } -func (s *simpleCacheIndexServant) IndexPosts(offset int, limit int) ([]*model.PostFormated, bool) { +func (s *simpleCacheIndexServant) IndexPosts(offset int, limit int) ([]*model.PostFormated, error) { posts := s.atomicIndex.Load().([]*model.PostFormated) end := offset + limit size := len(posts) logrus.Debugf("get index posts from posts: %d offset:%d limit:%d start:%d, end:%d", size, offset, limit, offset, end) if size >= end { - return posts[offset:end], true + return posts[offset:end], nil } - return nil, false + return nil, errNotExist } func (s *simpleCacheIndexServant) SendAction(act core.IndexActionT) { diff --git a/internal/dao/post_index.go b/internal/dao/post_index.go index 53a0913d..0a184dd7 100644 --- a/internal/dao/post_index.go +++ b/internal/dao/post_index.go @@ -8,7 +8,7 @@ import ( func (d *dataServant) IndexPosts(offset int, limit int) ([]*model.PostFormated, error) { if d.useCacheIndex { - if posts, ok := d.cacheIndex.IndexPosts(offset, limit); ok { + if posts, err := d.cacheIndex.IndexPosts(offset, limit); err == nil { logrus.Debugln("get index posts from cached") return posts, nil }