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

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

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

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

Loading…
Cancel
Save