优化代码

This commit is contained in:
xing 2023-02-05 21:06:04 +08:00
parent eee0b75d19
commit 907d5be596
13 changed files with 99 additions and 87 deletions

View File

@ -27,30 +27,40 @@ ssl:
cert: "" cert: ""
key: "" key: ""
# 最近文章缓存时间 cacheTime:
recentPostCacheTime: 5m # 最近文章缓存时间
# 分类缓存时间 recentPostCacheTime: 5m
categoryCacheTime: 5m # 分类缓存时间
# 上下篇缓存时间 categoryCacheTime: 5m
contextPostCacheTime: 10h # 上下篇缓存时间
# 最近评论缓存时间 contextPostCacheTime: 10h
recentCommentsCacheTime: 5m # 最近评论缓存时间
# 摘要缓存时间 recentCommentsCacheTime: 5m
digestCacheTime: 5m # 摘要缓存时间
digestCacheTime: 5m
# 文档列表id页缓存 包括默认列表、分类
postListCacheTime: 1h
# 搜索文档id缓存时间
searchPostCacheTime: 5m
# 月归档文章id缓存时间
monthPostCacheTime: 1h
# 文档数据缓存时间
postDataCacheTime: 1h
# 文章评论缓存时间
postCommentsCacheTime: 5m
# 定时清理缓存周期时间
crontabClearCacheTime: 5m
# 文档最大id缓存时间
maxPostIdCacheTime: 1h
# 用户信息缓存时间
userInfoCacheTime: 24h
# 单独评论缓存时间
commentsCacheTime: 24h
# 主题的页眉图片缓存时间
themeHeaderImagCacheTime: 5m
# 摘要字数 # 摘要字数
digestWordCount: 300 digestWordCount: 300
# 文档列表id页缓存 包括默认列表、分类
postListCacheTime: 1h
# 搜索文档id缓存时间
searchPostCacheTime: 5m
# 月归档文章id缓存时间
monthPostCacheTime: 1h
# 文档数据缓存时间
postDataCacheTime: 1h
# 文章评论缓存时间
postCommentsCacheTime: 5m
# 定时清理缓存周期时间
crontabClearCacheTime: 5m
# 到达指定并发请求数时随机sleep # 到达指定并发请求数时随机sleep
maxRequestSleepNum: 100 maxRequestSleepNum: 100
# 随机sleep时间 # 随机sleep时间
@ -59,14 +69,7 @@ sleepTime: [1s,3s]
maxRequestNum: 500 maxRequestNum: 500
# 单ip同时最大搜索请求数 # 单ip同时最大搜索请求数
singleIpSearchNum: 10 singleIpSearchNum: 10
# 文档最大id缓存时间
maxPostIdCacheTime: 1h
# 用户信息缓存时间
userInfoCacheTime: 24h
# 单独评论缓存时间
commentsCacheTime: 24h
# 主题的页眉图片缓存时间
themeHeaderImagCacheTime: 5m
# Gzip # Gzip
gzip: false gzip: false

View File

@ -34,6 +34,7 @@ func PostComment(c *gin.Context) {
c.Writer.WriteString(err.Error()) c.Writer.WriteString(err.Error())
} }
}() }()
conf := config.GetConfig()
if err != nil { if err != nil {
return return
} }
@ -43,7 +44,7 @@ func PostComment(c *gin.Context) {
m := c.PostForm("email") m := c.PostForm("email")
comment := c.PostForm("comment") comment := c.PostForm("comment")
c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
req, err := http.NewRequest("POST", config.Conf.Load().PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) req, err := http.NewRequest("POST", conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode()))
if err != nil { if err != nil {
return return
} }
@ -68,7 +69,7 @@ func PostComment(c *gin.Context) {
err = er err = er
return return
} }
cu, er := url.Parse(config.Conf.Load().PostCommentUrl) cu, er := url.Parse(conf.PostCommentUrl)
if er != nil { if er != nil {
err = er err = er
return return
@ -102,8 +103,8 @@ func PostComment(c *gin.Context) {
return return
} }
su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.Options.Value("siteurl"), author, m, post.PostTitle) su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.Options.Value("siteurl"), author, m, post.PostTitle)
err = mail.SendMail([]string{config.Conf.Load().Mail.User}, su, comment) err = mail.SendMail([]string{conf.Mail.User}, su, comment)
logs.ErrPrintln(err, "发送邮件", config.Conf.Load().Mail.User, su, comment) logs.ErrPrintln(err, "发送邮件", conf.Mail.User, su, comment)
}() }()
s, er := io.ReadAll(ress.Body) s, er := io.ReadAll(ress.Body)

