This commit is contained in:
xing 2022-10-08 19:35:05 +08:00
parent e3dc2a156c
commit 9bbbbc8f7b
4 changed files with 49 additions and 48 deletions

View File

@ -48,13 +48,13 @@ func InitActionsCommonCache() {
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) 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) maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime)
usersCache = cache.NewMapCacheByBatchFn[uint64, models.WpUsers](getUsers, vars.Conf.UserInfoCacheTime) 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() { func ClearCache() {
@ -65,6 +65,7 @@ func ClearCache() {
monthPostsCache.ClearExpired() monthPostsCache.ClearExpired()
postContextCache.ClearExpired() postContextCache.ClearExpired()
usersCache.ClearExpired() usersCache.ClearExpired()
commentsCache.ClearExpired()
} }
type PostIds struct { type PostIds struct {

View File

@ -36,7 +36,7 @@ monthPostCacheTime: 1h
# 文档数据缓存时间 # 文档数据缓存时间
postDataCacheTime: 1h postDataCacheTime: 1h
# 文章评论缓存时间 # 文章评论缓存时间
commentsCacheTime: 5m postCommentsCacheTime: 5m
# 定时清理缓存周期时间 # 定时清理缓存周期时间
crontabClearCacheTime: 5m crontabClearCacheTime: 5m
# 到达指定并发请求数时随机sleep # 到达指定并发请求数时随机sleep
@ -51,5 +51,7 @@ singleIpSearchNum: 10
maxPostIdCacheTime: 1h maxPostIdCacheTime: 1h
# 用户信息缓存时间 # 用户信息缓存时间
userInfoCacheTime: 24h userInfoCacheTime: 24h
# 单独评论缓存时间
commentsCacheTime: 24h
# Gzip # Gzip
gzip: false gzip: false

View File

@ -3,6 +3,7 @@ package rss2
import ( import (
"fmt" "fmt"
"github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/helper"
"strconv"
"strings" "strings"
) )
@ -38,19 +39,28 @@ var template = `<?xml version="1.0" encoding="UTF-8"?>
var templateItems = ` var templateItems = `
<item> <item>
<title>{$title}</title> <title>{$title}</title>
<link>{$link}</link> {$link}
{$comments} {$comments}
<dc:creator><![CDATA[{$author}]]></dc:creator> {$creator}
<pubDate>{$pubDate}</pubDate> <pubDate>{$pubDate}</pubDate>
{$category} {$category}
<guid isPermaLink="false">{$guid}</guid> {$guid}
{$description} {$description}
<content:encoded><![CDATA[{$content}]]></content:encoded> <content:encoded><![CDATA[{$content}]]></content:encoded>
{$commentRss} {$commentRss}
{$commentNumber} {$commentNumber}
</item> </item>
` `
var templateReplace = map[string]string{
"{$category}": "<category><![CDATA[%s]]></category>",
"{$link}": "<link>%s</link>",
"{$creator}": "<dc:creator><![CDATA[%s]]></dc:creator>",
"{$description}": "<description><![CDATA[%s]]></description>",
"{$comments}": "<comments>%s</comments>",
"{$commentRss}": "<wfw:commentRss>%s</wfw:commentRss>",
"{$commentNumber}": "<slash:comments>%s</slash:comments>",
"{$guid}": "<guid isPermaLink=\"false\">%s</guid>",
}
type Rss2 struct { type Rss2 struct {
Title string Title string
@ -103,54 +113,41 @@ func (r Rss2) GetXML() (xml string) {
func (i Item) GetXml() (xml string) { func (i Item) GetXml() (xml string) {
xml = templateItems xml = templateItems
for k, v := range map[string]string{ for k, v := range map[string]string{
"{$title}": i.Title, "{$title}": i.Title,
"{$link}": i.Link, "{$pubDate}": i.PubDate,
"{$comments}": i.getComments(), "{$content}": i.Content,
"{$author}": i.Creator,
"{$pubDate}": i.PubDate,
"{$category}": i.getCategory(),
"{$guid}": i.Guid,
"{$description}": i.getDescription(),
"{$content}": i.Content,
"{$commentRss}": i.getCommentRss(),
"{$commentNumber}": i.getSlashComments(),
} { } {
xml = strings.Replace(xml, k, v, -1) xml = strings.Replace(xml, k, v, -1)
} }
return
}
func (i Item) getCategory() string { m := map[string]string{
r := "" "{$category}": i.Category,
if i.Category != "" { "{$link}": i.Link,
r = fmt.Sprintf("<category><![CDATA[%s]]></category>", i.CommentLink) "{$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("<description><![CDATA[%s]]></description>", i.Description)
}
return r
}
func (i Item) getComments() string {
r := ""
if i.CommentLink != "" {
r = fmt.Sprintf("<comments>%s</comments>", i.CommentLink)
}
return r
}
func (i Item) getCommentRss() (r string) { if i.CommentRss != "" && i.SlashComments > 0 {
if i.CommentLink != "" && i.SlashComments > 0 { m["{$commentRss}"] = i.CommentRss
r = fmt.Sprintf("<wfw:commentRss>%s</wfw:commentRss>", i.CommentRss) } else {
m["{$commentRss}"] = ""
} }
return
}
func (i Item) getSlashComments() (r string) {
if i.SlashComments > 0 { if i.SlashComments > 0 {
r = fmt.Sprintf("<slash:comments>%d</slash:comments>", 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 return
} }

View File

@ -22,7 +22,7 @@ type Config struct {
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"` MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
SleepTime []time.Duration `yaml:"sleepTime"` SleepTime []time.Duration `yaml:"sleepTime"`
@ -30,6 +30,7 @@ type Config struct {
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"` SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
Gzip bool `yaml:"gzip"` Gzip bool `yaml:"gzip"`
} }