优化代码

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: ""
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,33 +8,21 @@ 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"`
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"`
CacheTime CacheTime `yaml:"cacheTime"`
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"`
@ -45,6 +33,26 @@ type Config struct {
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"`
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"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"`
SleepTime []time.Duration `yaml:"sleepTime"`
}
type Ssl struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
@ -77,7 +85,7 @@ func InitConfig(conf string) error {
if err != nil {
return err
}
Conf.Store(c)
config.Store(c)
return nil
}

View File

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

View File

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

View File

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