commit
06e91853c1
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
|||
Copyright (c) 2013, Jason Moiron
|
||||
Copyright (c) 2023 星
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
|
|
|
@ -116,6 +116,17 @@ func Slice[T any](arr []T, offset, length int) (r []T) {
|
|||
return
|
||||
}
|
||||
|
||||
func FilterAndToMap[K comparable, V, T any](arr []T, fn func(T) (K, V, bool)) map[K]V {
|
||||
r := make(map[K]V)
|
||||
for _, t := range arr {
|
||||
k, v, ok := fn(t)
|
||||
if ok {
|
||||
r[k] = v
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func Comb[T any](arr []T, m int) (r [][]T) {
|
||||
if m == 1 {
|
||||
for _, t := range arr {
|
||||
|
@ -135,7 +146,6 @@ func Comb[T any](arr []T, m int) (r [][]T) {
|
|||
return r
|
||||
}
|
||||
|
||||
|
||||
func IsContained[T comparable](a T, arr []T) bool {
|
||||
for _, v := range arr {
|
||||
if a == v {
|
||||
|
|
|
@ -132,6 +132,10 @@ func (h *indexHandle) parseParams() (err error) {
|
|||
if category == "" {
|
||||
category = h.c.Param("tag")
|
||||
if category != "" {
|
||||
allNames := cache.AllTagsNames(h.c)
|
||||
if _, ok := allNames[category]; !ok {
|
||||
return errors.New(str.Join("not exists tag ", category))
|
||||
}
|
||||
h.categoryType = "post_tag"
|
||||
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
|
||||
}
|
||||
|
|
45
internal/pkg/cache/cache.go
vendored
45
internal/pkg/cache/cache.go
vendored
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
var postContextCache *cache.MapCache[uint64, dao.PostContext]
|
||||
var archivesCaches *Arch
|
||||
var categoryCaches *cache.VarCache[[]models.TermsMy]
|
||||
var categoryAndTagsCaches *cache.VarCache[[]models.TermsMy]
|
||||
var recentPostsCaches *cache.VarCache[[]models.Posts]
|
||||
var recentCommentsCaches *cache.VarCache[[]models.Comments]
|
||||
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
||||
|
@ -62,7 +62,7 @@ func InitActionsCommonCache() {
|
|||
|
||||
postMetaCache = cache.NewMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.PostDataCacheTime)
|
||||
|
||||
categoryCaches = cache.NewVarCache(dao.Categories, c.CategoryCacheTime)
|
||||
categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CategoryCacheTime)
|
||||
|
||||
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.RecentPostCacheTime)
|
||||
|
||||
|
@ -88,13 +88,6 @@ func InitActionsCommonCache() {
|
|||
|
||||
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.UserInfoCacheTime)
|
||||
|
||||
allCategories = cache.NewVarCache(func(a ...any) (map[string]struct{}, error) {
|
||||
ctx := a[0].(context.Context)
|
||||
return slice.ToMap(Categories(ctx), func(v models.TermsMy) (string, struct{}) {
|
||||
return v.Name, struct{}{}
|
||||
}, true), nil
|
||||
}, c.CategoryCacheTime)
|
||||
|
||||
InitFeed()
|
||||
}
|
||||
|
||||
|
@ -154,12 +147,40 @@ func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
|
|||
}
|
||||
|
||||
func Categories(ctx context.Context) []models.TermsMy {
|
||||
r, err := categoryCaches.GetCache(ctx, time.Second, ctx)
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return my.Taxonomy == "category"
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
||||
r, _ := allCategories.GetCache(ctx, time.Second, ctx)
|
||||
func Tags(ctx context.Context) []models.TermsMy {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return my.Taxonomy == "post_tag"
|
||||
})
|
||||
return r
|
||||
}
|
||||
func AllTagsNames(ctx context.Context) map[string]struct{} {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||
if t.Taxonomy == "post_tag" {
|
||||
return t.Name, struct{}{}, true
|
||||
}
|
||||
return "", struct{}{}, false
|
||||
})
|
||||
}
|
||||
|
||||
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||
if t.Taxonomy == "category" {
|
||||
return t.Name, struct{}{}, true
|
||||
}
|
||||
return "", struct{}{}, false
|
||||
})
|
||||
}
|
||||
|
|
|
@ -26,9 +26,9 @@ func PasswordProjectTitle(post *models.Posts) {
|
|||
}
|
||||
}
|
||||
|
||||
func Categories(a ...any) (terms []models.TermsMy, err error) {
|
||||
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
||||
ctx := a[0].(context.Context)
|
||||
var in = []any{"category"}
|
||||
var in = []any{"category", "post_tag"}
|
||||
terms, err = model.Find[models.TermsMy](ctx, model.SqlBuilder{
|
||||
{"tt.count", ">", "0", "int"},
|
||||
{"tt.taxonomy", "in", ""},
|
||||
|
|
Loading…
Reference in New Issue
Block a user