From c7b40f76b11034d79b351863e3ba6b0e1e6d67fe Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 20 Sep 2022 12:00:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BC=93=E5=AD=98=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 13 +++++++------ actions/detail.go | 6 ++++-- actions/index.go | 6 ++++-- cache/cache.go | 41 +++++++++++++++++++++++++++++----------- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index b1a4cd9..1e7d9bc 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -1,6 +1,7 @@ package common import ( + "context" "fmt" "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" @@ -170,11 +171,11 @@ func Archives() (r []models.PostArchive) { return archivesCaches.GetCache() } -func Categories() []models.WpTermsMy { - return categoryCaches.GetCache() +func Categories(ctx context.Context) []models.WpTermsMy { + return categoryCaches.GetCache(ctx, time.Second) } -func categories() (terms []models.WpTermsMy, err error) { +func categories(...any) (terms []models.WpTermsMy, err error) { var in = []interface{}{"category"} terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{ {"tt.count", ">", "0", "int"}, @@ -195,10 +196,10 @@ func categories() (terms []models.WpTermsMy, err error) { return } -func RecentPosts() (r []models.WpPosts) { - return recentPostsCaches.GetCache() +func RecentPosts(ctx context.Context) (r []models.WpPosts) { + return recentPostsCaches.GetCache(ctx, time.Second) } -func recentPosts() (r []models.WpPosts, err error) { +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) diff --git a/actions/detail.go b/actions/detail.go index d8d966e..f4ef509 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -1,6 +1,7 @@ package actions import ( + "context" "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -12,9 +13,10 @@ import ( func Detail(c *gin.Context) { var err error - recent := common.RecentPosts() + ctx := context.TODO() + recent := common.RecentPosts(ctx) archive := common.Archives() - categoryItems := common.Categories() + categoryItems := common.Categories(ctx) var h = gin.H{ "title": models.Options["blogname"], "options": models.Options, diff --git a/actions/index.go b/actions/index.go index 892ad16..fb473f2 100644 --- a/actions/index.go +++ b/actions/index.go @@ -1,6 +1,7 @@ package actions import ( + "context" "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" @@ -148,9 +149,10 @@ func (h *indexHandle) getTotalPage(totalRaws int) int { func Index(c *gin.Context) { h := newIndexHandle(c) h.parseParams() + ctx := context.TODO() archive := common.Archives() - recent := common.RecentPosts() - categoryItems := common.Categories() + recent := common.RecentPosts(ctx) + categoryItems := common.Categories(ctx) ginH := gin.H{ "options": models.Options, "recentPosts": recent, diff --git a/cache/cache.go b/cache/cache.go index 7e3b365..0551565 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,7 @@ package cache import ( + "context" "log" "sync" "time" @@ -9,12 +10,13 @@ import ( type SliceCache[T any] struct { data []T mutex *sync.Mutex - setCacheFunc func() ([]T, error) + setCacheFunc func(...any) ([]T, error) expireTime time.Duration setTime time.Time + incr int } -func NewSliceCache[T any](fun func() ([]T, error), duration time.Duration) *SliceCache[T] { +func NewSliceCache[T any](fun func(...any) ([]T, error), duration time.Duration) *SliceCache[T] { return &SliceCache[T]{ mutex: &sync.Mutex{}, setCacheFunc: fun, @@ -28,19 +30,36 @@ func (c *SliceCache[T]) FlushCache() { c.data = nil } -func (c *SliceCache[T]) GetCache() []T { +func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration) []T { l := len(c.data) expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) if l < 1 || (l > 0 && c.expireTime >= 0 && expired) { - r, err := c.setCacheFunc() - if err != nil { - log.Printf("set cache err[%s]", err) - return nil + t := c.incr + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + done := make(chan struct{}) + go func() { + c.mutex.Lock() + defer c.mutex.Unlock() + if c.incr > t { + return + } + r, err := c.setCacheFunc() + if err != nil { + log.Printf("set cache err[%s]", err) + return + } + c.setTime = time.Now() + c.data = r + c.incr++ + done <- struct{}{} + }() + select { + case <-ctx.Done(): + log.Printf("get cache timeout") + case <-done: + } - c.mutex.Lock() - defer c.mutex.Unlock() - c.setTime = time.Now() - c.data = r } return c.data }