diff --git a/actions/common/common.go b/actions/common/common.go index 856751c..fe612c7 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -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() { diff --git a/actions/common/posts.go b/actions/common/posts.go index 0606314..dbec9b9 100644 --- a/actions/common/posts.go +++ b/actions/common/posts.go @@ -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 +} diff --git a/actions/detail.go b/actions/detail.go index 513430f..93a6448 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -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 diff --git a/actions/index.go b/actions/index.go index a120953..1ef476d 100644 --- a/actions/index.go +++ b/actions/index.go @@ -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"]) } diff --git a/config.example.yaml b/config.example.yaml index 076293d..006ca33 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -47,3 +47,5 @@ sleepTime: [1s,3s] maxRequestNum: 500 # 单ip同时最大搜索请求数 singleIpSearchNum: 10 +# 文档最大id缓存时间 +maxPostIdCacheTime: 1h diff --git a/middleware/flowLimit.go b/middleware/flowLimit.go index e7ef1c2..e8ef405 100644 --- a/middleware/flowLimit.go +++ b/middleware/flowLimit.go @@ -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 diff --git a/vars/config.go b/vars/config.go index f1956c9..9f0159c 100644 --- a/vars/config.go +++ b/vars/config.go @@ -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 {