View File

@ -99,7 +99,7 @@ func (h *indexHandle) parseParams() (err error) {
h.order = h.c.Query("order") h.order = h.c.Query("order")
if !slice.IsContained(h.order, orders) { if !slice.IsContained(h.order, orders) {
order := config.Conf.Load().PostOrder order := config.GetConfig().PostOrder
h.order = "asc" h.order = "asc"
if order != "" && slice.IsContained(order, orders) { if order != "" && slice.IsContained(order, orders) {
h.order = order h.order = order

View File

@ -72,7 +72,7 @@ func initConf(c string) (err error) {
} }
func cronClearCache() { func cronClearCache() {
t := time.NewTicker(config.Conf.Load().CrontabClearCacheTime) t := time.NewTicker(config.GetConfig().CacheTime.CrontabClearCacheTime)
for { for {
select { select {
case <-t.C: case <-t.C:
@ -85,7 +85,7 @@ func cronClearCache() {
func flushCache() { func flushCache() {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err := mail.SendMail([]string{config.Conf.Load().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r)) err := mail.SendMail([]string{config.GetConfig().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
logs.ErrPrintln(err, "发邮件失败") logs.ErrPrintln(err, "发邮件失败")
} }
}() }()
@ -129,7 +129,7 @@ func signalNotify() {
func main() { func main() {
go signalNotify() go signalNotify()
Gin, reloadFn := route.SetupRouter() Gin, reloadFn := route.SetupRouter()
c := config.Conf.Load() c := config.GetConfig()
middleWareReloadFn = reloadFn middleWareReloadFn = reloadFn
if c.Ssl.Key != "" && c.Ssl.Cert != "" { if c.Ssl.Key != "" && c.Ssl.Cert != "" {
err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key) err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key)

View File

@ -18,7 +18,7 @@ func SetupRouter() (*gin.Engine, func()) {
// Disable Console Color // Disable Console Color
// gin.DisableConsoleColor() // gin.DisableConsoleColor()
r := gin.New() r := gin.New()
c := config.Conf.Load() c := config.GetConfig()
if len(c.TrustIps) > 0 { if len(c.TrustIps) > 0 {
err := r.SetTrustedProxies(c.TrustIps) err := r.SetTrustedProxies(c.TrustIps)
if err != nil { if err != nil {
@ -29,7 +29,7 @@ func SetupRouter() (*gin.Engine, func()) {
r.HTMLRender = theme.GetTemplate() r.HTMLRender = theme.GetTemplate()
validServerName, reloadValidServerNameFn := middleware.ValidateServerNames() validServerName, reloadValidServerNameFn := middleware.ValidateServerNames()
fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
r.Use( r.Use(
gin.Logger(), gin.Logger(),
validServerName, validServerName,
@ -76,15 +76,15 @@ func SetupRouter() (*gin.Engine, func()) {
r.GET("/p/:id/feed", actions.PostFeed) r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed) r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed) r.GET("/comments/feed", actions.CommentsFeed)
cfl, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.SleepTime) cfl, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)
r.POST("/comment", cfl, actions.PostComment) r.POST("/comment", cfl, actions.PostComment)
if c.Pprof != "" { if c.Pprof != "" {
pprof.Register(r, c.Pprof) pprof.Register(r, c.Pprof)
} }
fn := func() { fn := func() {
reloadValidServerNameFn() reloadValidServerNameFn()
c := config.Conf.Load() c := config.GetConfig()
flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
slRload(c.SingleIpSearchNum) slRload(c.SingleIpSearchNum)
} }
return r, fn return r, fn

View File

@ -18,7 +18,7 @@ func SendMail(mailTo []string, subject string, body string, files ...string) err
m := gomail.NewMessage( m := gomail.NewMessage(
gomail.SetEncoding(gomail.Base64), gomail.SetEncoding(gomail.Base64),
) )
c := config.Conf.Load() c := config.GetConfig()
m.SetHeader("From", m.SetHeader("From",
m.FormatAddress(c.Mail.User, m.FormatAddress(c.Mail.User,
c.Mail.Alias, c.Mail.Alias,

View File

@ -43,7 +43,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) {
) )
er := mail.SendMail( er := mail.SendMail(
[]string{config.Conf.Load().Mail.User}, []string{config.GetConfig().Mail.User},
fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wpconfig.Options.Value("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content) fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wpconfig.Options.Value("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content)
if er != nil { if er != nil {

View File

@ -11,7 +11,7 @@ import (
func ValidateServerNames() (func(ctx *gin.Context), func()) { func ValidateServerNames() (func(ctx *gin.Context), func()) {
var serverName safety.Map[string, struct{}] var serverName safety.Map[string, struct{}]
fn := func() { fn := func() {
r := config.Conf.Load().TrustServerNames r := config.GetConfig().TrustServerNames
if len(r) > 0 { if len(r) > 0 {
for _, name := range r { for _, name := range r {
serverName.Store(name, struct{}{}) serverName.Store(name, struct{}{})

View File

@ -48,39 +48,43 @@ var headerImagesCache *cache.MapCache[string, []models.PostThumbnail]
var ctx context.Context var ctx context.Context
func InitActionsCommonCache() { func InitActionsCommonCache() {
c := config.Conf.Load() c := config.GetConfig()
archivesCaches = &Arch{ archivesCaches = &Arch{
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
setCacheFunc: dao.Archives, setCacheFunc: dao.Archives,
} }
searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.SearchPostCacheTime) searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
postListIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.PostListCacheTime) postListIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.PostListCacheTime)
monthPostsCache = cache.NewMemoryMapCacheByFn[string](dao.MonthPost, c.MonthPostCacheTime) monthPostsCache = cache.NewMemoryMapCacheByFn[string](dao.MonthPost, c.CacheTime.MonthPostCacheTime)
postContextCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetPostContext, c.ContextPostCacheTime) postContextCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetPostContext, c.CacheTime.ContextPostCacheTime)
postsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostsByIds, c.PostDataCacheTime) postsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostsByIds, c.CacheTime.PostDataCacheTime)
postMetaCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.PostDataCacheTime) postMetaCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime)
categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CategoryCacheTime) categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime)
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.RecentPostCacheTime) recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime)
recentCommentsCaches = cache.NewVarCache(dao.RecentComments, c.RecentCommentsCacheTime) recentCommentsCaches = cache.NewVarCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime)
postCommentCaches = cache.NewMemoryMapCacheByFn[uint64](dao.PostComments, c.PostCommentsCacheTime) postCommentCaches = cache.NewMemoryMapCacheByFn[uint64](dao.PostComments, c.CacheTime.PostCommentsCacheTime)
maxPostIdCache = cache.NewVarCache(dao.GetMaxPostId, c.MaxPostIdCacheTime) maxPostIdCache = cache.NewVarCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime)
usersCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetUserById, c.UserInfoCacheTime) usersCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetUserById, c.CacheTime.UserInfoCacheTime)
usersNameCache = cache.NewMemoryMapCacheByFn[string](dao.GetUserByName, c.UserInfoCacheTime) usersNameCache = cache.NewMemoryMapCacheByFn[string](dao.GetUserByName, c.CacheTime.UserInfoCacheTime)
commentsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetCommentByIds, c.CommentsCacheTime) commentsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetCommentByIds, c.CacheTime.CommentsCacheTime)
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime)
headerImagesCache = cache.NewMemoryMapCacheByFn[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime)
feedCache = cache.NewVarCache(feed, time.Hour) feedCache = cache.NewVarCache(feed, time.Hour)
@ -90,10 +94,6 @@ func InitActionsCommonCache() {
newCommentCache = cache.NewMemoryMapCacheByFn[string, string](nil, 15*time.Minute) newCommentCache = cache.NewMemoryMapCacheByFn[string, string](nil, 15*time.Minute)
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.UserInfoCacheTime)
headerImagesCache = cache.NewMemoryMapCacheByFn[string](getHeaderImages, c.ThemeHeaderImagCacheTime)
ctx = context.Background() ctx = context.Background()
InitFeed() InitFeed()

View File

@ -8,41 +8,49 @@ import (
"time" "time"
) )
var Conf safety.Var[Config] var config safety.Var[Config]
func GetConfig() Config {
return config.Load()
}
type Config struct { type Config struct {
Ssl Ssl `yaml:"ssl"` Ssl Ssl `yaml:"ssl"`
Mysql Mysql `yaml:"mysql"` Mysql Mysql `yaml:"mysql"`
Mail Mail `yaml:"mail"` Mail Mail `yaml:"mail"`
CacheTime CacheTime `yaml:"cacheTime"`
DigestWordCount int `yaml:"digestWordCount"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
MaxRequestNum int64 `yaml:"maxRequestNum"`
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
Gzip bool `yaml:"gzip"`
PostCommentUrl string `yaml:"postCommentUrl"`
TrustIps []string `yaml:"trustIps"`
TrustServerNames []string `yaml:"trustServerNames"`
Theme string `yaml:"theme"`
PostOrder string `yaml:"postOrder"`
UploadDir string `yaml:"uploadDir"`
Pprof string `yaml:"pprof"`
}
type CacheTime struct {
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
DigestCacheTime time.Duration `yaml:"digestCacheTime"` DigestCacheTime time.Duration `yaml:"digestCacheTime"`
DigestWordCount int `yaml:"digestWordCount"`
PostListCacheTime time.Duration `yaml:"postListCacheTime"` PostListCacheTime time.Duration `yaml:"postListCacheTime"`
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"`
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
SleepTime []time.Duration `yaml:"sleepTime"`
MaxRequestNum int64 `yaml:"maxRequestNum"`
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"` CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"` ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"`
Gzip bool `yaml:"gzip"` SleepTime []time.Duration `yaml:"sleepTime"`
PostCommentUrl string `yaml:"postCommentUrl"`
TrustIps []string `yaml:"trustIps"`
TrustServerNames []string `yaml:"trustServerNames"`
Theme string `yaml:"theme"`
PostOrder string `yaml:"postOrder"`
UploadDir string `yaml:"uploadDir"`
Pprof string `yaml:"pprof"`
} }
type Ssl struct { type Ssl struct {
@ -77,7 +85,7 @@ func InitConfig(conf string) error {
if err != nil { if err != nil {
return err return err
} }
Conf.Store(c) config.Store(c)
return nil return nil
} }

View File

@ -55,7 +55,7 @@ func formatSql(sql string, params []any) string {
} }
func InitDb() error { func InitDb() error {
c := config.Conf.Load() c := config.GetConfig()
dsn := c.Mysql.Dsn.GetDsn() dsn := c.Mysql.Dsn.GetDsn()
var err error var err error
Db, err = sqlx.Open("mysql", dsn) Db, err = sqlx.Open("mysql", dsn)

View File

@ -17,7 +17,7 @@ var ctx context.Context
func InitDigestCache() { func InitDigestCache() {
ctx = context.Background() ctx = context.Background()
digestCache = cache.NewMemoryMapCacheByFn[uint64](digestRaw, config.Conf.Load().DigestCacheTime) digestCache = cache.NewMemoryMapCacheByFn[uint64](digestRaw, config.GetConfig().CacheTime.DigestCacheTime)
} }
func ClearDigestCache() { func ClearDigestCache() {
@ -30,7 +30,7 @@ func FlushCache() {
func digestRaw(arg ...any) (string, error) { func digestRaw(arg ...any) (string, error) {
str := arg[0].(string) str := arg[0].(string)
id := arg[1].(uint64) id := arg[1].(uint64)
limit := config.Conf.Load().DigestWordCount limit := config.GetConfig().DigestWordCount
if limit < 0 { if limit < 0 {
return str, nil return str, nil
} else if limit == 0 { } else if limit == 0 {

View File

@ -11,7 +11,7 @@ func InitThemeAndTemplateFuncMap() {
} }
func GetTemplateName() string { func GetTemplateName() string {
tmlp := config.Conf.Load().Theme tmlp := config.GetConfig().Theme
if tmlp == "" { if tmlp == "" {
tmlp = wpconfig.Options.Value("template") tmlp = wpconfig.Options.Value("template")
} }