From 972ca5625f52c586779d27f210b7aebc7b32fefb Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 19 Sep 2022 20:15:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 37 +++++++++++++++++++++++++++++++++---- actions/index.go | 32 ++++++++++++++++---------------- cache/cache.go | 8 ++------ models/model.go | 6 +++--- 4 files changed, 54 insertions(+), 29 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index 6d52bab..57d3bae 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -5,6 +5,7 @@ import ( "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/vars" + "log" "strings" "sync" "time" @@ -13,16 +14,44 @@ import ( var PostsCache sync.Map var PostContextCache sync.Map -var archivesCaches *cache.SliceCache[models.PostArchive] +var archivesCaches *Arch[models.PostArchive] var categoryCaches *cache.SliceCache[models.WpTermsMy] var recentPostsCaches *cache.SliceCache[models.WpPosts] func InitCache() { - archivesCaches = cache.NewSliceCache[models.PostArchive](archives, vars.Conf.ArchiveCacheTime) + archivesCaches = &Arch[models.PostArchive]{ + mutex: &sync.Mutex{}, + setCacheFunc: archives, + } categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) } +type Arch[T any] struct { + cache.SliceCache[T] + data []T + mutex *sync.Mutex + setCacheFunc func() ([]T, error) + month time.Month +} + +func (c *Arch[T]) GetCache() []T { + 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 @@ -55,7 +84,7 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { {"post_date", ">", t.Format("2006-01-02 15:04:05")}, {"post_status", "publish"}, {"post_type", "post"}, - }, "ID,post_title,post_password") + }, "ID,post_title,post_password", nil) if _, ok := PostsCache.Load(next.Id); !ok { } @@ -63,7 +92,7 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { {"post_date", "<", t.Format("2006-01-02 15:04:05")}, {"post_status", "publish"}, {"post_type", "post"}, - }, "ID,post_title") + }, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}) return } diff --git a/actions/index.go b/actions/index.go index c194ee6..892ad16 100644 --- a/actions/index.go +++ b/actions/index.go @@ -148,7 +148,18 @@ func (h *indexHandle) getTotalPage(totalRaws int) int { func Index(c *gin.Context) { h := newIndexHandle(c) h.parseParams() - ginH := gin.H{} + archive := common.Archives() + recent := common.RecentPosts() + categoryItems := common.Categories() + ginH := gin.H{ + "options": models.Options, + "recentPosts": recent, + "archives": archive, + "categories": categoryItems, + "search": h.search, + "header": h.header, + "title": h.getTitle(), + } postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status) defer func() { c.HTML(http.StatusOK, "posts/index.gohtml", ginH) @@ -174,27 +185,16 @@ func Index(c *gin.Context) { } postIds[i] = px } - recent := common.RecentPosts() for i, post := range recent { if post.PostPassword != "" && pw != post.PostPassword { common.PasswdProjectContent(&recent[i]) } } - archive := common.Archives() - categoryItems := common.Categories() q := c.Request.URL.Query().Encode() - ginH = gin.H{ - "posts": postIds, - "options": models.Options, - "recentPosts": recent, - "archives": archive, - "categories": categoryItems, - "totalPage": h.getTotalPage(totalRaw), - "pagination": pagination(h.page, h.totalPage, h.paginationStep, c.Request.URL.Path, q), - "search": h.search, - "header": h.header, - "title": h.getTitle(), - } + ginH["posts"] = postIds + ginH["totalPage"] = h.getTotalPage(totalRaw) + ginH["pagination"] = pagination(h.page, h.totalPage, h.paginationStep, c.Request.URL.Path, q) + ginH["title"] = h.getTitle() } func pagination(currentPage, totalPage, step int, path, query string) (html string) { diff --git a/cache/cache.go b/cache/cache.go index 0ff85e6..f07e545 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -22,12 +22,6 @@ func NewSliceCache[T any](fun func() ([]T, error), duration time.Duration) *Slic } } -func (c *SliceCache[T]) FlushCache() { - c.mutex.Lock() - defer c.mutex.Unlock() - c.data = nil -} - func (c *SliceCache[T]) GetCache() []T { l := len(c.data) expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) @@ -37,6 +31,8 @@ func (c *SliceCache[T]) GetCache() []T { log.Printf("set cache err[%s]", err) return nil } + c.mutex.Lock() + defer c.mutex.Unlock() c.setTime = time.Now() c.data = r } diff --git a/models/model.go b/models/model.go index 9b3cf0b..2a3c956 100644 --- a/models/model.go +++ b/models/model.go @@ -221,11 +221,11 @@ func FindOneById[T Model](id int) (T, error) { return r, nil } -func FirstOne[T Model](where ParseWhere, fields string, in ...[]interface{}) (T, error) { +func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]interface{}) (T, error) { var r T w, args := where.ParseWhere(in...) - tp := "select %s from %s %s" - sql := fmt.Sprintf(tp, fields, r.Table(), w) + tp := "select %s from %s %s %s" + sql := fmt.Sprintf(tp, fields, r.Table(), w, order.parseOrderBy()) err := db.Db.Get(&r, sql, args...) if err != nil { return r, err