优化代码,及打印错误日志

This commit is contained in:
xing 2022-09-27 15:35:34 +08:00
parent 10efcc9711
commit 086de50dcf
6 changed files with 133 additions and 115 deletions

View File

@ -5,18 +5,15 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/cache"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/vars" "github/fthvgb1/wp-go/vars"
"log"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
) )
var PostsCache sync.Map var postContextCache *cache.MapCache[uint64, PostContext]
var PostContextCache sync.Map
var archivesCaches *Arch var archivesCaches *Arch
var categoryCaches *cache.SliceCache[models.WpTermsMy] var categoryCaches *cache.SliceCache[models.WpTermsMy]
var recentPostsCaches *cache.SliceCache[models.WpPosts] var recentPostsCaches *cache.SliceCache[models.WpPosts]
@ -29,6 +26,9 @@ func InitActionsCommonCache() {
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
setCacheFunc: archives, setCacheFunc: archives,
} }
postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime)
postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPosts, time.Hour) postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPosts, time.Hour)
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
@ -52,7 +52,7 @@ func (c *Arch) getArchiveCache() []models.PostArchive {
if l > 0 && c.month != m || l < 1 { if l > 0 && c.month != m || l < 1 {
r, err := c.setCacheFunc() r, err := c.setCacheFunc()
if err != nil { if err != nil {
log.Printf("set cache err[%s]", err) logs.ErrPrintln(err, "set cache err[%s]")
return nil return nil
} }
c.mutex.Lock() c.mutex.Lock()
@ -64,10 +64,8 @@ func (c *Arch) getArchiveCache() []models.PostArchive {
} }
type PostContext struct { type PostContext struct {
Prev models.WpPosts prev models.WpPosts
Next models.WpPosts next models.WpPosts
expireTime time.Duration
setTime time.Time
} }
func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) { func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) {
@ -86,7 +84,8 @@ func postComments(args ...any) ([]models.WpComments, error) {
} }
func RecentComments(ctx context.Context) (r []models.WpComments) { func RecentComments(ctx context.Context) (r []models.WpComments) {
r, _ = recentCommentsCaches.GetCache(ctx, time.Second) r, err := recentCommentsCaches.GetCache(ctx, time.Second)
logs.ErrPrintln(err, "get recent comment")
return return
} }
func recentComments(...any) (r []models.WpComments, err error) { func recentComments(...any) (r []models.WpComments, err error) {
@ -98,48 +97,30 @@ func recentComments(...any) (r []models.WpComments, err error) {
}, 5) }, 5)
} }
func getMonthPost(args ...any) ([]models.WpPosts, error) { func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next models.WpPosts, err error) {
y := args[0].(string) postCtx, err := postContextCache.GetCache(ctx, id, time.Second, date)
m := args[1].(string) if err != nil {
where := models.SqlBuilder{ return models.WpPosts{}, models.WpPosts{}, err
{"post_type", "in", ""},
{"post_status", "in", ""},
{"month(post_date)", m},
{"year(post_date)", y},
} }
return models.Find[models.WpPosts](where, "ID", "", prev = postCtx.prev
models.SqlBuilder{{"post_date", "asc"}}, next = postCtx.next
nil, 0, []any{"post"}, []any{"publish"},
)
}
func GetContextPost(id uint64, t time.Time) (prev, next models.WpPosts, err error) {
post, ok := PostContextCache.Load(id)
if ok {
c := post.(PostContext)
isExp := c.expireTime/time.Second+time.Duration(c.setTime.Unix()) < time.Duration(time.Now().Unix())
if !isExp && (c.Prev.Id > 0 || c.Next.Id > 0) {
return c.Prev, c.Next, nil
}
}
prev, next, err = getPostContext(t)
post = PostContext{
Prev: prev,
Next: next,
expireTime: vars.Conf.ContextPostCacheTime,
setTime: time.Now(),
}
PostContextCache.Store(id, post)
return return
} }
func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { func getPostContext(arg ...any) (r PostContext, err error) {
next, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ t := arg[0].(time.Time)
next, err := models.FirstOne[models.WpPosts](models.SqlBuilder{
{"post_date", ">", t.Format("2006-01-02 15:04:05")}, {"post_date", ">", t.Format("2006-01-02 15:04:05")},
{"post_status", "in", ""}, {"post_status", "in", ""},
{"post_type", "post"}, {"post_type", "post"},
}, "ID,post_title,post_password", nil, []any{"publish", "private"}) }, "ID,post_title,post_password", nil, []any{"publish", "private"})
prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ if err == sql.ErrNoRows {
err = nil
}
if err != nil {
return
}
prev, err := models.FirstOne[models.WpPosts](models.SqlBuilder{
{"post_date", "<", t.Format("2006-01-02 15:04:05")}, {"post_date", "<", t.Format("2006-01-02 15:04:05")},
{"post_status", "in", ""}, {"post_status", "in", ""},
{"post_type", "post"}, {"post_type", "post"},
@ -147,62 +128,12 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
err = nil err = nil
} }
return if err != nil {
} return
func QueryAndSetPostCache(postIds []models.WpPosts) (err error) {
var all []uint64
var needQuery []any
for _, wpPosts := range postIds {
all = append(all, wpPosts.Id)
if _, ok := PostsCache.Load(wpPosts.Id); !ok {
needQuery = append(needQuery, wpPosts.Id)
}
} }
if len(needQuery) > 0 { r = PostContext{
rawPosts, er := models.Find[models.WpPosts](models.SqlBuilder{{ prev: prev,
"Id", "in", "", next: next,
}}, "a.*,ifnull(d.name,'') category_name,ifnull(taxonomy,'') `taxonomy`", "", nil, models.SqlBuilder{{
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
}, {
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
}, {
"left join", "wp_terms d", "c.term_id=d.term_id",
}}, 0, needQuery)
if er != nil {
err = er
return
}
postsMap := make(map[uint64]*models.WpPosts)
for i, post := range rawPosts {
v, ok := postsMap[post.Id]
if !ok {
v = &rawPosts[i]
}
if post.Taxonomy == "category" {
v.Categories = append(v.Categories, post.CategoryName)
} else if post.Taxonomy == "post_tag" {
v.Tags = append(v.Tags, post.CategoryName)
}
postsMap[post.Id] = v
}
for _, pp := range postsMap {
if len(pp.Categories) > 0 {
t := make([]string, 0, len(pp.Categories))
for _, cat := range pp.Categories {
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
}
pp.CategoriesHtml = strings.Join(t, "、")
}
if len(pp.Tags) > 0 {
t := make([]string, 0, len(pp.Tags))
for _, cat := range pp.Tags {
t = append(t, fmt.Sprintf(`<a href="/p/tag/%s" rel="tag">%s</a>`, cat, cat))
}
pp.TagsHtml = strings.Join(t, "、")
}
PostsCache.Store(pp.Id, pp)
}
} }
return return
} }
@ -218,7 +149,8 @@ func Archives() (r []models.PostArchive) {
} }
func Categories(ctx context.Context) []models.WpTermsMy { func Categories(ctx context.Context) []models.WpTermsMy {
r, _ := categoryCaches.GetCache(ctx, time.Second) r, err := categoryCaches.GetCache(ctx, time.Second)
logs.ErrPrintln(err, "get category ")
return r return r
} }
@ -244,7 +176,8 @@ func categories(...any) (terms []models.WpTermsMy, err error) {
} }
func RecentPosts(ctx context.Context) (r []models.WpPosts) { func RecentPosts(ctx context.Context) (r []models.WpPosts) {
r, _ = recentPostsCaches.GetCache(ctx, time.Second) r, err := recentPostsCaches.GetCache(ctx, time.Second)
logs.ErrPrintln(err, "get recent post")
return return
} }
func recentPosts(...any) (r []models.WpPosts, err error) { func recentPosts(...any) (r []models.WpPosts, err error) {

View File

@ -2,16 +2,30 @@ package common
import ( import (
"context" "context"
"fmt"
"github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"strings"
"time" "time"
) )
func GetPostById(ctx context.Context, id uint64, ids ...uint64) (models.WpPosts, error) { func GetPostAndCache(ctx context.Context, id uint64, ids ...uint64) (models.WpPosts, error) {
return postsCache.GetCacheBatch(ctx, id, time.Second, ids) return postsCache.GetCacheBatch(ctx, id, time.Second, ids)
} }
func GetPost(id uint64) models.WpPosts {
return postsCache.Get(id)
}
func SetPostCache(ids []models.WpPosts) error {
var arg []uint64
for _, posts := range ids {
arg = append(arg, posts.Id)
}
return postsCache.SetByBatchFn(arg)
}
func getPosts(ids ...any) (m map[uint64]models.WpPosts, err error) { func getPosts(ids ...any) (m map[uint64]models.WpPosts, err error) {
m = make(map[uint64]models.WpPosts) m = make(map[uint64]models.WpPosts)
id := ids[0].([]uint64) id := ids[0].([]uint64)
@ -25,10 +39,38 @@ func getPosts(ids ...any) (m map[uint64]models.WpPosts, err error) {
}, { }, {
"left join", "wp_terms d", "c.term_id=d.term_id", "left join", "wp_terms d", "c.term_id=d.term_id",
}}, 0, arg) }}, 0, arg)
if err == nil { if err != nil {
for _, v := range rawPosts { return m, err
m[v.Id] = v }
postsMap := make(map[uint64]models.WpPosts)
for i, post := range rawPosts {
v, ok := postsMap[post.Id]
if !ok {
v = rawPosts[i]
} }
if post.Taxonomy == "category" {
v.Categories = append(v.Categories, post.CategoryName)
} else if post.Taxonomy == "post_tag" {
v.Tags = append(v.Tags, post.CategoryName)
}
postsMap[post.Id] = v
}
for k, pp := range postsMap {
if len(pp.Categories) > 0 {
t := make([]string, 0, len(pp.Categories))
for _, cat := range pp.Categories {
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
}
pp.CategoriesHtml = strings.Join(t, "、")
}
if len(pp.Tags) > 0 {
t := make([]string, 0, len(pp.Tags))
for _, cat := range pp.Tags {
t = append(t, fmt.Sprintf(`<a href="/p/tag/%s" rel="tag">%s</a>`, cat, cat))
}
pp.TagsHtml = strings.Join(t, "、")
}
m[k] = pp
} }
return return
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/plugins" "github/fthvgb1/wp-go/plugins"
"math/rand" "math/rand"
@ -55,7 +56,7 @@ func Detail(c *gin.Context) {
} }
} }
ID := uint64(Id) ID := uint64(Id)
post, err := common.GetPostById(c, ID, ID) post, err := common.GetPostAndCache(c, ID, ID)
if post.Id == 0 || err != nil { if post.Id == 0 || err != nil {
return return
} }
@ -71,8 +72,10 @@ func Detail(c *gin.Context) {
} }
plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post) plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post)
comments, err := common.PostComments(c, post.Id) comments, err := common.PostComments(c, post.Id)
logs.ErrPrintln(err, "get post comment", post.Id)
commentss := treeComments(comments) commentss := treeComments(comments)
prev, next, err := common.GetContextPost(post.Id, post.PostDate) prev, next, err := common.GetContextPost(c, post.Id, post.PostDate)
logs.ErrPrintln(err, "get pre and next post", post.Id, post.PostDate)
h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]) h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
h["post"] = post h["post"] = post
h["showComment"] = showComment h["showComment"] = showComment

