From 528e8e9348f8200d2cda217416532afe277059ad Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 26 Sep 2022 16:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=91=98=E8=A6=81=E7=AE=97=E6=B3=95=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/detail.go | 10 ++-- actions/index.go | 9 ++-- cache/map.go | 4 +- config.example.yaml | 12 +++-- main.go | 2 + plugins/Excerpt.go | 68 -------------------------- plugins/digest.go | 113 ++++++++++++++++++++++++++++++++++++++++++++ plugins/posts.go | 2 +- vars/config.go | 2 + 9 files changed, 135 insertions(+), 87 deletions(-) delete mode 100644 plugins/Excerpt.go create mode 100644 plugins/digest.go diff --git a/actions/detail.go b/actions/detail.go index da28a1f..36969b0 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -1,7 +1,6 @@ package actions import ( - "context" "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -27,11 +26,10 @@ func Detail(c *gin.Context) { hh := detailHandler{ c, } - ctx := context.TODO() - recent := common.RecentPosts(ctx) + recent := common.RecentPosts(c) archive := common.Archives() - categoryItems := common.Categories(ctx) - recentComments := common.RecentComments(ctx) + categoryItems := common.Categories(c) + recentComments := common.RecentComments(c) var h = gin.H{ "title": models.Options["blogname"], "options": models.Options, @@ -80,7 +78,7 @@ func Detail(c *gin.Context) { showComment = false } plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post) - comments, err := common.PostComments(ctx, post.Id) + comments, err := common.PostComments(c, post.Id) commentss := treeComments(comments) prev, next, err := common.GetContextPost(post.Id, post.PostDate) h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]) diff --git a/actions/index.go b/actions/index.go index d53ad48..c8e137f 100644 --- a/actions/index.go +++ b/actions/index.go @@ -1,7 +1,6 @@ package actions import ( - "context" "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -155,11 +154,10 @@ func (h *indexHandle) getTotalPage(totalRaws int) int { func Index(c *gin.Context) { h := newIndexHandle(c) h.parseParams() - ctx := context.TODO() archive := common.Archives() - recent := common.RecentPosts(ctx) - categoryItems := common.Categories(ctx) - recentComments := common.RecentComments(ctx) + recent := common.RecentPosts(c) + categoryItems := common.Categories(c) + recentComments := common.RecentComments(c) ginH := gin.H{ "options": models.Options, "recentPosts": recent, @@ -185,7 +183,6 @@ func Index(c *gin.Context) { } err = common.QueryAndSetPostCache(postIds) pw := h.session.Get("post_password") - c.Set("post_password", pw) plug := plugins.NewPostPlugin(c, h.scene) for i, v := range postIds { post, _ := common.PostsCache.Load(v.Id) diff --git a/cache/map.go b/cache/map.go index cf9956d..5c5f481 100644 --- a/cache/map.go +++ b/cache/map.go @@ -17,11 +17,11 @@ type MapCache[K comparable, V any] struct { incr int } -func NewMapCache[K comparable, V any](fun func(...any) (V, error), duration time.Duration) *MapCache[K, V] { +func NewMapCache[K comparable, V any](fun func(...any) (V, error), expireTime time.Duration) *MapCache[K, V] { return &MapCache[K, V]{ mutex: &sync.Mutex{}, setCacheFunc: fun, - expireTime: duration, + expireTime: expireTime, data: make(map[K]V), } } diff --git a/config.example.yaml b/config.example.yaml index b4ce843..5efb338 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -16,10 +16,14 @@ mysql: # 连接的生命时长 connMaxLifetime: 236 # 最近文章缓存时间 -recentPostCacheTime: 30m +recentPostCacheTime: 5m # 分类缓存时间 -categoryCacheTime: 30m +categoryCacheTime: 5m # 上下篇缓存时间 -contextPostCacheTime: 1h +contextPostCacheTime: 10h # 最近评论缓存时间 -recentCommentsCacheTime: 10m +recentCommentsCacheTime: 5m +# 摘要缓存时间 +digestCacheTime: 5m +# 摘要字数 +digestWordCount: 300 diff --git a/main.go b/main.go index 9fe5e1a..a48b013 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/models" + "github/fthvgb1/wp-go/plugins" "github/fthvgb1/wp-go/route" "github/fthvgb1/wp-go/vars" ) @@ -30,6 +31,7 @@ func init() { } common.InitCache() + plugins.InitDigest() } func main() { diff --git a/plugins/Excerpt.go b/plugins/Excerpt.go deleted file mode 100644 index ea741ae..0000000 --- a/plugins/Excerpt.go +++ /dev/null @@ -1,68 +0,0 @@ -package plugins - -import ( - "fmt" - "github.com/gin-gonic/gin" - "github/fthvgb1/wp-go/helper" - "github/fthvgb1/wp-go/models" - "regexp" - "strings" - "unicode/utf8" -) - -var removeWpBlock = regexp.MustCompile("") -var more = regexp.MustCompile("") -var tag = regexp.MustCompile(`<.*?>`) -var limit = 300 - -func ExceptRaw(str string, limit, id int) string { - if r := more.FindString(str); r != "" { - m := strings.Split(str, r) - str = m[0] - return "" - } - content := removeWpBlock.ReplaceAllString(str, "") - content = strings.Trim(content, " \t\n\r\000\x0B") - content = strings.Replace(content, "]]>", "]]>", -1) - content = helper.StripTagsX(content, "