From 7bdc47559a29ede59176da8a6a56c92186b0c443 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 4 Oct 2022 11:13:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=9C=80=E5=A4=A7Id=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 4 ++++ actions/common/posts.go | 18 ++++++++++++++++++ actions/detail.go | 5 +++++ actions/index.go | 3 +++ config.example.yaml | 2 ++ middleware/flowLimit.go | 2 +- vars/config.go | 1 + 7 files changed, 34 insertions(+), 1 deletion(-) 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 {