Merge pull request #83 from alimy/main

abstract IndexPostsService interface for get index posts
pull/87/head
Michael Li 3 years ago committed by GitHub
commit b46cec2c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,5 @@
package core package core
import (
"github.com/rocboss/paopao-ce/internal/model"
)
const ( const (
IdxActNop IndexActionT = iota + 1 IdxActNop IndexActionT = iota + 1
IdxActCreatePost IdxActCreatePost
@ -33,6 +29,6 @@ func (a IndexActionT) String() string {
// CacheIndexService cache index service interface // CacheIndexService cache index service interface
type CacheIndexService interface { type CacheIndexService interface {
IndexPosts(offset int, limit int) ([]*model.PostFormated, bool) IndexPostsService
SendAction(active IndexActionT) SendAction(active IndexActionT)
} }

@ -8,6 +8,7 @@ import (
type DataService interface { type DataService interface {
WalletService WalletService
SearchService SearchService
IndexPostsService
GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error)
GetCommentByID(id int64) (*model.Comment, error) GetCommentByID(id int64) (*model.Comment, error)
@ -34,7 +35,6 @@ type DataService interface {
LockPost(post *model.Post) error LockPost(post *model.Post) error
StickPost(post *model.Post) error StickPost(post *model.Post) error
GetPostByID(id int64) (*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) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error)
GetPostCount(conditions *model.ConditionsT) (int64, error) GetPostCount(conditions *model.ConditionsT) (int64, error)
UpdatePost(post *model.Post) error UpdatePost(post *model.Post) error

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

@ -1,6 +1,7 @@
package dao package dao
import ( import (
"errors"
"time" "time"
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
@ -9,6 +10,10 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
var (
errNotExist = errors.New("index posts cache not exist")
)
func newSimpleCacheIndexServant(getIndexPosts func(offset, limit int) ([]*model.PostFormated, error)) *simpleCacheIndexServant { func newSimpleCacheIndexServant(getIndexPosts func(offset, limit int) ([]*model.PostFormated, error)) *simpleCacheIndexServant {
s := conf.SimpleCacheIndexSetting s := conf.SimpleCacheIndexSetting
cacheIndex := &simpleCacheIndexServant{ cacheIndex := &simpleCacheIndexServant{
@ -43,15 +48,15 @@ func newSimpleCacheIndexServant(getIndexPosts func(offset, limit int) ([]*model.
return cacheIndex 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) posts := s.atomicIndex.Load().([]*model.PostFormated)
end := offset + limit end := offset + limit
size := len(posts) size := len(posts)
logrus.Debugf("get index posts from posts: %d offset:%d limit:%d start:%d, end:%d", size, offset, limit, offset, end) logrus.Debugf("get index posts from posts: %d offset:%d limit:%d start:%d, end:%d", size, offset, limit, offset, end)
if size >= 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) { func (s *simpleCacheIndexServant) SendAction(act core.IndexActionT) {

@ -8,7 +8,7 @@ import (
func (d *dataServant) IndexPosts(offset int, limit int) ([]*model.PostFormated, error) { func (d *dataServant) IndexPosts(offset int, limit int) ([]*model.PostFormated, error) {
if d.useCacheIndex { 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") logrus.Debugln("get index posts from cached")
return posts, nil return posts, nil
} }

Loading…
Cancel
Save