56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package service
|
|
|
|
import (
|
|
"Blog/third_party/database"
|
|
"Common/consts"
|
|
"Common/model/blog"
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type articleService struct {
|
|
}
|
|
|
|
var ArticleService articleService
|
|
|
|
func (*articleService) InitArticleData() {
|
|
var articleSlice []blog.BlogArticle
|
|
result := database.GormTemplate.Where("state = ?", consts.ARTICLE_STATE_PUBLISH).Find(&articleSlice)
|
|
log.Println("文章初始化数据加载量:", result.RowsAffected)
|
|
ctx := context.Background()
|
|
for _, article := range articleSlice {
|
|
// log.Println(article)
|
|
publishTime := article.PublishTime
|
|
err := database.RedisTemplate.ZAdd(ctx, consts.REDIS_BLOG_ARTICLE_LATEST, redis.Z{Score: float64(publishTime), Member: &article}).Err()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
|
|
type viewRecord struct {
|
|
RelId string
|
|
Counts int
|
|
}
|
|
var records []viewRecord
|
|
database.GormTemplate.Table("blog_view_record").Select("rel_id ,count(1) counts").Group("rel_id").Find(&records)
|
|
|
|
for _, val := range records {
|
|
database.RedisTemplate.HSet(ctx, consts.REDIS_BLOG_VIEW_RECORD, map[string]any{val.RelId: val.Counts})
|
|
}
|
|
}
|
|
|
|
func (*articleService) GetBlogArticle(id string) blog.BlogArticle {
|
|
ctx := context.Background()
|
|
var article blog.BlogArticle
|
|
key := consts.REDIS_BLOG_ARTICLE + id
|
|
err := database.RedisTemplate.HGetAll(ctx, key).Scan(&article)
|
|
if err != nil || article.Id == "" {
|
|
database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(&article)
|
|
database.RedisTemplate.HSet(ctx, key, article)
|
|
}
|
|
|
|
return article
|
|
}
|