package common import ( "context" "database/sql" "fmt" "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/vars" "log" "strconv" "strings" "sync" "time" ) var PostsCache sync.Map var PostContextCache sync.Map var archivesCaches *Arch var categoryCaches *cache.SliceCache[models.WpTermsMy] var recentPostsCaches *cache.SliceCache[models.WpPosts] var recentCommentsCaches *cache.SliceCache[models.WpComments] var postCommentCaches *cache.MapCache[uint64, []models.WpComments] var postsCache *cache.MapCache[uint64, models.WpPosts] func InitActionsCommonCache() { archivesCaches = &Arch{ mutex: &sync.Mutex{}, setCacheFunc: archives, } postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPosts, time.Hour) categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5) } type Arch struct { data []models.PostArchive mutex *sync.Mutex setCacheFunc func() ([]models.PostArchive, error) month time.Month } func (c *Arch) getArchiveCache() []models.PostArchive { l := len(c.data) m := time.Now().Month() if l > 0 && c.month != m || l < 1 { r, err := c.setCacheFunc() if err != nil { log.Printf("set cache err[%s]", err) return nil } c.mutex.Lock() defer c.mutex.Unlock() c.month = m c.data = r } return c.data } type PostContext struct { Prev models.WpPosts Next models.WpPosts expireTime time.Duration setTime time.Time } func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) { return postCommentCaches.GetCache(ctx, Id, time.Second, Id) } func postComments(args ...any) ([]models.WpComments, error) { postId := args[0].(uint64) return models.Find[models.WpComments](models.SqlBuilder{ {"comment_approved", "1"}, {"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"}, }, "*", "", models.SqlBuilder{ {"comment_date_gmt", "asc"}, {"comment_ID", "asc"}, }, nil, 0) } func RecentComments(ctx context.Context) (r []models.WpComments) { r, _ = recentCommentsCaches.GetCache(ctx, time.Second) return } func recentComments(...any) (r []models.WpComments, err error) { return models.Find[models.WpComments](models.SqlBuilder{ {"comment_approved", "1"}, {"post_status", "publish"}, }, "comment_ID,comment_author,comment_post_ID,post_title", "", models.SqlBuilder{{"comment_date_gmt", "desc"}}, models.SqlBuilder{ {"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}, }, 5) } func getMonthPost(args ...any) ([]models.WpPosts, error) { y := args[0].(string) m := args[1].(string) where := models.SqlBuilder{ {"post_type", "in", ""}, {"post_status", "in", ""}, {"month(post_date)", m}, {"year(post_date)", y}, } return models.Find[models.WpPosts](where, "ID", "", models.SqlBuilder{{"post_date", "asc"}}, 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 } func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { next, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ {"post_date", ">", t.Format("2006-01-02 15:04:05")}, {"post_status", "in", ""}, {"post_type", "post"}, }, "ID,post_title,post_password", nil, []any{"publish", "private"}) prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ {"post_date", "<", t.Format("2006-01-02 15:04:05")}, {"post_status", "in", ""}, {"post_type", "post"}, }, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []any{"publish", "private"}) if err == sql.ErrNoRows { 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 { rawPosts, er := models.Find[models.WpPosts](models.SqlBuilder{{ "Id", "in", "", }}, "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(`%s`, 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(`%s`, cat, cat)) } pp.TagsHtml = strings.Join(t, "、") } PostsCache.Store(pp.Id, pp) } } return } func archives() ([]models.PostArchive, error) { return models.Find[models.PostArchive](models.SqlBuilder{ {"post_type", "post"}, {"post_status", "publish"}, }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0) } func Archives() (r []models.PostArchive) { return archivesCaches.getArchiveCache() } func Categories(ctx context.Context) []models.WpTermsMy { r, _ := categoryCaches.GetCache(ctx, time.Second) return r } func categories(...any) (terms []models.WpTermsMy, err error) { var in = []any{"category"} terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{ {"tt.count", ">", "0", "int"}, {"tt.taxonomy", "in", ""}, }, "t.term_id", "", models.SqlBuilder{ {"t.name", "asc"}, }, models.SqlBuilder{ {"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"}, }, 0, in) for i := 0; i < len(terms); i++ { if v, ok := models.Terms[terms[i].WpTerms.TermId]; ok { terms[i].WpTerms = v } if v, ok := models.TermTaxonomy[terms[i].WpTerms.TermId]; ok { terms[i].WpTermTaxonomy = v } } return } func RecentPosts(ctx context.Context) (r []models.WpPosts) { r, _ = recentPostsCaches.GetCache(ctx, time.Second) return } func recentPosts(...any) (r []models.WpPosts, err error) { r, err = models.Find[models.WpPosts](models.SqlBuilder{{ "post_type", "post", }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) for i, post := range r { if post.PostPassword != "" { PasswordProjectTitle(&r[i]) } } return } func PasswordProjectTitle(post *models.WpPosts) { if post.PostPassword != "" { post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) } } func PasswdProjectContent(post *models.WpPosts) { if post.PostContent != "" { format := `
` post.PostContent = fmt.Sprintf(format, post.Id, post.Id) } }