左侧栏评论
This commit is contained in:
parent
a3c08edb71
commit
a656ea3fd5
|
@ -20,6 +20,7 @@ var archivesCaches *Arch
|
||||||
var categoryCaches *cache.SliceCache[models.WpTermsMy]
|
var categoryCaches *cache.SliceCache[models.WpTermsMy]
|
||||||
var recentPostsCaches *cache.SliceCache[models.WpPosts]
|
var recentPostsCaches *cache.SliceCache[models.WpPosts]
|
||||||
var monthCaches *cache.MapCache[string, []models.WpPosts]
|
var monthCaches *cache.MapCache[string, []models.WpPosts]
|
||||||
|
var recentCommentsCaches *cache.SliceCache[models.WpComments]
|
||||||
|
|
||||||
func InitCache() {
|
func InitCache() {
|
||||||
archivesCaches = &Arch{
|
archivesCaches = &Arch{
|
||||||
|
@ -29,6 +30,7 @@ func InitCache() {
|
||||||
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
||||||
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
||||||
monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute)
|
monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute)
|
||||||
|
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Arch struct {
|
type Arch struct {
|
||||||
|
@ -66,6 +68,19 @@ func GetMonthPost(ctx context.Context, year, month string) ([]models.WpPosts, er
|
||||||
return monthCaches.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
|
return monthCaches.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RecentComments(ctx context.Context) (r []models.WpComments) {
|
||||||
|
r, _ = recentCommentsCaches.GetCache(ctx, time.Second)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func recentComments(...any) (r []models.WpComments, err error) {
|
||||||
|
return models.Find[models.WpComments](models.SqlBuilder{
|
||||||
|
{"comment_approved", "1"},
|
||||||
|
{"post_status", "publish"},
|
||||||
|
}, "comment_ID,comment_author,comment_post_ID,post_title", "", models.SqlBuilder{{"comment_date_gmt", "desc"}}, models.SqlBuilder{
|
||||||
|
{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"},
|
||||||
|
}, 5)
|
||||||
|
}
|
||||||
|
|
||||||
func getMonthPost(args ...any) ([]models.WpPosts, error) {
|
func getMonthPost(args ...any) ([]models.WpPosts, error) {
|
||||||
y := args[0].(string)
|
y := args[0].(string)
|
||||||
m := args[1].(string)
|
m := args[1].(string)
|
||||||
|
|
|
@ -17,12 +17,14 @@ func Detail(c *gin.Context) {
|
||||||
recent := common.RecentPosts(ctx)
|
recent := common.RecentPosts(ctx)
|
||||||
archive := common.Archives()
|
archive := common.Archives()
|
||||||
categoryItems := common.Categories(ctx)
|
categoryItems := common.Categories(ctx)
|
||||||
|
comments := common.RecentComments(ctx)
|
||||||
var h = gin.H{
|
var h = gin.H{
|
||||||
"title": models.Options["blogname"],
|
"title": models.Options["blogname"],
|
||||||
"options": models.Options,
|
"options": models.Options,
|
||||||
"recentPosts": recent,
|
"recentPosts": recent,
|
||||||
"archives": archive,
|
"archives": archive,
|
||||||
"categories": categoryItems,
|
"categories": categoryItems,
|
||||||
|
"comments": comments,
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
c.HTML(http.StatusOK, "posts/detail.gohtml", h)
|
c.HTML(http.StatusOK, "posts/detail.gohtml", h)
|
||||||
|
|
|
@ -153,6 +153,7 @@ func Index(c *gin.Context) {
|
||||||
archive := common.Archives()
|
archive := common.Archives()
|
||||||
recent := common.RecentPosts(ctx)
|
recent := common.RecentPosts(ctx)
|
||||||
categoryItems := common.Categories(ctx)
|
categoryItems := common.Categories(ctx)
|
||||||
|
comments := common.RecentComments(ctx)
|
||||||
ginH := gin.H{
|
ginH := gin.H{
|
||||||
"options": models.Options,
|
"options": models.Options,
|
||||||
"recentPosts": recent,
|
"recentPosts": recent,
|
||||||
|
@ -161,6 +162,7 @@ func Index(c *gin.Context) {
|
||||||
"search": h.search,
|
"search": h.search,
|
||||||
"header": h.header,
|
"header": h.header,
|
||||||
"title": h.getTitle(),
|
"title": h.getTitle(),
|
||||||
|
"comments": comments,
|
||||||
}
|
}
|
||||||
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status)
|
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
1
cache/map.go
vendored
1
cache/map.go
vendored
|
@ -37,6 +37,7 @@ func (c *MapCache[K, V]) GetCache(ctx context.Context, key K, timeout time.Durat
|
||||||
_, ok := c.data[key]
|
_, ok := c.data[key]
|
||||||
var err error
|
var err error
|
||||||
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
||||||
|
//todo 这里应该判断下取出的值是否为零值,不过怎么操作呢?
|
||||||
if !ok || (c.expireTime >= 0 && expired) {
|
if !ok || (c.expireTime >= 0 && expired) {
|
||||||
t := c.incr
|
t := c.incr
|
||||||
call := func() {
|
call := func() {
|
||||||
|
|
|
@ -21,3 +21,5 @@ recentPostCacheTime: 30m
|
||||||
categoryCacheTime: 30m
|
categoryCacheTime: 30m
|
||||||
# 上下篇缓存时间
|
# 上下篇缓存时间
|
||||||
contextPostCacheTime: 1h
|
contextPostCacheTime: 1h
|
||||||
|
# 最近评论缓存时间
|
||||||
|
recentCommentsCacheTime: 10m
|
||||||
|
|
|
@ -25,7 +25,7 @@ func StructColumn[T any, M any](arr []M, field string) (r []T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](start, end, step T) []T {
|
func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](start, end, step T) []T {
|
||||||
r := make([]T, 0, int(end/step+1))
|
r := make([]T, 0, int((end-start+1)/step+1))
|
||||||
for i := start; i <= end; {
|
for i := start; i <= end; {
|
||||||
r = append(r, i)
|
r = append(r, i)
|
||||||
i = i + step
|
i = i + step
|
||||||
|
@ -45,3 +45,16 @@ func StrJoin(s ...string) (str string) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SlicePagination[T any](arr []T, page, pageSize int) []T {
|
||||||
|
start := (page - 1) * pageSize
|
||||||
|
l := len(arr)
|
||||||
|
if start > l {
|
||||||
|
start = l
|
||||||
|
}
|
||||||
|
end := page * pageSize
|
||||||
|
if l < end {
|
||||||
|
end = l
|
||||||
|
}
|
||||||
|
return arr[start:end]
|
||||||
|
}
|
||||||
|
|
|
@ -103,6 +103,15 @@ func TestRangeSlice(t *testing.T) {
|
||||||
},
|
},
|
||||||
want: []int{0, 2, 4},
|
want: []int{0, 2, 4},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "t3",
|
||||||
|
args: args{
|
||||||
|
start: 1,
|
||||||
|
end: 11,
|
||||||
|
step: 3,
|
||||||
|
},
|
||||||
|
want: []int{1, 4, 7, 10},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
@ -132,3 +141,58 @@ func TestStrJoin(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSlicePagination(t *testing.T) {
|
||||||
|
arr := RangeSlice[int](1, 10, 1)
|
||||||
|
type args struct {
|
||||||
|
arr []int
|
||||||
|
page int
|
||||||
|
pageSize int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "t1",
|
||||||
|
args: args{
|
||||||
|
arr: arr,
|
||||||
|
page: 1,
|
||||||
|
pageSize: 2,
|
||||||
|
},
|
||||||
|
want: RangeSlice[int](1, 2, 1),
|
||||||
|
}, {
|
||||||
|
name: "t2",
|
||||||
|
args: args{
|
||||||
|
arr: arr,
|
||||||
|
page: 2,
|
||||||
|
pageSize: 2,
|
||||||
|
},
|
||||||
|
want: RangeSlice[int](3, 4, 1),
|
||||||
|
}, {
|
||||||
|
name: "t3",
|
||||||
|
args: args{
|
||||||
|
arr: arr,
|
||||||
|
page: 4,
|
||||||
|
pageSize: 3,
|
||||||
|
},
|
||||||
|
want: []int{10},
|
||||||
|
}, {
|
||||||
|
name: "t4",
|
||||||
|
args: args{
|
||||||
|
arr: arr,
|
||||||
|
page: 5,
|
||||||
|
pageSize: 3,
|
||||||
|
},
|
||||||
|
want: []int{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := SlicePagination(tt.args.arr, tt.args.page, tt.args.pageSize); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("SlicePagination() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ type WpComments struct {
|
||||||
CommentType string `gorm:"column:comment_type" db:"comment_type" json:"comment_type" form:"comment_type"`
|
CommentType string `gorm:"column:comment_type" db:"comment_type" json:"comment_type" form:"comment_type"`
|
||||||
CommentParent uint64 `gorm:"column:comment_parent" db:"comment_parent" json:"comment_parent" form:"comment_parent"`
|
CommentParent uint64 `gorm:"column:comment_parent" db:"comment_parent" json:"comment_parent" form:"comment_parent"`
|
||||||
UserId uint64 `gorm:"column:user_id" db:"user_id" json:"user_id" form:"user_id"`
|
UserId uint64 `gorm:"column:user_id" db:"user_id" json:"user_id" form:"user_id"`
|
||||||
|
//扩展字段
|
||||||
|
PostTitle string `db:"post_title"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w WpComments) PrimaryKey() string {
|
func (w WpComments) PrimaryKey() string {
|
||||||
|
|
|
@ -24,7 +24,13 @@
|
||||||
</aside>
|
</aside>
|
||||||
<aside id="recent-comments-2" class="widget widget_recent_comments"><h2 class="widget-title">近期评论</h2>
|
<aside id="recent-comments-2" class="widget widget_recent_comments"><h2 class="widget-title">近期评论</h2>
|
||||||
<nav aria-label="近期评论">
|
<nav aria-label="近期评论">
|
||||||
<ul id="recentcomments"></ul>
|
<ul id="recentcomments">
|
||||||
|
{{ range $i,$v := .comments}}
|
||||||
|
<li class="recentcomments">
|
||||||
|
<span class="comment-author-link">{{$v.CommentAuthor}}</span>发表在《<a class="wp-block-latest-comments__comment-link" href="/p/{{$v.CommentPostId}}#comment-{{$v.CommentId}}">{{$v.PostTitle}}</a>》
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
<aside id="archives-2" class="widget widget_archive"><h2 class="widget-title">归档</h2>
|
<aside id="archives-2" class="widget widget_archive"><h2 class="widget-title">归档</h2>
|
||||||
|
|
|
@ -10,11 +10,12 @@ import (
|
||||||
var Conf Config
|
var Conf Config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Mysql Mysql `yaml:"mysql"`
|
Mysql Mysql `yaml:"mysql"`
|
||||||
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
|
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
|
||||||
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
|
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
|
||||||
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
|
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
|
||||||
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
|
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
|
||||||
|
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mysql struct {
|
type Mysql struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user