diff --git a/helper/html/html.go b/helper/html/html.go
index 21c31c9..ea598af 100644
--- a/helper/html/html.go
+++ b/helper/html/html.go
@@ -192,3 +192,20 @@ func RenderedHtml(t *template.Template, data map[string]any) (r string, err erro
r = buf.String()
return
}
+
+func BuildOptions[T any, K comparable](a []T, selected K, fn func(T) (K, any), attr ...string) string {
+ s := strings2.NewBuilder()
+ att := ""
+ if len(attr) > 0 {
+ att = strings.Join(attr, " ")
+ }
+ for _, t := range a {
+ k, v := fn(t)
+ ss := ""
+ if k == selected {
+ ss = "selected"
+ }
+ s.Sprintf(``, ss, att, v, k)
+ }
+ return s.String()
+}
diff --git a/helper/number/number.go b/helper/number/number.go
index 6be801b..a1c5a9e 100644
--- a/helper/number/number.go
+++ b/helper/number/number.go
@@ -91,3 +91,13 @@ func Divide[T constraints.Integer | constraints.Float](i, j T) T {
func CalTotalPage[T constraints.Integer](totalRows, size T) T {
return T(math.Ceil(float64(totalRows) / float64(size)))
}
+
+type Counter[T constraints.Integer] func() T
+
+func Counters[T constraints.Integer]() func() T {
+ var count T
+ return func() T {
+ count++
+ return count
+ }
+}
diff --git a/helper/number/number_test.go b/helper/number/number_test.go
index 2f1602e..7c6ab61 100644
--- a/helper/number/number_test.go
+++ b/helper/number/number_test.go
@@ -2,6 +2,7 @@ package number
import (
"fmt"
+ "github.com/fthvgb1/wp-go/taskPools"
"golang.org/x/exp/constraints"
"reflect"
"testing"
@@ -250,3 +251,40 @@ func TestCalTotalPage(t *testing.T) {
})
}
}
+
+func TestCounters(t *testing.T) {
+ type testCase[T constraints.Integer] struct {
+ name string
+ want func() T
+ }
+ var c = 0
+ tests := []testCase[int]{
+ {
+ name: "t1",
+ want: func() int {
+ c++
+ return c
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := Counters[int]()
+ if !reflect.DeepEqual(got(), tt.want()) {
+ t.Errorf("Counters() = %v, want %v", got(), tt.want())
+ }
+ got()
+ got()
+ got()
+ p := taskPools.NewPools(6)
+ for i := 0; i < 50; i++ {
+ p.Execute(func() {
+ got()
+ })
+ }
+ p.Wait()
+ fmt.Println("got ", got())
+ })
+
+ }
+}
diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go
index bb5409e..66b40e2 100644
--- a/internal/pkg/cache/cache.go
+++ b/internal/pkg/cache/cache.go
@@ -3,11 +3,9 @@ package cache
import (
"context"
"github.com/fthvgb1/wp-go/cache"
- "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/cmd/cachemanager"
"github.com/fthvgb1/wp-go/internal/pkg/config"
- "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/dao"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
@@ -17,7 +15,7 @@ import (
var postContextCache *cache.MapCache[uint64, dao.PostContext]
var archivesCaches *Arch
-var categoryAndTagsCaches *cache.VarCache[[]models.TermsMy]
+var categoryAndTagsCaches *cache.MapCache[int, []models.TermsMy]
var recentPostsCaches *cache.VarCache[[]models.Posts]
var recentCommentsCaches *cache.VarCache[[]models.Comments]
var postCommentCaches *cache.MapCache[uint64, []uint64]
@@ -65,7 +63,7 @@ func InitActionsCommonCache() {
postMetaCache = cachemanager.MapBatchCacheBy(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime)
- categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime)
+ categoryAndTagsCaches = cachemanager.MapCacheBy[int](dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime)
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime)
@@ -128,22 +126,22 @@ func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
//
// t is constraints.Tag or constraints.Category
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
- r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
- logs.ErrPrintln(err, "get category err")
+ tt := 0
if len(t) > 0 {
- return slice.Filter(r, func(my models.TermsMy, i int) bool {
- return helper.Or(t[0] == constraints.Tag, "post_tag", "category") == my.Taxonomy
- })
+ tt = t[0]
}
+ r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
+ logs.ErrPrintln(err, "get category err")
return r
}
-func AllCategoryTagsNames(ctx context.Context, c int) map[string]struct{} {
- r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
+func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} {
+ tt := 0
+ if len(t) > 0 {
+ tt = t[0]
+ }
+ r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
logs.ErrPrintln(err, "get category err")
- return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
- if helper.Or(c == constraints.Tag, "post_tag", "category") == t.Taxonomy {
- return t.Name, struct{}{}, true
- }
- return "", struct{}{}, false
- })
+ return slice.ToMap(r, func(t models.TermsMy) (string, struct{}) {
+ return t.Name, struct{}{}
+ }, true)
}
diff --git a/internal/pkg/dao/common.go b/internal/pkg/dao/common.go
index 6907c61..7ddb116 100644
--- a/internal/pkg/dao/common.go
+++ b/internal/pkg/dao/common.go
@@ -2,6 +2,7 @@ package dao
import (
"context"
+ "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
@@ -21,7 +22,16 @@ type PostContext struct {
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
ctx := a[0].(context.Context)
+ t, ok := a[1].(int)
var in = []any{"category", "post_tag"}
+ if ok {
+ switch t {
+ case constraints.Category:
+ in = []any{"category"}
+ case constraints.Tag:
+ in = []any{"post_tag"}
+ }
+ }
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"tt.count", ">", "0", "int"},
diff --git a/internal/theme/wp/components/block.go b/internal/theme/wp/components/block.go
deleted file mode 100644
index 80f2650..0000000
--- a/internal/theme/wp/components/block.go
+++ /dev/null
@@ -1,71 +0,0 @@
-package components
-
-import (
- "github.com/dlclark/regexp2"
-)
-
-type Block struct {
- Closer string
- NameSpace string
- Name string
- Attrs string
- Void string
- Len int
- StartOffset int
-}
-
-var block = regexp2.MustCompile(`).)*)?}\s+)?(?\/)?-->`, regexp2.IgnoreCase|regexp2.Singleline)
-
-func ParseBlock(s string) []Block {
- m, err := block.FindStringMatch(s)
- if err != nil {
- panic(err)
- }
- var blocks []Block
- for m != nil {
- if m.GroupCount() < 1 {
- continue
- }
-
- b, _ := token(m.Groups())
- b.StartOffset = m.Group.Index
- b.Len = m.Length
- blocks = append(blocks, b)
- m, _ = block.FindNextMatch(m)
- }
- return blocks
-}
-
-func token(g []regexp2.Group) (Block, string) {
- if len(g) < 1 {
- return Block{}, "no-more-tokens"
- }
- b := Block{NameSpace: "core/"}
- for i, group := range g {
- v := group.String()
- if v == "" {
- continue
- }
- switch i {
- case 1:
- b.Closer = v
- case 2:
- b.NameSpace = v
- case 3:
- b.Name = v
- case 4:
- b.Attrs = v
- case 5:
- b.Void = v
- default:
- continue
- }
- }
- if b.Void != "" {
- return b, ""
- }
- if b.Closer != "" {
- return b, "block-closer"
- }
- return b, "block-opener"
-}
diff --git a/internal/theme/wp/components/widget/category.go b/internal/theme/wp/components/widget/category.go
index a304a02..bcfbd26 100644
--- a/internal/theme/wp/components/widget/category.go
+++ b/internal/theme/wp/components/widget/category.go
@@ -144,15 +144,17 @@ func categoryLi(root *tree.Node[models.TermsMy, uint64], cate, roots *tree.Node[
}
s.Sprintf(`
%s %s
-
+
`, category.Terms.TermId, strings.Join(class, " "), aria, category.Name, category.Name, count)
if len(*child.Children) > 0 {
- s.WriteString(`
+ s.WriteString(`
`)
categoryLi(&child, cate, roots, isCount, s)
- s.WriteString(`
`)
+ s.WriteString(`
+`)
}
+ s.Sprintf(``)
}
}
diff --git a/safety/number.go b/safety/number.go
new file mode 100644
index 0000000..4b118e9
--- /dev/null
+++ b/safety/number.go
@@ -0,0 +1,13 @@
+package safety
+
+import (
+ "golang.org/x/exp/constraints"
+ "sync/atomic"
+)
+
+func Counter[T constraints.Integer]() func() T {
+ var counter int64
+ return func() T {
+ return T(atomic.AddInt64(&counter, 1))
+ }
+}