This commit is contained in:
xing 2022-11-17 17:44:11 +08:00
parent 8752d4a023
commit 6ad7472d93
4 changed files with 18 additions and 18 deletions

View File

@ -15,7 +15,7 @@ import (
var postContextCache *cache.MapCache[uint64, PostContext] var postContextCache *cache.MapCache[uint64, PostContext]
var archivesCaches *Arch var archivesCaches *Arch
var categoryCaches *cache.SliceCache[wp.WpTermsMy] var categoryCaches *cache.SliceCache[wp.TermsMy]
var recentPostsCaches *cache.SliceCache[wp.Posts] var recentPostsCaches *cache.SliceCache[wp.Posts]
var recentCommentsCaches *cache.SliceCache[wp.Comments] var recentCommentsCaches *cache.SliceCache[wp.Comments]
var postCommentCaches *cache.MapCache[uint64, []uint64] var postCommentCaches *cache.MapCache[uint64, []uint64]
@ -46,7 +46,7 @@ func InitActionsCommonCache() {
postsCache = cache.NewMapCacheByBatchFn[uint64, wp.Posts](getPostsByIds, c.PostDataCacheTime) postsCache = cache.NewMapCacheByBatchFn[uint64, wp.Posts](getPostsByIds, c.PostDataCacheTime)
categoryCaches = cache.NewSliceCache[wp.WpTermsMy](categories, c.CategoryCacheTime) categoryCaches = cache.NewSliceCache[wp.TermsMy](categories, c.CategoryCacheTime)
recentPostsCaches = cache.NewSliceCache[wp.Posts](recentPosts, c.RecentPostCacheTime) recentPostsCaches = cache.NewSliceCache[wp.Posts](recentPosts, c.RecentPostCacheTime)
@ -130,16 +130,16 @@ func Archives(ctx context.Context) (r []wp.PostArchive) {
return archivesCaches.getArchiveCache(ctx) return archivesCaches.getArchiveCache(ctx)
} }
func Categories(ctx context.Context) []wp.WpTermsMy { func Categories(ctx context.Context) []wp.TermsMy {
r, err := categoryCaches.GetCache(ctx, time.Second, ctx) r, err := categoryCaches.GetCache(ctx, time.Second, ctx)
logs.ErrPrintln(err, "get category ") logs.ErrPrintln(err, "get category ")
return r return r
} }
func categories(a ...any) (terms []wp.WpTermsMy, err error) { func categories(a ...any) (terms []wp.TermsMy, err error) {
ctx := a[0].(context.Context) ctx := a[0].(context.Context)
var in = []any{"category"} var in = []any{"category"}
terms, err = models.Find[wp.WpTermsMy](ctx, models.SqlBuilder{ terms, err = models.Find[wp.TermsMy](ctx, models.SqlBuilder{
{"tt.count", ">", "0", "int"}, {"tt.count", ">", "0", "int"},
{"tt.taxonomy", "in", ""}, {"tt.taxonomy", "in", ""},
}, "t.term_id", "", models.SqlBuilder{ }, "t.term_id", "", models.SqlBuilder{
@ -148,10 +148,10 @@ func categories(a ...any) (terms []wp.WpTermsMy, err error) {
{"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) }, nil, 0, in)
for i := 0; i < len(terms); i++ { for i := 0; i < len(terms); i++ {
if v, ok := wpconfig.Terms.Load(terms[i].WpTerms.TermId); ok { if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok {
terms[i].WpTerms = v terms[i].Terms = v
} }
if v, ok := wpconfig.TermTaxonomies.Load(terms[i].WpTerms.TermId); ok { if v, ok := wpconfig.TermTaxonomies.Load(terms[i].Terms.TermId); ok {
terms[i].TermTaxonomy = v terms[i].TermTaxonomy = v
} }
} }

View File

@ -7,12 +7,12 @@ import (
"github/fthvgb1/wp-go/safety" "github/fthvgb1/wp-go/safety"
) )
var Terms safety.Map[uint64, wp.WpTerms] var Terms safety.Map[uint64, wp.Terms]
var TermTaxonomies safety.Map[uint64, wp.TermTaxonomy] var TermTaxonomies safety.Map[uint64, wp.TermTaxonomy]
func InitTerms() (err error) { func InitTerms() (err error) {
ctx := context.Background() ctx := context.Background()
terms, err := models.SimpleFind[wp.WpTerms](ctx, nil, "*") terms, err := models.SimpleFind[wp.Terms](ctx, nil, "*")
if err != nil { if err != nil {
return err return err
} }

View File

@ -153,7 +153,7 @@ func TestFind(t *testing.T) {
{"a", "left join", wp.Users{}.Table() + " b", "a.post_author=b.ID"}, {"a", "left join", wp.Users{}.Table() + " b", "a.post_author=b.ID"},
{"left join", "wp_term_relationships c", "a.Id=c.object_id"}, {"left join", "wp_term_relationships c", "a.Id=c.object_id"},
{"left join", wp.TermTaxonomy{}.Table() + " d", "c.term_taxonomy_id=d.term_taxonomy_id"}, {"left join", wp.TermTaxonomy{}.Table() + " d", "c.term_taxonomy_id=d.term_taxonomy_id"},
{"left join", wp.WpTerms{}.Table() + " e", "d.term_id=e.term_id"}, {"left join", wp.Terms{}.Table() + " e", "d.term_id=e.term_id"},
}, },
having: SqlBuilder{{"n", ">", "0", "int"}}, having: SqlBuilder{{"n", ">", "0", "int"}},
limit: 10, limit: 10,

View File

@ -1,27 +1,27 @@
package wp package wp
type WpTerms struct { type Terms struct {
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"` TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
Name string `gorm:"column:name" db:"name" json:"name" form:"name"` Name string `gorm:"column:name" db:"name" json:"name" form:"name"`
Slug string `gorm:"column:slug" db:"slug" json:"slug" form:"slug"` Slug string `gorm:"column:slug" db:"slug" json:"slug" form:"slug"`
TermGroup int64 `gorm:"column:term_group" db:"term_group" json:"term_group" form:"term_group"` TermGroup int64 `gorm:"column:term_group" db:"term_group" json:"term_group" form:"term_group"`
} }
func (t WpTerms) PrimaryKey() string { func (t Terms) PrimaryKey() string {
return "term_id" return "term_id"
} }
func (t WpTerms) Table() string { func (t Terms) Table() string {
return "wp_terms" return "wp_terms"
} }
type WpTermsMy struct { type TermsMy struct {
WpTerms Terms
TermTaxonomy TermTaxonomy
} }
func (t WpTermsMy) PrimaryKey() string { func (t TermsMy) PrimaryKey() string {
return "term_id" return "term_id"
} }
func (t WpTermsMy) Table() string { func (t TermsMy) Table() string {
return "wp_terms" return "wp_terms"
} }