package common import ( "context" "fmt" "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/logs" "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/vars" "sync" "time" ) var postContextCache *cache.MapCache[uint64, PostContext] 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] var monthPostsCache *cache.MapCache[string, []uint64] var postListIdsCache *cache.MapCache[string, PostIds] var searchPostIdsCache *cache.MapCache[string, PostIds] var maxPostIdCache *cache.SliceCache[uint64] var TotalRaw int var usersCache *cache.MapCache[uint64, models.WpUsers] var commentsCache *cache.MapCache[uint64, models.WpComments] func InitActionsCommonCache() { archivesCaches = &Arch{ mutex: &sync.Mutex{}, setCacheFunc: archives, } searchPostIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime) postListIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime) monthPostsCache = cache.NewMapCacheByFn[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime) postContextCache = cache.NewMapCacheByFn[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) postsCache = cache.NewMapCacheByBatchFn[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime) 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.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.PostCommentsCacheTime) maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime) usersCache = cache.NewMapCacheByBatchFn[uint64, models.WpUsers](getUsers, vars.Conf.UserInfoCacheTime) commentsCache = cache.NewMapCacheByBatchFn[uint64, models.WpComments](getCommentByIds, vars.Conf.CommentsCacheTime) } func ClearCache() { searchPostIdsCache.ClearExpired() postsCache.ClearExpired() postsCache.ClearExpired() postListIdsCache.ClearExpired() monthPostsCache.ClearExpired() postContextCache.ClearExpired() usersCache.ClearExpired() commentsCache.ClearExpired() } type PostIds struct { Ids []uint64 Length int } 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 { logs.ErrPrintln(err, "set cache err[%s]") 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 } 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, err := categoryCaches.GetCache(ctx, time.Second) logs.ErrPrintln(err, "get category ") 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 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) } }