From e780c358d77cd96db3716d4665d37cac0b814248 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 20 Jan 2023 18:10:13 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bug=20=E5=8F=8A=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/{slice.go => vars.go} | 31 +++++++++++++----------- internal/actions/detail.go | 29 ++--------------------- internal/pkg/cache/cache.go | 46 ++++++++++++++++++------------------ internal/pkg/cache/feed.go | 4 ++-- internal/pkg/cache/posts.go | 3 +-- internal/pkg/dao/posts.go | 4 ++-- internal/plugins/comment.go | 1 + internal/plugins/gravatar.go | 33 ++++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 70 deletions(-) rename cache/{slice.go => vars.go} (60%) create mode 100644 internal/plugins/comment.go create mode 100644 internal/plugins/gravatar.go diff --git a/cache/slice.go b/cache/vars.go similarity index 60% rename from cache/slice.go rename to cache/vars.go index e19ba89..9324d81 100644 --- a/cache/slice.go +++ b/cache/vars.go @@ -9,26 +9,26 @@ import ( "time" ) -type SliceCache[T any] struct { - v safety.Var[slice[T]] +type VarCache[T any] struct { + v safety.Var[vars[T]] } -type slice[T any] struct { - data []T +type vars[T any] struct { + data T mutex *sync.Mutex - setCacheFunc func(...any) ([]T, error) + setCacheFunc func(...any) (T, error) expireTime time.Duration setTime time.Time incr int } -func (c *SliceCache[T]) GetLastSetTime() time.Time { +func (c *VarCache[T]) GetLastSetTime() time.Time { return c.v.Load().setTime } -func NewSliceCache[T any](fun func(...any) ([]T, error), duration time.Duration) *SliceCache[T] { - return &SliceCache[T]{ - v: safety.NewVar(slice[T]{ +func NewVarCache[T any](fun func(...any) (T, error), duration time.Duration) *VarCache[T] { + return &VarCache[T]{ + v: safety.NewVar(vars[T]{ mutex: &sync.Mutex{}, setCacheFunc: fun, expireTime: duration, @@ -36,20 +36,23 @@ func NewSliceCache[T any](fun func(...any) ([]T, error), duration time.Duration) } } -func (c *SliceCache[T]) FlushCache() { +func (c *VarCache[T]) IsExpired() bool { + v := c.v.Load() + return time.Duration(v.setTime.UnixNano())+v.expireTime < time.Duration(time.Now().UnixNano()) +} + +func (c *VarCache[T]) Flush() { mu := c.v.Load().mutex mu.Lock() defer mu.Unlock() c.v.Delete() } -func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) ([]T, error) { +func (c *VarCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) (T, error) { v := c.v.Load() - l := len(v.data) data := v.data var err error - expired := time.Duration(v.setTime.UnixNano())+v.expireTime < time.Duration(time.Now().UnixNano()) - if l < 1 || (l > 0 && v.expireTime >= 0 && expired) { + if v.expireTime <= 0 || ((time.Duration(v.setTime.UnixNano()) + v.expireTime) < time.Duration(time.Now().UnixNano())) { t := v.incr call := func() { v.mutex.Lock() diff --git a/internal/actions/detail.go b/internal/actions/detail.go index 7a8a4a3..5e95d63 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -10,13 +10,10 @@ import ( "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" - "math/rand" "net/http" - "net/url" "sort" "strconv" "strings" - "time" ) type detailHandler struct { @@ -68,7 +65,7 @@ func Detail(c *gin.Context) { return } post, err := cache.GetPostById(c, ID) - if post.Id == 0 || err != nil { + if post.Id == 0 || err != nil || post.PostStatus != "publish" { return } pw := sessions.Default(c).Get("post_password") @@ -244,7 +241,7 @@ func (d detailHandler) formatLi(comments models.Comments, depth int, eo, parent for k, v := range map[string]string{ "{{CommentId}}": strconv.FormatUint(comments.CommentId, 10), "{{Depth}}": strconv.Itoa(depth), - "{{Gravatar}}": gravatar(d.Context, comments.CommentAuthorEmail), + "{{Gravatar}}": plugins.Gravatar(comments.CommentAuthorEmail, d.Context.Request.TLS != nil), "{{CommentAuthorUrl}}": comments.CommentAuthorUrl, "{{CommentAuthor}}": comments.CommentAuthor, "{{PostId}}": strconv.FormatUint(comments.CommentPostId, 10), @@ -258,25 +255,3 @@ func (d detailHandler) formatLi(comments models.Comments, depth int, eo, parent } return li } - -func gravatar(c *gin.Context, email string) (u string) { - email = strings.Trim(email, " \t\n\r\000\x0B") - rand.Seed(time.Now().UnixNano()) - num := rand.Intn(3) - h := "" - if email != "" { - h = helper.StringMd5(strings.ToLower(email)) - num = int(h[0] % 3) - } - if c.Request.TLS != nil { - u = fmt.Sprintf("%s%s", "https://secure.gravatar.com/avatar/", h) - } else { - u = fmt.Sprintf("http://%d.gravatar.com/avatar/%s", num, h) - } - q := url.Values{} - q.Add("s", "112") - q.Add("d", "mm") - q.Add("r", strings.ToLower(wpconfig.Options.Value("avatar_rating"))) - u = fmt.Sprintf("%s?%s", u, q.Encode()) - return -} diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 72c1755..97005bd 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -13,9 +13,9 @@ import ( var postContextCache *cache.MapCache[uint64, common.PostContext] var archivesCaches *Arch -var categoryCaches *cache.SliceCache[models.TermsMy] -var recentPostsCaches *cache.SliceCache[models.Posts] -var recentCommentsCaches *cache.SliceCache[models.Comments] +var categoryCaches *cache.VarCache[[]models.TermsMy] +var recentPostsCaches *cache.VarCache[[]models.Posts] +var recentCommentsCaches *cache.VarCache[[]models.Comments] var postCommentCaches *cache.MapCache[uint64, []uint64] var postsCache *cache.MapCache[uint64, models.Posts] @@ -24,17 +24,17 @@ var postMetaCache *cache.MapCache[uint64, map[string]any] var monthPostsCache *cache.MapCache[string, []uint64] var postListIdsCache *cache.MapCache[string, common.PostIds] var searchPostIdsCache *cache.MapCache[string, common.PostIds] -var maxPostIdCache *cache.SliceCache[uint64] +var maxPostIdCache *cache.VarCache[uint64] var usersCache *cache.MapCache[uint64, models.Users] var usersNameCache *cache.MapCache[string, models.Users] var commentsCache *cache.MapCache[uint64, models.Comments] -var feedCache *cache.SliceCache[string] +var feedCache *cache.VarCache[[]string] var postFeedCache *cache.MapCache[string, string] -var commentsFeedCache *cache.SliceCache[string] +var commentsFeedCache *cache.VarCache[[]string] var newCommentCache *cache.MapCache[string, string] @@ -45,39 +45,39 @@ func InitActionsCommonCache() { setCacheFunc: common.Archives, } - searchPostIdsCache = cache.NewMapCacheByFn[string, common.PostIds](common.SearchPostIds, c.SearchPostCacheTime) + searchPostIdsCache = cache.NewMapCacheByFn[string](common.SearchPostIds, c.SearchPostCacheTime) - postListIdsCache = cache.NewMapCacheByFn[string, common.PostIds](common.SearchPostIds, c.PostListCacheTime) + postListIdsCache = cache.NewMapCacheByFn[string](common.SearchPostIds, c.PostListCacheTime) - monthPostsCache = cache.NewMapCacheByFn[string, []uint64](common.MonthPost, c.MonthPostCacheTime) + monthPostsCache = cache.NewMapCacheByFn[string](common.MonthPost, c.MonthPostCacheTime) - postContextCache = cache.NewMapCacheByFn[uint64, common.PostContext](common.GetPostContext, c.ContextPostCacheTime) + postContextCache = cache.NewMapCacheByFn[uint64](common.GetPostContext, c.ContextPostCacheTime) - postsCache = cache.NewMapCacheByBatchFn[uint64, models.Posts](common.GetPostsByIds, c.PostDataCacheTime) + postsCache = cache.NewMapCacheByBatchFn(common.GetPostsByIds, c.PostDataCacheTime) - postMetaCache = cache.NewMapCacheByBatchFn[uint64, map[string]any](common.GetPostMetaByPostIds, c.PostDataCacheTime) + postMetaCache = cache.NewMapCacheByBatchFn(common.GetPostMetaByPostIds, c.PostDataCacheTime) - categoryCaches = cache.NewSliceCache[models.TermsMy](common.Categories, c.CategoryCacheTime) + categoryCaches = cache.NewVarCache(common.Categories, c.CategoryCacheTime) - recentPostsCaches = cache.NewSliceCache[models.Posts](common.RecentPosts, c.RecentPostCacheTime) + recentPostsCaches = cache.NewVarCache(common.RecentPosts, c.RecentPostCacheTime) - recentCommentsCaches = cache.NewSliceCache[models.Comments](common.RecentComments, c.RecentCommentsCacheTime) + recentCommentsCaches = cache.NewVarCache(common.RecentComments, c.RecentCommentsCacheTime) - postCommentCaches = cache.NewMapCacheByFn[uint64, []uint64](common.PostComments, c.PostCommentsCacheTime) + postCommentCaches = cache.NewMapCacheByFn[uint64](common.PostComments, c.PostCommentsCacheTime) - maxPostIdCache = cache.NewSliceCache[uint64](common.GetMaxPostId, c.MaxPostIdCacheTime) + maxPostIdCache = cache.NewVarCache(common.GetMaxPostId, c.MaxPostIdCacheTime) - usersCache = cache.NewMapCacheByFn[uint64, models.Users](common.GetUserById, c.UserInfoCacheTime) + usersCache = cache.NewMapCacheByFn[uint64](common.GetUserById, c.UserInfoCacheTime) - usersNameCache = cache.NewMapCacheByFn[string, models.Users](common.GetUserByName, c.UserInfoCacheTime) + usersNameCache = cache.NewMapCacheByFn[string](common.GetUserByName, c.UserInfoCacheTime) - commentsCache = cache.NewMapCacheByBatchFn[uint64, models.Comments](common.GetCommentByIds, c.CommentsCacheTime) + commentsCache = cache.NewMapCacheByBatchFn(common.GetCommentByIds, c.CommentsCacheTime) - feedCache = cache.NewSliceCache(feed, time.Hour) + feedCache = cache.NewVarCache(feed, time.Hour) - postFeedCache = cache.NewMapCacheByFn[string, string](postFeed, time.Hour) + postFeedCache = cache.NewMapCacheByFn[string](postFeed, time.Hour) - commentsFeedCache = cache.NewSliceCache(commentsFeed, time.Hour) + commentsFeedCache = cache.NewVarCache(commentsFeed, time.Hour) newCommentCache = cache.NewMapCacheByFn[string, string](nil, 15*time.Minute) diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index 8f892f3..0f36212 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -32,11 +32,11 @@ func InitFeed() { } } -func CommentsFeedCache() *cache.SliceCache[string] { +func CommentsFeedCache() *cache.VarCache[[]string] { return commentsFeedCache } -func FeedCache() *cache.SliceCache[string] { +func FeedCache() *cache.VarCache[[]string] { return feedCache } diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index 6784b9a..e4b2c8c 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -39,8 +39,7 @@ func PostLists(ctx context.Context, key string, args ...any) (r []models.Posts, } func GetMaxPostId(ctx *gin.Context) (uint64, error) { - Id, err := maxPostIdCache.GetCache(ctx, time.Second, ctx) - return Id[0], err + return maxPostIdCache.GetCache(ctx, time.Second, ctx) } func RecentPosts(ctx context.Context, n int) (r []models.Posts) { diff --git a/internal/pkg/dao/posts.go b/internal/pkg/dao/posts.go index cea3b28..1e24d45 100644 --- a/internal/pkg/dao/posts.go +++ b/internal/pkg/dao/posts.go @@ -109,14 +109,14 @@ func SearchPostIds(args ...any) (ids PostIds, err error) { return } -func GetMaxPostId(a ...any) ([]uint64, error) { +func GetMaxPostId(a ...any) (uint64, error) { ctx := a[0].(context.Context) r, err := model.SimpleFind[models.Posts](ctx, model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID") var id uint64 if len(r) > 0 { id = r[0].Id } - return []uint64{id}, err + return id, err } func RecentPosts(a ...any) (r []models.Posts, err error) { diff --git a/internal/plugins/comment.go b/internal/plugins/comment.go new file mode 100644 index 0000000..d5c343e --- /dev/null +++ b/internal/plugins/comment.go @@ -0,0 +1 @@ +package plugins diff --git a/internal/plugins/gravatar.go b/internal/plugins/gravatar.go new file mode 100644 index 0000000..1509233 --- /dev/null +++ b/internal/plugins/gravatar.go @@ -0,0 +1,33 @@ +package plugins + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "math/rand" + "net/url" + "strings" + "time" +) + +func Gravatar(email string, isTls bool) (u string) { + email = strings.Trim(email, " \t\n\r\000\x0B") + rand.Seed(time.Now().UnixNano()) + num := rand.Intn(3) + h := "" + if email != "" { + h = helper.StringMd5(strings.ToLower(email)) + num = int(h[0] % 3) + } + if isTls { + u = fmt.Sprintf("%s%s", "https://secure.gravatar.com/avatar/", h) + } else { + u = fmt.Sprintf("http://%d.gravatar.com/avatar/%s", num, h) + } + q := url.Values{} + q.Add("s", "112") + q.Add("d", "mm") + q.Add("r", strings.ToLower(wpconfig.Options.Value("avatar_rating"))) + u = fmt.Sprintf("%s?%s", u, q.Encode()) + return +}