优化代码
This commit is contained in:
parent
b453910f82
commit
562520c080
|
@ -21,7 +21,7 @@ type detailHandler struct {
|
||||||
func Detail(c *gin.Context) {
|
func Detail(c *gin.Context) {
|
||||||
var err error
|
var err error
|
||||||
var post models.Posts
|
var post models.Posts
|
||||||
recent := cache.RecentPosts(c, 5)
|
recent := cache.RecentPosts(c, 5, true)
|
||||||
archive := cache.Archives(c)
|
archive := cache.Archives(c)
|
||||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||||
recentComments := cache.RecentComments(c, 5)
|
recentComments := cache.RecentComments(c, 5)
|
||||||
|
|
|
@ -220,7 +220,7 @@ func Index(c *gin.Context) {
|
||||||
var totalRaw int
|
var totalRaw int
|
||||||
var err error
|
var err error
|
||||||
archive := cache.Archives(c)
|
archive := cache.Archives(c)
|
||||||
recent := cache.RecentPosts(c, 5)
|
recent := cache.RecentPosts(c, 5, true)
|
||||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||||
recentComments := cache.RecentComments(c, 5)
|
recentComments := cache.RecentComments(c, 5)
|
||||||
ginH := gin.H{
|
ginH := gin.H{
|
||||||
|
|
2
internal/pkg/cache/feed.go
vendored
2
internal/pkg/cache/feed.go
vendored
|
@ -46,7 +46,7 @@ func PostFeedCache() *cache.MapCache[string, string] {
|
||||||
|
|
||||||
func feed(arg ...any) (xml []string, err error) {
|
func feed(arg ...any) (xml []string, err error) {
|
||||||
c := arg[0].(*gin.Context)
|
c := arg[0].(*gin.Context)
|
||||||
r := RecentPosts(c, 10)
|
r := RecentPosts(c, 10, true)
|
||||||
ids := slice.Map(r, func(t models.Posts) uint64 {
|
ids := slice.Map(r, func(t models.Posts) uint64 {
|
||||||
return t.Id
|
return t.Id
|
||||||
})
|
})
|
||||||
|
|
17
internal/pkg/cache/posts.go
vendored
17
internal/pkg/cache/posts.go
vendored
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -42,11 +43,23 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) {
|
||||||
return maxPostIdCache.GetCache(ctx, time.Second, ctx)
|
return maxPostIdCache.GetCache(ctx, time.Second, ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
|
func RecentPosts(ctx context.Context, n int, password bool) (r []models.Posts) {
|
||||||
r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx)
|
nn := n
|
||||||
|
if nn <= 5 {
|
||||||
|
nn = 10
|
||||||
|
}
|
||||||
|
r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx, nn)
|
||||||
if n < len(r) {
|
if n < len(r) {
|
||||||
r = r[:n]
|
r = r[:n]
|
||||||
}
|
}
|
||||||
|
if password {
|
||||||
|
r = slice.Map(r, func(t models.Posts) models.Posts {
|
||||||
|
if t.PostPassword != "" {
|
||||||
|
plugins.PasswordProjectTitle(&t)
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
})
|
||||||
|
}
|
||||||
logs.ErrPrintln(err, "get recent post")
|
logs.ErrPrintln(err, "get recent post")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/fthvgb1/wp-go/model"
|
"github.com/fthvgb1/wp-go/model"
|
||||||
|
@ -20,23 +19,21 @@ type PostContext struct {
|
||||||
Next models.Posts
|
Next models.Posts
|
||||||
}
|
}
|
||||||
|
|
||||||
func PasswordProjectTitle(post *models.Posts) {
|
|
||||||
if post.PostPassword != "" {
|
|
||||||
post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
||||||
ctx := a[0].(context.Context)
|
ctx := a[0].(context.Context)
|
||||||
var in = []any{"category", "post_tag"}
|
var in = []any{"category", "post_tag"}
|
||||||
terms, err = model.Find[models.TermsMy](ctx, model.SqlBuilder{
|
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
|
||||||
|
model.Where(model.SqlBuilder{
|
||||||
{"tt.count", ">", "0", "int"},
|
{"tt.count", ">", "0", "int"},
|
||||||
{"tt.taxonomy", "in", ""},
|
{"tt.taxonomy", "in", ""},
|
||||||
}, "t.term_id", "", model.SqlBuilder{
|
}),
|
||||||
{"t.name", "asc"},
|
model.Fields("t.term_id"),
|
||||||
}, model.SqlBuilder{
|
model.Order(model.SqlBuilder{{"t.name", "asc"}}),
|
||||||
|
model.Join(model.SqlBuilder{
|
||||||
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
|
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
|
||||||
}, nil, 0, in)
|
}),
|
||||||
|
model.In(in),
|
||||||
|
))
|
||||||
for i := 0; i < len(terms); i++ {
|
for i := 0; i < len(terms); i++ {
|
||||||
if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok {
|
if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok {
|
||||||
terms[i].Terms = v
|
terms[i].Terms = v
|
||||||
|
@ -49,7 +46,13 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Archives(ctx context.Context) ([]models.PostArchive, error) {
|
func Archives(ctx context.Context) ([]models.PostArchive, error) {
|
||||||
return model.Find[models.PostArchive](ctx, model.SqlBuilder{
|
return model.Finds[models.PostArchive](ctx, model.Conditions(
|
||||||
{"post_type", "post"}, {"post_status", "publish"},
|
model.Where(model.SqlBuilder{
|
||||||
}, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", model.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, nil, 0)
|
{"post_type", "post"},
|
||||||
|
{"post_status", "publish"},
|
||||||
|
}),
|
||||||
|
model.Fields("YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts"),
|
||||||
|
model.Group("year,month"),
|
||||||
|
model.Order(model.SqlBuilder{{"year", "desc"}, {"month", "desc"}}),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,10 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error)
|
||||||
r = make(map[uint64]map[string]any)
|
r = make(map[uint64]map[string]any)
|
||||||
ctx := args[0].(context.Context)
|
ctx := args[0].(context.Context)
|
||||||
ids := args[1].([]uint64)
|
ids := args[1].([]uint64)
|
||||||
rr, err := model.Find[models.PostMeta](ctx, model.SqlBuilder{
|
rr, err := model.Finds[models.PostMeta](ctx, model.Conditions(
|
||||||
{"post_id", "in", ""},
|
model.Where(model.SqlBuilder{{"post_id", "in", ""}}),
|
||||||
}, "*", "", nil, nil, nil, 0, slice.ToAnySlice(ids))
|
model.In(slice.ToAnySlice(ids)),
|
||||||
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -24,6 +25,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error)
|
||||||
if _, ok := r[postmeta.PostId]; !ok {
|
if _, ok := r[postmeta.PostId]; !ok {
|
||||||
r[postmeta.PostId] = make(map[string]any)
|
r[postmeta.PostId] = make(map[string]any)
|
||||||
}
|
}
|
||||||
|
r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue
|
||||||
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
||||||
metadata, err := plugins.UnPHPSerialize[models.WpAttachmentMetadata](postmeta.MetaValue)
|
metadata, err := plugins.UnPHPSerialize[models.WpAttachmentMetadata](postmeta.MetaValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,11 +33,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r[postmeta.PostId][postmeta.MetaKey] = metadata
|
r[postmeta.PostId][postmeta.MetaKey] = metadata
|
||||||
|
|
||||||
} else {
|
|
||||||
r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,11 @@ func SearchPostIds(args ...any) (ids PostIds, err error) {
|
||||||
join := args[5].(model.SqlBuilder)
|
join := args[5].(model.SqlBuilder)
|
||||||
postType := args[6].([]any)
|
postType := args[6].([]any)
|
||||||
postStatus := args[7].([]any)
|
postStatus := args[7].([]any)
|
||||||
res, total, err := model.SimplePagination[models.Posts](ctx, where, "ID", "", page, limit, order, join, nil, postType, postStatus)
|
res, total, err := model.SimplePagination[models.Posts](
|
||||||
|
ctx, where, "ID",
|
||||||
|
"", page, limit, order,
|
||||||
|
join, nil, postType, postStatus,
|
||||||
|
)
|
||||||
for _, posts := range res {
|
for _, posts := range res {
|
||||||
ids.Ids = append(ids.Ids, posts.Id)
|
ids.Ids = append(ids.Ids, posts.Id)
|
||||||
}
|
}
|
||||||
|
@ -113,7 +117,10 @@ func SearchPostIds(args ...any) (ids PostIds, err error) {
|
||||||
|
|
||||||
func GetMaxPostId(a ...any) (uint64, error) {
|
func GetMaxPostId(a ...any) (uint64, error) {
|
||||||
ctx := a[0].(context.Context)
|
ctx := a[0].(context.Context)
|
||||||
r, err := model.SimpleFind[models.Posts](ctx, model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID")
|
r, err := model.SimpleFind[models.Posts](ctx,
|
||||||
|
model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}},
|
||||||
|
"max(ID) ID",
|
||||||
|
)
|
||||||
var id uint64
|
var id uint64
|
||||||
if len(r) > 0 {
|
if len(r) > 0 {
|
||||||
id = r[0].Id
|
id = r[0].Id
|
||||||
|
@ -123,14 +130,16 @@ func GetMaxPostId(a ...any) (uint64, error) {
|
||||||
|
|
||||||
func RecentPosts(a ...any) (r []models.Posts, err error) {
|
func RecentPosts(a ...any) (r []models.Posts, err error) {
|
||||||
ctx := a[0].(context.Context)
|
ctx := a[0].(context.Context)
|
||||||
r, err = model.Find[models.Posts](ctx, model.SqlBuilder{{
|
num := a[1].(int)
|
||||||
"post_type", "post",
|
r, err = model.Finds[models.Posts](ctx, model.Conditions(
|
||||||
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", model.SqlBuilder{{"post_date", "desc"}}, nil, nil, 10)
|
model.Where(model.SqlBuilder{
|
||||||
for i, post := range r {
|
{"post_type", "post"},
|
||||||
if post.PostPassword != "" {
|
{"post_status", "publish"},
|
||||||
PasswordProjectTitle(&r[i])
|
}),
|
||||||
}
|
model.Fields("ID,post_title,post_password"),
|
||||||
}
|
model.Order(model.SqlBuilder{{"post_date", "desc"}}),
|
||||||
|
model.Limit(num),
|
||||||
|
))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package models
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Posts struct {
|
type Posts struct {
|
||||||
|
post
|
||||||
Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"`
|
Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"`
|
||||||
PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"`
|
PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"`
|
||||||
PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"`
|
PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"`
|
||||||
|
@ -47,23 +48,19 @@ type PostThumbnail struct {
|
||||||
OriginAttachmentData WpAttachmentMetadata
|
OriginAttachmentData WpAttachmentMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Posts) PrimaryKey() string {
|
type post struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w post) PrimaryKey() string {
|
||||||
return "ID"
|
return "ID"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Posts) Table() string {
|
func (w post) Table() string {
|
||||||
return "wp_posts"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w PostArchive) PrimaryKey() string {
|
|
||||||
return "ID"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w PostArchive) Table() string {
|
|
||||||
return "wp_posts"
|
return "wp_posts"
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostArchive struct {
|
type PostArchive struct {
|
||||||
|
post
|
||||||
Year string `db:"year"`
|
Year string `db:"year"`
|
||||||
Month string `db:"month"`
|
Month string `db:"month"`
|
||||||
Posts int `db:"posts"`
|
Posts int `db:"posts"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user