文档最大Id缓存时间

This commit is contained in:
xing 2022-10-04 11:13:14 +08:00
parent 0ddd39e209
commit 7bdc47559a
7 changed files with 34 additions and 1 deletions

View File

@ -24,6 +24,8 @@ var postsCache *cache.MapCache[uint64, models.WpPosts]
var monthPostsCache *cache.MapCache[string, []uint64]
var postListIdsCache *cache.MapCache[string, PostIds]
var searchPostIdsCache *cache.MapCache[string, PostIds]
var maxPostIdCache *cache.SliceCache[uint64]
var TotalRaw int
func InitActionsCommonCache() {
archivesCaches = &Arch{
@ -49,6 +51,8 @@ func InitActionsCommonCache() {
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
postCommentCaches = cache.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.CommentsCacheTime)
maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime)
}
func ClearCache() {

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models"
"strings"
@ -114,5 +115,22 @@ func searchPostIds(args ...any) (ids PostIds, err error) {
ids.Ids = append(ids.Ids, posts.Id)
}
ids.Length = total
if total > TotalRaw {
TotalRaw = total
}
return
}
func getMaxPostId(...any) ([]uint64, error) {
r, err := models.Find[models.WpPosts](models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID", "", nil, nil, 0)
var id uint64
if len(r) > 0 {
id = r[0].Id
}
return []uint64{id}, err
}
func GetMaxPostId(ctx *gin.Context) (uint64, error) {
Id, err := maxPostIdCache.GetCache(ctx, time.Second)
return Id[0], err
}

View File

@ -56,6 +56,11 @@ func Detail(c *gin.Context) {
}
}
ID := uint64(Id)
maxId, err := common.GetMaxPostId(c)
logs.ErrPrintln(err, "get max post id")
if ID > maxId || err != nil {
return
}
post, err := common.GetPostAndCache(c, ID)
if post.Id == 0 || err != nil {
return

View File

@ -145,6 +145,9 @@ func (h *indexHandle) parseParams() {
h.page = pa
}
}
if common.TotalRaw > 0 && h.getTotalPage(common.TotalRaw) < h.page*h.page {
h.page = 1
}
if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), models.Options["blogname"])
}

View File

@ -47,3 +47,5 @@ sleepTime: [1s,3s]
maxRequestNum: 500
# 单ip同时最大搜索请求数
singleIpSearchNum: 10
# 文档最大id缓存时间
maxPostIdCacheTime: 1h

View File

@ -54,7 +54,7 @@ func FlowLimit() func(ctx *gin.Context) {
t := randFn(vars.Conf.SleepTime[0], vars.Conf.SleepTime[1])
time.Sleep(t)
} else if flow > vars.Conf.MaxRequestNum {
c.String(http.StatusForbidden, "请求太多了,服务器君压力山大==!, 请稍后访问")
c.String(http.StatusForbidden, "请求太多了,服务器君表示压力山大==!, 请稍后访问")
c.Abort()
return

View File

@ -28,6 +28,7 @@ type Config struct {
SleepTime []time.Duration `yaml:"sleepTime"`
MaxRequestNum int64 `yaml:"maxRequestNum"`
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
}
type Mysql struct {