From 096514a677c09bbff9148dbb7a45179064f4ea5a Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 6 Mar 2023 23:43:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=90=8E=E5=8F=B0=E8=BF=91?= =?UTF-8?q?=E6=9C=9F=E6=96=87=E7=AB=A0=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmd/reload/reload.go | 16 +++++--- internal/pkg/cache/posts.go | 8 ++-- internal/pkg/dao/posts.go | 4 +- internal/theme/fs.go | 2 +- internal/theme/templateFuncs.go | 3 ++ .../theme/twentyfifteen/layout/sidebar.gohtml | 14 +------ .../twentyseventeen/layout/sidebar.gohtml | 14 +------ .../theme/wp/components/widget/archive.gohtml | 38 ++++++++++++++++++ .../wp/components/widget/recent-posts.gohtml | 18 +++++++++ internal/theme/wp/template.gohtml | 39 ------------------- internal/theme/wp/widgetareadata.go | 36 ++++++++++++++--- internal/theme/wp/wp.go | 9 ++--- internal/wpconfig/options.go | 2 +- model/condition.go | 12 +++--- 14 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 internal/theme/wp/components/widget/archive.gohtml create mode 100644 internal/theme/wp/components/widget/recent-posts.gohtml diff --git a/internal/cmd/reload/reload.go b/internal/cmd/reload/reload.go index 589013e..8e8721a 100644 --- a/internal/cmd/reload/reload.go +++ b/internal/cmd/reload/reload.go @@ -10,11 +10,16 @@ var calls []func() var str = safety.NewMap[string, string]() -func GetStr(name string) (string, bool) { - return str.Load(name) -} -func SetStr(name, val string) { - str.Store(name, val) +var anyMap = safety.NewMap[string, any]() + +func GetAnyValBy[T any](k string, fn func() T) T { + v, ok := anyMap.Load(k) + if ok { + return v.(T) + } + vv := fn() + anyMap.Store(k, vv) + return vv } func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string { @@ -52,5 +57,6 @@ func Reload() { for _, call := range calls { call() } + anyMap.Flush() str.Flush() } diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index 52f2bef..1709070 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -3,9 +3,12 @@ package cache import ( "context" "fmt" + "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/gin-gonic/gin" "time" ) @@ -44,9 +47,8 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) { func RecentPosts(ctx context.Context, n int) (r []models.Posts) { nn := n - if nn <= 5 { - nn = 10 - } + feedNum := str.ToInteger(wpconfig.GetOption("posts_per_rss"), 10) + nn = number.Max(n, feedNum) r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx, nn) if n < len(r) { r = r[:n] diff --git a/internal/pkg/dao/posts.go b/internal/pkg/dao/posts.go index 64007d1..fc4ba23 100644 --- a/internal/pkg/dao/posts.go +++ b/internal/pkg/dao/posts.go @@ -129,8 +129,8 @@ func RecentPosts(a ...any) (r []models.Posts, err error) { {"post_type", "post"}, {"post_status", "publish"}, }), - model.Fields("ID,post_title,post_password"), - model.Order(model.SqlBuilder{{"post_date", "desc"}}), + model.Fields("ID,post_title,post_password,post_date_gmt"), + model.Order([][]string{{"post_date", "desc"}}), model.Limit(num), )) return diff --git a/internal/theme/fs.go b/internal/theme/fs.go index 5eaa264..bae4ddb 100644 --- a/internal/theme/fs.go +++ b/internal/theme/fs.go @@ -33,7 +33,7 @@ func commonTemplate(t *multipTemplate.MultipleFsTemplate) { for _, main := range m { file := filepath.Base(main) dir := strings.Split(main, "/")[0] - templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml")) + templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml", "wp/*/*/*.gohtml")) t.SetTemplate(main, templ) } } diff --git a/internal/theme/templateFuncs.go b/internal/theme/templateFuncs.go index cc31132..ab788d0 100644 --- a/internal/theme/templateFuncs.go +++ b/internal/theme/templateFuncs.go @@ -13,6 +13,9 @@ var comFn = template.FuncMap{ "dateCh": func(t time.Time) any { return t.Format("2006年 01月 02日") }, + "timeFormat": func(t time.Time, format string) any { + return t.Format(format) + }, "getOption": func(k string) string { return wpconfig.GetOption(k) }, diff --git a/internal/theme/twentyfifteen/layout/sidebar.gohtml b/internal/theme/twentyfifteen/layout/sidebar.gohtml index dba5de8..367b315 100644 --- a/internal/theme/twentyfifteen/layout/sidebar.gohtml +++ b/internal/theme/twentyfifteen/layout/sidebar.gohtml @@ -9,19 +9,7 @@ - + {{template "common/recent-posts" .}}