mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.3 KiB
122 lines
3.3 KiB
2 years ago
|
package dao
|
||
|
|
||
|
import (
|
||
2 years ago
|
"errors"
|
||
2 years ago
|
"time"
|
||
|
|
||
2 years ago
|
"github.com/Masterminds/semver/v3"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/conf"
|
||
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
|
"github.com/rocboss/paopao-ce/internal/model"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
errNotExist = errors.New("index posts cache not exist")
|
||
|
)
|
||
|
|
||
2 years ago
|
func newSimpleCacheIndexServant(getIndexPosts indexPostsFunc) *simpleCacheIndexServant {
|
||
2 years ago
|
s := conf.SimpleCacheIndexSetting
|
||
2 years ago
|
cacheIndex := &simpleCacheIndexServant{
|
||
|
getIndexPosts: getIndexPosts,
|
||
|
maxIndexSize: s.MaxIndexSize,
|
||
|
indexPosts: make([]*model.PostFormated, 0),
|
||
2 years ago
|
checkTick: time.NewTicker(s.CheckTickDuration), // check whether need update index every 1 minute
|
||
2 years ago
|
expireIndexTick: time.NewTicker(time.Second),
|
||
|
}
|
||
|
|
||
|
// force expire index every ExpireTickDuration second
|
||
|
if s.ExpireTickDuration != 0 {
|
||
2 years ago
|
cacheIndex.expireIndexTick.Reset(s.CheckTickDuration)
|
||
2 years ago
|
} else {
|
||
|
cacheIndex.expireIndexTick.Stop()
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// indexActionCh capacity custom configure by conf.yaml need in [10, 10000]
|
||
|
// or re-compile source to adjust min/max capacity
|
||
|
capacity := s.ActionQPS
|
||
|
if capacity < 10 {
|
||
|
capacity = 10
|
||
|
} else if capacity > 10000 {
|
||
|
capacity = 10000
|
||
|
}
|
||
|
cacheIndex.indexActionCh = make(chan core.IndexActionT, capacity)
|
||
|
|
||
2 years ago
|
// start index posts
|
||
|
cacheIndex.atomicIndex.Store(cacheIndex.indexPosts)
|
||
|
go cacheIndex.startIndexPosts()
|
||
|
|
||
|
return cacheIndex
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *simpleCacheIndexServant) IndexPosts(_userId int64, offset int, limit int) ([]*model.PostFormated, error) {
|
||
2 years ago
|
posts := s.atomicIndex.Load().([]*model.PostFormated)
|
||
2 years ago
|
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 {
|
||
2 years ago
|
return posts[offset:end], nil
|
||
2 years ago
|
}
|
||
2 years ago
|
return nil, errNotExist
|
||
2 years ago
|
}
|
||
|
|
||
|
func (s *simpleCacheIndexServant) SendAction(act core.IndexActionT) {
|
||
|
select {
|
||
|
case s.indexActionCh <- act:
|
||
|
logrus.Debugf("send indexAction by chan: %s", act)
|
||
|
default:
|
||
|
go func(ch chan<- core.IndexActionT, act core.IndexActionT) {
|
||
|
logrus.Debugf("send indexAction by goroutine: %s", act)
|
||
|
ch <- act
|
||
|
}(s.indexActionCh, act)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *simpleCacheIndexServant) startIndexPosts() {
|
||
|
var err error
|
||
|
for {
|
||
|
select {
|
||
|
case <-s.checkTick.C:
|
||
|
if len(s.indexPosts) == 0 {
|
||
|
logrus.Debugf("index posts by checkTick")
|
||
2 years ago
|
if s.indexPosts, err = s.getIndexPosts(0, 0, s.maxIndexSize); err == nil {
|
||
2 years ago
|
s.atomicIndex.Store(s.indexPosts)
|
||
|
} else {
|
||
|
logrus.Errorf("get index posts err: %v", err)
|
||
|
}
|
||
|
}
|
||
|
case <-s.expireIndexTick.C:
|
||
|
logrus.Debugf("expire index posts by expireIndexTick")
|
||
|
if len(s.indexPosts) != 0 {
|
||
|
s.indexPosts = nil
|
||
|
s.atomicIndex.Store(s.indexPosts)
|
||
|
}
|
||
|
case action := <-s.indexActionCh:
|
||
|
switch action {
|
||
2 years ago
|
// TODO: 这里列出来是因为后续可能会精细化处理每种情况
|
||
|
case core.IdxActCreatePost,
|
||
|
core.IdxActUpdatePost,
|
||
|
core.IdxActDeletePost,
|
||
|
core.IdxActStickPost,
|
||
|
core.IdxActVisiblePost:
|
||
2 years ago
|
// prevent many update post in least time
|
||
|
if len(s.indexPosts) != 0 {
|
||
|
logrus.Debugf("remove index posts by action %s", action)
|
||
|
s.indexPosts = nil
|
||
|
s.atomicIndex.Store(s.indexPosts)
|
||
|
}
|
||
|
default:
|
||
|
// nop
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
func (s *simpleCacheIndexServant) Name() string {
|
||
|
return "SimpleCacheIndex"
|
||
|
}
|
||
|
|
||
|
func (s *simpleCacheIndexServant) Version() *semver.Version {
|
||
|
return semver.MustParse("v0.1.0")
|
||
|
}
|