From 9bbbbc8f7bbb8a1f96b6954c25079ea80b6c1817 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 8 Oct 2022 19:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 5 ++- config.example.yaml | 4 +- rss2/rss2.go | 85 +++++++++++++++++++--------------------- vars/config.go | 3 +- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index ba6f2ed..ce0e8f1 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -48,13 +48,13 @@ func InitActionsCommonCache() { recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) - postCommentCaches = cache.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.CommentsCacheTime) + postCommentCaches = cache.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.PostCommentsCacheTime) maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime) usersCache = cache.NewMapCacheByBatchFn[uint64, models.WpUsers](getUsers, vars.Conf.UserInfoCacheTime) - commentsCache = cache.NewMapCacheByBatchFn[uint64, models.WpComments](getCommentByIds, time.Hour) + commentsCache = cache.NewMapCacheByBatchFn[uint64, models.WpComments](getCommentByIds, vars.Conf.CommentsCacheTime) } func ClearCache() { @@ -65,6 +65,7 @@ func ClearCache() { monthPostsCache.ClearExpired() postContextCache.ClearExpired() usersCache.ClearExpired() + commentsCache.ClearExpired() } type PostIds struct { diff --git a/config.example.yaml b/config.example.yaml index 19494ca..036ed1a 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -36,7 +36,7 @@ monthPostCacheTime: 1h # 文档数据缓存时间 postDataCacheTime: 1h # 文章评论缓存时间 -commentsCacheTime: 5m +postCommentsCacheTime: 5m # 定时清理缓存周期时间 crontabClearCacheTime: 5m # 到达指定并发请求数时随机sleep @@ -51,5 +51,7 @@ singleIpSearchNum: 10 maxPostIdCacheTime: 1h # 用户信息缓存时间 userInfoCacheTime: 24h +# 单独评论缓存时间 +commentsCacheTime: 24h # Gzip gzip: false diff --git a/rss2/rss2.go b/rss2/rss2.go index 1120427..9c2efe8 100644 --- a/rss2/rss2.go +++ b/rss2/rss2.go @@ -3,6 +3,7 @@ package rss2 import ( "fmt" "github/fthvgb1/wp-go/helper" + "strconv" "strings" ) @@ -38,19 +39,28 @@ var template = ` var templateItems = ` {$title} - {$link} + {$link} {$comments} - + {$creator} {$pubDate} {$category} - {$guid} + {$guid} {$description} {$commentRss} {$commentNumber} - ` +var templateReplace = map[string]string{ + "{$category}": "", + "{$link}": "%s", + "{$creator}": "", + "{$description}": "", + "{$comments}": "%s", + "{$commentRss}": "%s", + "{$commentNumber}": "%s", + "{$guid}": "%s", +} type Rss2 struct { Title string @@ -103,54 +113,41 @@ func (r Rss2) GetXML() (xml string) { func (i Item) GetXml() (xml string) { xml = templateItems for k, v := range map[string]string{ - "{$title}": i.Title, - "{$link}": i.Link, - "{$comments}": i.getComments(), - "{$author}": i.Creator, - "{$pubDate}": i.PubDate, - "{$category}": i.getCategory(), - "{$guid}": i.Guid, - "{$description}": i.getDescription(), - "{$content}": i.Content, - "{$commentRss}": i.getCommentRss(), - "{$commentNumber}": i.getSlashComments(), + "{$title}": i.Title, + "{$pubDate}": i.PubDate, + "{$content}": i.Content, } { xml = strings.Replace(xml, k, v, -1) } - return -} -func (i Item) getCategory() string { - r := "" - if i.Category != "" { - r = fmt.Sprintf("", i.CommentLink) + m := map[string]string{ + "{$category}": i.Category, + "{$link}": i.Link, + "{$creator}": i.Creator, + "{$description}": i.Description, + "{$comments}": i.CommentLink, + "{$guid}": i.Guid, + "{$commentRss}": i.CommentRss, } - return r -} -func (i Item) getDescription() string { - r := "" - if i.Description != "" { - r = fmt.Sprintf("", i.Description) - } - return r -} -func (i Item) getComments() string { - r := "" - if i.CommentLink != "" { - r = fmt.Sprintf("%s", i.CommentLink) - } - return r -} -func (i Item) getCommentRss() (r string) { - if i.CommentLink != "" && i.SlashComments > 0 { - r = fmt.Sprintf("%s", i.CommentRss) + if i.CommentRss != "" && i.SlashComments > 0 { + m["{$commentRss}"] = i.CommentRss + } else { + m["{$commentRss}"] = "" } - return -} -func (i Item) getSlashComments() (r string) { if i.SlashComments > 0 { - r = fmt.Sprintf("%d", i.SlashComments) + m["{$commentNumber}"] = strconv.Itoa(i.SlashComments) + } else { + m["{$commentNumber}"] = "" } + + for k, v := range m { + t := "" + if v != "" { + t = fmt.Sprintf(templateReplace[k], v) + } + xml = strings.Replace(xml, k, t, -1) + } + return } diff --git a/vars/config.go b/vars/config.go index f4fed55..e8e2207 100644 --- a/vars/config.go +++ b/vars/config.go @@ -22,7 +22,7 @@ type Config struct { SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` - CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` + PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"` SleepTime []time.Duration `yaml:"sleepTime"` @@ -30,6 +30,7 @@ type Config struct { SingleIpSearchNum int64 `yaml:"singleIpSearchNum"` MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` + CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` Gzip bool `yaml:"gzip"` }