46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"Blog/third_party/database"
|
|
"Common/consts"
|
|
"Common/model/vo"
|
|
"context"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type contentService struct {
|
|
}
|
|
|
|
var ContentService contentService
|
|
|
|
func (*contentService) InitContentData() {
|
|
var contentSlice []vo.CommonContent
|
|
result := database.GormTemplate.Table("common_contents").Where("state = ?", consts.CONTENT_STATE_PUBLISH).Find(&contentSlice)
|
|
log.Println("大文本初始化数据加载量:", result.RowsAffected)
|
|
ctx := context.Background()
|
|
|
|
for _, content := range contentSlice {
|
|
database.RedisTemplate.Set(ctx, consts.REDIS_BLOG_CONTENT+content.RelId, content.Content, time.Duration(0))
|
|
}
|
|
|
|
}
|
|
|
|
func (*contentService) GetContentByCache(relId string) string {
|
|
ctx := context.Background()
|
|
var content string
|
|
database.RedisTemplate.Get(ctx, consts.REDIS_BLOG_CONTENT+relId).Scan(&content)
|
|
return content
|
|
}
|
|
|
|
func (*contentService) GetContentByDb(relId string) (vo.CommonContent, error) {
|
|
var content vo.CommonContent
|
|
err := database.GormTemplate.Table("common_contents").Where("rel_id = ?", relId).First(&content).Error
|
|
return content, err
|
|
}
|
|
|
|
func (*contentService) UpdataState(relId string, state string) error {
|
|
err := database.GormTemplate.Table("common_contents").Where("rel_id = ?", relId).UpdateColumn("state", state).Error
|
|
return err
|
|
}
|