2022-09-18 04:34:48 +00:00
package common
import (
2022-09-20 04:00:09 +00:00
"context"
2022-09-18 04:34:48 +00:00
"fmt"
2022-09-19 09:39:00 +00:00
"github/fthvgb1/wp-go/cache"
2022-11-04 02:38:59 +00:00
"github/fthvgb1/wp-go/config"
2022-11-17 08:29:39 +00:00
"github/fthvgb1/wp-go/config/wpconfig"
2022-09-27 07:35:34 +00:00
"github/fthvgb1/wp-go/logs"
2022-09-18 04:34:48 +00:00
"github/fthvgb1/wp-go/models"
2022-11-05 02:32:57 +00:00
"github/fthvgb1/wp-go/models/wp"
2022-09-18 14:06:27 +00:00
"sync"
2022-09-19 09:39:00 +00:00
"time"
2022-09-18 04:34:48 +00:00
)
2022-09-27 07:35:34 +00:00
var postContextCache * cache . MapCache [ uint64 , PostContext ]
2022-09-19 12:34:20 +00:00
var archivesCaches * Arch
2022-11-17 09:44:11 +00:00
var categoryCaches * cache . SliceCache [ wp . TermsMy ]
2022-11-05 14:40:02 +00:00
var recentPostsCaches * cache . SliceCache [ wp . Posts ]
var recentCommentsCaches * cache . SliceCache [ wp . Comments ]
2022-10-10 14:46:04 +00:00
var postCommentCaches * cache . MapCache [ uint64 , [ ] uint64 ]
2022-11-05 14:40:02 +00:00
var postsCache * cache . MapCache [ uint64 , wp . Posts ]
2023-01-10 12:09:55 +00:00
var postMetaCache * cache . MapCache [ uint64 , map [ string ] any ]
2022-09-27 13:52:15 +00:00
var monthPostsCache * cache . MapCache [ string , [ ] uint64 ]
2022-09-28 12:02:43 +00:00
var postListIdsCache * cache . MapCache [ string , PostIds ]
2022-09-28 08:23:20 +00:00
var searchPostIdsCache * cache . MapCache [ string , PostIds ]
2022-10-04 03:13:14 +00:00
var maxPostIdCache * cache . SliceCache [ uint64 ]
2022-12-28 06:24:59 +00:00
var TotalRaw int64
2022-11-05 14:40:02 +00:00
var usersCache * cache . MapCache [ uint64 , wp . Users ]
2022-11-17 03:22:29 +00:00
var usersNameCache * cache . MapCache [ string , wp . Users ]
2022-11-05 14:40:02 +00:00
var commentsCache * cache . MapCache [ uint64 , wp . Comments ]
2022-09-19 11:11:36 +00:00
2022-09-26 13:25:41 +00:00
func InitActionsCommonCache ( ) {
2022-11-15 08:36:21 +00:00
c := config . Conf . Load ( )
2022-09-19 12:34:20 +00:00
archivesCaches = & Arch {
2022-09-19 12:15:10 +00:00
mutex : & sync . Mutex { } ,
setCacheFunc : archives ,
}
2022-09-27 07:35:34 +00:00
2022-11-15 08:36:21 +00:00
searchPostIdsCache = cache . NewMapCacheByFn [ string , PostIds ] ( searchPostIds , c . SearchPostCacheTime )
2022-09-28 08:23:20 +00:00
2022-11-15 08:36:21 +00:00
postListIdsCache = cache . NewMapCacheByFn [ string , PostIds ] ( searchPostIds , c . PostListCacheTime )
2022-09-28 12:02:43 +00:00
2022-11-15 08:36:21 +00:00
monthPostsCache = cache . NewMapCacheByFn [ string , [ ] uint64 ] ( monthPost , c . MonthPostCacheTime )
2022-09-27 13:52:15 +00:00
2022-11-15 08:36:21 +00:00
postContextCache = cache . NewMapCacheByFn [ uint64 , PostContext ] ( getPostContext , c . ContextPostCacheTime )
2022-09-27 07:35:34 +00:00
2022-11-15 08:36:21 +00:00
postsCache = cache . NewMapCacheByBatchFn [ uint64 , wp . Posts ] ( getPostsByIds , c . PostDataCacheTime )
2022-09-26 13:25:41 +00:00
2023-01-10 12:09:55 +00:00
postMetaCache = cache . NewMapCacheByBatchFn [ uint64 , map [ string ] any ] ( getPostMetaByPostIds , c . PostDataCacheTime )
2022-11-17 09:44:11 +00:00
categoryCaches = cache . NewSliceCache [ wp . TermsMy ] ( categories , c . CategoryCacheTime )
2022-09-26 13:25:41 +00:00
2022-11-15 08:36:21 +00:00
recentPostsCaches = cache . NewSliceCache [ wp . Posts ] ( recentPosts , c . RecentPostCacheTime )
2022-09-26 13:25:41 +00:00
2022-11-15 08:36:21 +00:00
recentCommentsCaches = cache . NewSliceCache [ wp . Comments ] ( recentComments , c . RecentCommentsCacheTime )
2022-09-28 12:02:43 +00:00
2022-11-15 08:36:21 +00:00
postCommentCaches = cache . NewMapCacheByFn [ uint64 , [ ] uint64 ] ( postComments , c . PostCommentsCacheTime )
2022-10-04 03:13:14 +00:00
2022-11-15 08:36:21 +00:00
maxPostIdCache = cache . NewSliceCache [ uint64 ] ( getMaxPostId , c . MaxPostIdCacheTime )
2022-10-06 13:33:04 +00:00
2022-11-17 03:22:29 +00:00
usersCache = cache . NewMapCacheByFn [ uint64 , wp . Users ] ( getUserById , c . UserInfoCacheTime )
usersNameCache = cache . NewMapCacheByFn [ string , wp . Users ] ( getUserByName , c . UserInfoCacheTime )
2022-10-08 06:01:05 +00:00
2022-11-15 08:36:21 +00:00
commentsCache = cache . NewMapCacheByBatchFn [ uint64 , wp . Comments ] ( getCommentByIds , c . CommentsCacheTime )
2022-09-28 13:16:05 +00:00
}
func ClearCache ( ) {
searchPostIdsCache . ClearExpired ( )
postsCache . ClearExpired ( )
2023-01-10 12:09:55 +00:00
postMetaCache . ClearExpired ( )
2022-09-28 13:16:05 +00:00
postListIdsCache . ClearExpired ( )
monthPostsCache . ClearExpired ( )
postContextCache . ClearExpired ( )
2022-10-06 13:36:13 +00:00
usersCache . ClearExpired ( )
2022-10-08 11:35:05 +00:00
commentsCache . ClearExpired ( )
2022-11-17 03:22:29 +00:00
usersNameCache . ClearExpired ( )
2022-09-19 11:11:36 +00:00
}
2022-11-15 08:36:21 +00:00
func FlushCache ( ) {
searchPostIdsCache . Flush ( )
postsCache . Flush ( )
2023-01-10 12:09:55 +00:00
postMetaCache . Flush ( )
2022-11-15 08:36:21 +00:00
postListIdsCache . Flush ( )
monthPostsCache . Flush ( )
postContextCache . Flush ( )
usersCache . Flush ( )
commentsCache . Flush ( )
2022-11-17 03:22:29 +00:00
usersCache . Flush ( )
2022-11-15 08:36:21 +00:00
}
2022-09-19 11:11:36 +00:00
2022-09-27 13:52:15 +00:00
type PostIds struct {
Ids [ ] uint64
Length int
}
2022-09-19 12:34:20 +00:00
type Arch struct {
2022-11-05 02:32:57 +00:00
data [ ] wp . PostArchive
2022-09-19 12:15:10 +00:00
mutex * sync . Mutex
2022-11-07 08:04:13 +00:00
setCacheFunc func ( context . Context ) ( [ ] wp . PostArchive , error )
2022-09-19 12:15:10 +00:00
month time . Month
}
2022-11-07 08:04:13 +00:00
func ( c * Arch ) getArchiveCache ( ctx context . Context ) [ ] wp . PostArchive {
2022-09-19 12:15:10 +00:00
l := len ( c . data )
m := time . Now ( ) . Month ( )
if l > 0 && c . month != m || l < 1 {
2022-11-07 08:04:13 +00:00
r , err := c . setCacheFunc ( ctx )
2022-09-19 12:15:10 +00:00
if err != nil {
2022-09-27 07:35:34 +00:00
logs . ErrPrintln ( err , "set cache err[%s]" )
2022-09-19 12:15:10 +00:00
return nil
}
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
c . month = m
c . data = r
}
return c . data
}
2022-09-19 11:11:36 +00:00
type PostContext struct {
2022-11-05 14:40:02 +00:00
prev wp . Posts
next wp . Posts
2022-09-19 11:11:36 +00:00
}
2022-11-07 08:04:13 +00:00
func archives ( ctx context . Context ) ( [ ] wp . PostArchive , error ) {
return models . Find [ wp . PostArchive ] ( ctx , models . SqlBuilder {
2022-09-18 04:34:48 +00:00
{ "post_type" , "post" } , { "post_status" , "publish" } ,
2022-11-04 09:42:11 +00:00
} , "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts" , "year,month" , models . SqlBuilder { { "year" , "desc" } , { "month" , "desc" } } , nil , nil , 0 )
2022-09-18 04:34:48 +00:00
}
2022-11-07 08:04:13 +00:00
func Archives ( ctx context . Context ) ( r [ ] wp . PostArchive ) {
return archivesCaches . getArchiveCache ( ctx )
2022-09-19 09:39:00 +00:00
}
2022-11-17 09:44:11 +00:00
func Categories ( ctx context . Context ) [ ] wp . TermsMy {
2022-11-07 08:04:13 +00:00
r , err := categoryCaches . GetCache ( ctx , time . Second , ctx )
2022-09-27 07:35:34 +00:00
logs . ErrPrintln ( err , "get category " )
2022-09-20 08:11:20 +00:00
return r
2022-09-19 09:39:00 +00:00
}
2022-11-17 09:44:11 +00:00
func categories ( a ... any ) ( terms [ ] wp . TermsMy , err error ) {
2022-11-07 08:04:13 +00:00
ctx := a [ 0 ] . ( context . Context )
2022-09-20 14:37:31 +00:00
var in = [ ] any { "category" }
2022-11-17 09:44:11 +00:00
terms , err = models . Find [ wp . TermsMy ] ( ctx , models . SqlBuilder {
2022-09-18 04:34:48 +00:00
{ "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" } ,
2022-11-04 09:42:11 +00:00
} , nil , 0 , in )
2022-09-18 04:34:48 +00:00
for i := 0 ; i < len ( terms ) ; i ++ {
2022-11-17 09:44:11 +00:00
if v , ok := wpconfig . Terms . Load ( terms [ i ] . Terms . TermId ) ; ok {
terms [ i ] . Terms = v
2022-09-18 04:34:48 +00:00
}
2022-11-17 09:44:11 +00:00
if v , ok := wpconfig . TermTaxonomies . Load ( terms [ i ] . Terms . TermId ) ; ok {
2022-11-05 14:40:02 +00:00
terms [ i ] . TermTaxonomy = v
2022-09-18 04:34:48 +00:00
}
}
return
}
2022-11-05 14:40:02 +00:00
func PasswordProjectTitle ( post * wp . Posts ) {
2022-09-18 14:06:27 +00:00
if post . PostPassword != "" {
2022-09-18 04:34:48 +00:00
post . PostTitle = fmt . Sprintf ( "密码保护:%s" , post . PostTitle )
}
2022-09-18 14:06:27 +00:00
}
2022-11-05 14:40:02 +00:00
func PasswdProjectContent ( post * wp . Posts ) {
2022-09-18 04:34:48 +00:00
if post . PostContent != "" {
format := `
< form action = "/login" class = "post-password-form" method = "post" >
< p > 此内容受密码保护 。 如需查阅 , 请在下列字段中输入您的密码 。 < / p >
< p > < label for = "pwbox-%d" > 密码 : < input name = "post_password" id = "pwbox-%d" type = "password" size = "20" > < / label > < input type = "submit" name = "Submit" value = "提交" > < / p >
< / form > `
post . PostContent = fmt . Sprintf ( format , post . Id , post . Id )
}
}