From 907d5be5961bbe6b03871851c59d77b2bb9b136c Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 5 Feb 2023 21:06:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 63 ++++++++++++----------- internal/actions/comment.go | 9 ++-- internal/actions/index.go | 2 +- internal/cmd/main.go | 6 +-- internal/cmd/route/route.go | 10 ++-- internal/mail/mail.go | 2 +- internal/middleware/sendmail.go | 2 +- internal/middleware/validateservername.go | 2 +- internal/pkg/cache/cache.go | 38 +++++++------- internal/pkg/config/config.go | 44 +++++++++------- internal/pkg/db/db.go | 2 +- internal/plugins/digest.go | 4 +- internal/theme/theme.go | 2 +- 13 files changed, 99 insertions(+), 87 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 18f4ef1..64d9631 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -27,30 +27,40 @@ ssl: cert: "" key: "" -# 最近文章缓存时间 -recentPostCacheTime: 5m -# 分类缓存时间 -categoryCacheTime: 5m -# 上下篇缓存时间 -contextPostCacheTime: 10h -# 最近评论缓存时间 -recentCommentsCacheTime: 5m -# 摘要缓存时间 -digestCacheTime: 5m +cacheTime: + # 最近文章缓存时间 + recentPostCacheTime: 5m + # 分类缓存时间 + categoryCacheTime: 5m + # 上下篇缓存时间 + contextPostCacheTime: 10h + # 最近评论缓存时间 + recentCommentsCacheTime: 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 -# 文档列表id页缓存 包括默认列表、分类 -postListCacheTime: 1h -# 搜索文档id缓存时间 -searchPostCacheTime: 5m -# 月归档文章id缓存时间 -monthPostCacheTime: 1h -# 文档数据缓存时间 -postDataCacheTime: 1h -# 文章评论缓存时间 -postCommentsCacheTime: 5m -# 定时清理缓存周期时间 -crontabClearCacheTime: 5m + # 到达指定并发请求数时随机sleep maxRequestSleepNum: 100 # 随机sleep时间 @@ -59,14 +69,7 @@ sleepTime: [1s,3s] maxRequestNum: 500 # 单ip同时最大搜索请求数 singleIpSearchNum: 10 -# 文档最大id缓存时间 -maxPostIdCacheTime: 1h -# 用户信息缓存时间 -userInfoCacheTime: 24h -# 单独评论缓存时间 -commentsCacheTime: 24h -# 主题的页眉图片缓存时间 -themeHeaderImagCacheTime: 5m + # Gzip gzip: false diff --git a/internal/actions/comment.go b/internal/actions/comment.go index 40a7079..846bdca 100644 --- a/internal/actions/comment.go +++ b/internal/actions/comment.go @@ -34,6 +34,7 @@ func PostComment(c *gin.Context) { c.Writer.WriteString(err.Error()) } }() + conf := config.GetConfig() if err != nil { return } @@ -43,7 +44,7 @@ func PostComment(c *gin.Context) { m := c.PostForm("email") comment := c.PostForm("comment") 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 { return } @@ -68,7 +69,7 @@ func PostComment(c *gin.Context) { err = er return } - cu, er := url.Parse(config.Conf.Load().PostCommentUrl) + cu, er := url.Parse(conf.PostCommentUrl) if er != nil { err = er return @@ -102,8 +103,8 @@ func PostComment(c *gin.Context) { return } 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) - logs.ErrPrintln(err, "发送邮件", config.Conf.Load().Mail.User, su, comment) + err = mail.SendMail([]string{conf.Mail.User}, su, comment) + logs.ErrPrintln(err, "发送邮件", conf.Mail.User, su, comment) }() s, er := io.ReadAll(ress.Body) diff --git a/internal/actions/index.go b/internal/actions/index.go index 1336a86..47c6b2a 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -99,7 +99,7 @@ func (h *indexHandle) parseParams() (err error) { h.order = h.c.Query("order") if !slice.IsContained(h.order, orders) { - order := config.Conf.Load().PostOrder + order := config.GetConfig().PostOrder h.order = "asc" if order != "" && slice.IsContained(order, orders) { h.order = order diff --git a/internal/cmd/main.go b/internal/cmd/main.go index b65c393..4165dcd 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -72,7 +72,7 @@ func initConf(c string) (err error) { } func cronClearCache() { - t := time.NewTicker(config.Conf.Load().CrontabClearCacheTime) + t := time.NewTicker(config.GetConfig().CacheTime.CrontabClearCacheTime) for { select { case <-t.C: @@ -85,7 +85,7 @@ func cronClearCache() { func flushCache() { defer func() { 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, "发邮件失败") } }() @@ -129,7 +129,7 @@ func signalNotify() { func main() { go signalNotify() Gin, reloadFn := route.SetupRouter() - c := config.Conf.Load() + c := config.GetConfig() middleWareReloadFn = reloadFn if c.Ssl.Key != "" && c.Ssl.Cert != "" { err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key) diff --git a/internal/cmd/route/route.go b/internal/cmd/route/route.go index 2763e11..78ff6a5 100644 --- a/internal/cmd/route/route.go +++ b/internal/cmd/route/route.go @@ -18,7 +18,7 @@ func SetupRouter() (*gin.Engine, func()) { // Disable Console Color // gin.DisableConsoleColor() r := gin.New() - c := config.Conf.Load() + c := config.GetConfig() if len(c.TrustIps) > 0 { err := r.SetTrustedProxies(c.TrustIps) if err != nil { @@ -29,7 +29,7 @@ func SetupRouter() (*gin.Engine, func()) { r.HTMLRender = theme.GetTemplate() 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( gin.Logger(), validServerName, @@ -76,15 +76,15 @@ func SetupRouter() (*gin.Engine, func()) { r.GET("/p/:id/feed", actions.PostFeed) r.GET("/feed", actions.Feed) 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) if c.Pprof != "" { pprof.Register(r, c.Pprof) } fn := func() { reloadValidServerNameFn() - c := config.Conf.Load() - flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime) + c := config.GetConfig() + flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) slRload(c.SingleIpSearchNum) } return r, fn diff --git a/internal/mail/mail.go b/internal/mail/mail.go index 81595aa..45dc867 100644 --- a/internal/mail/mail.go +++ b/internal/mail/mail.go @@ -18,7 +18,7 @@ func SendMail(mailTo []string, subject string, body string, files ...string) err m := gomail.NewMessage( gomail.SetEncoding(gomail.Base64), ) - c := config.Conf.Load() + c := config.GetConfig() m.SetHeader("From", m.FormatAddress(c.Mail.User, c.Mail.Alias, diff --git a/internal/middleware/sendmail.go b/internal/middleware/sendmail.go index 0870d1f..aded58d 100644 --- a/internal/middleware/sendmail.go +++ b/internal/middleware/sendmail.go @@ -43,7 +43,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) { ) 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) if er != nil { diff --git a/internal/middleware/validateservername.go b/internal/middleware/validateservername.go index 444b80d..6e3f261 100644 --- a/internal/middleware/validateservername.go +++ b/internal/middleware/validateservername.go @@ -11,7 +11,7 @@ import ( func ValidateServerNames() (func(ctx *gin.Context), func()) { var serverName safety.Map[string, struct{}] fn := func() { - r := config.Conf.Load().TrustServerNames + r := config.GetConfig().TrustServerNames if len(r) > 0 { for _, name := range r { serverName.Store(name, struct{}{}) diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 710f4b5..6fb8d46 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -48,39 +48,43 @@ var headerImagesCache *cache.MapCache[string, []models.PostThumbnail] var ctx context.Context func InitActionsCommonCache() { - c := config.Conf.Load() + c := config.GetConfig() archivesCaches = &Arch{ mutex: &sync.Mutex{}, 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) @@ -90,10 +94,6 @@ func InitActionsCommonCache() { 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() InitFeed() diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index ea0d070..81c30a1 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -8,41 +8,49 @@ import ( "time" ) -var Conf safety.Var[Config] +var config safety.Var[Config] + +func GetConfig() Config { + return config.Load() +} type Config struct { - Ssl Ssl `yaml:"ssl"` - Mysql Mysql `yaml:"mysql"` - Mail Mail `yaml:"mail"` + Ssl Ssl `yaml:"ssl"` + Mysql Mysql `yaml:"mysql"` + 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"` CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` DigestCacheTime time.Duration `yaml:"digestCacheTime"` - DigestWordCount int `yaml:"digestWordCount"` PostListCacheTime time.Duration `yaml:"postListCacheTime"` SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` 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"` UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"` - 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"` + SleepTime []time.Duration `yaml:"sleepTime"` } type Ssl struct { @@ -77,7 +85,7 @@ func InitConfig(conf string) error { if err != nil { return err } - Conf.Store(c) + config.Store(c) return nil } diff --git a/internal/pkg/db/db.go b/internal/pkg/db/db.go index a7196c9..7e64406 100644 --- a/internal/pkg/db/db.go +++ b/internal/pkg/db/db.go @@ -55,7 +55,7 @@ func formatSql(sql string, params []any) string { } func InitDb() error { - c := config.Conf.Load() + c := config.GetConfig() dsn := c.Mysql.Dsn.GetDsn() var err error Db, err = sqlx.Open("mysql", dsn) diff --git a/internal/plugins/digest.go b/internal/plugins/digest.go index 90311ce..d2d97fd 100644 --- a/internal/plugins/digest.go +++ b/internal/plugins/digest.go @@ -17,7 +17,7 @@ var ctx context.Context func InitDigestCache() { ctx = context.Background() - digestCache = cache.NewMemoryMapCacheByFn[uint64](digestRaw, config.Conf.Load().DigestCacheTime) + digestCache = cache.NewMemoryMapCacheByFn[uint64](digestRaw, config.GetConfig().CacheTime.DigestCacheTime) } func ClearDigestCache() { @@ -30,7 +30,7 @@ func FlushCache() { func digestRaw(arg ...any) (string, error) { str := arg[0].(string) id := arg[1].(uint64) - limit := config.Conf.Load().DigestWordCount + limit := config.GetConfig().DigestWordCount if limit < 0 { return str, nil } else if limit == 0 { diff --git a/internal/theme/theme.go b/internal/theme/theme.go index bc0e23a..f38d3fb 100644 --- a/internal/theme/theme.go +++ b/internal/theme/theme.go @@ -11,7 +11,7 @@ func InitThemeAndTemplateFuncMap() { } func GetTemplateName() string { - tmlp := config.Conf.Load().Theme + tmlp := config.GetConfig().Theme if tmlp == "" { tmlp = wpconfig.Options.Value("template") }