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)
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 {

View File

@ -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

View File

@ -3,6 +3,7 @@ package rss2
import (
"fmt"
"github/fthvgb1/wp-go/helper"
"strconv"
"strings"
)
@ -38,19 +39,28 @@ var template = `<?xml version="1.0" encoding="UTF-8"?>
var templateItems = `
<item>
<title>{$title}</title>
<link>{$link}</link>
{$link}
{$comments}
<dc:creator><![CDATA[{$author}]]></dc:creator>
{$creator}
<pubDate>{$pubDate}</pubDate>
{$category}
<guid isPermaLink="false">{$guid}</guid>
{$guid}
{$description}
<content:encoded><![CDATA[{$content}]]></content:encoded>
{$commentRss}
{$commentNumber}
</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 {
Title string
@ -104,53 +114,40 @@ 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(),
} {
xml = strings.Replace(xml, k, v, -1)
}
return
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,
}
func (i Item) getCategory() string {
r := ""
if i.Category != "" {
r = fmt.Sprintf("<category><![CDATA[%s]]></category>", i.CommentLink)
if i.CommentRss != "" && i.SlashComments > 0 {
m["{$commentRss}"] = i.CommentRss
} else {
m["{$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.CommentLink != "" && i.SlashComments > 0 {
r = fmt.Sprintf("<wfw:commentRss>%s</wfw:commentRss>", i.CommentRss)
}
return
}
func (i Item) getSlashComments() (r string) {
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
}

View File

@ -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"`
}