View File

@ -181,23 +181,23 @@ func Index(c *gin.Context) {
if len(postIds) < 1 && h.category != "" { if len(postIds) < 1 && h.category != "" {
h.titleL = "未找到页面" h.titleL = "未找到页面"
} }
err = common.QueryAndSetPostCache(postIds) err = common.SetPostCache(postIds)
if err != nil {
return
}
pw := h.session.Get("post_password") pw := h.session.Get("post_password")
plug := plugins.NewPostPlugin(c, h.scene) plug := plugins.NewPostPlugin(c, h.scene)
for i, v := range postIds { for i, v := range postIds {
post, _ := common.PostsCache.Load(v.Id) post := common.GetPost(v.Id)
pp := post.(*models.WpPosts) postIds[i] = post
px := *pp
postIds[i] = px
common.PasswordProjectTitle(&postIds[i]) common.PasswordProjectTitle(&postIds[i])
if px.PostPassword != "" && pw != px.PostPassword { if post.PostPassword != "" && pw != post.PostPassword {
common.PasswdProjectContent(&postIds[i]) common.PasswdProjectContent(&postIds[i])
} else { } else {
plugins.ApplyPlugin(plug, &postIds[i]) plugins.ApplyPlugin(plug, &postIds[i])
} }
} }
for i, post := range recent { for i, post := range recent {
if post.PostPassword != "" && pw != post.PostPassword { if post.PostPassword != "" && pw != post.PostPassword {
common.PasswdProjectContent(&recent[i]) common.PasswdProjectContent(&recent[i])
} }

14
cache/map.go vendored
View File

@ -56,6 +56,20 @@ func (m *MapCache[K, V]) Set(k K, v V) {
m.set(k, v) m.set(k, v)
} }
func (m *MapCache[K, V]) SetByBatchFn(params ...any) error {
m.mutex.Lock()
defer m.mutex.Unlock()
r, err := m.setBatchCacheFn(params...)
if err != nil {
return err
}
for k, v := range r {
m.set(k, v)
}
return nil
}
func (m *MapCache[K, V]) set(k K, v V) { func (m *MapCache[K, V]) set(k K, v V) {
data, ok := m.data[k] data, ok := m.data[k]
t := time.Now() t := time.Now()

26
logs/log.go Normal file
View File

@ -0,0 +1,26 @@
package logs
import (
"log"
"strings"
)
func ErrPrintln(err error, desc string, args ...any) {
s := strings.Builder{}
tmp := "%s err:[%s]"
if desc == "" {
tmp = "%s%s"
}
s.WriteString(tmp)
argss := []any{desc, err}
if len(args) > 0 {
s.WriteString(" args:")
for _, arg := range args {
s.WriteString("%v ")
argss = append(argss, arg)
}
}
if err != nil {
log.Printf(s.String(), argss...)
}
}